Computer Knowledge

Object-Oriented Programming

2,686 Questions

Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.

Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects

Object-Oriented Programming Questions

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Generics were introduced in Java 5 (J2SE 5.0, released in 2004), not in Java 1.4. Java 1.4 was released in 2002 and did not include generic type support. The introduction of generics allowed developers to write type-safe collections and eliminate the need for explicit casting when retrieving objects from collections.

Multiple choice technology
  1. Public

  2. Protected

  3. Private

  4. Default

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Objective-C, instance variables default to @protected access, meaning they are accessible within the class and its subclasses. This differs from C++ and Java where the default is typically private. @public would allow external access, @private restricts to the defining class only.

Multiple choice technology
  1. Encaplulation

  2. Polymorphism

  3. Multiple Inheritence

  4. Method overriding

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Objective-C supports encapsulation (through @private/@public), polymorphism (through method overriding and dynamic typing), and method overriding. However, Objective-C does NOT support multiple inheritance - classes can only inherit from one superclass. Protocols provide a similar capability without true multiple inheritance.

Multiple choice technology
  1. NSString

  2. NSObject

  3. NSControl

  4. NSButton

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

NSObject is the root class in the Objective-C runtime and Cocoa framework. All other classes like NSString, NSControl, and NSButton inherit from NSObject, making it the base of the class hierarchy.

Multiple choice technology
  1. @interface and @implement

  2. @property and @synthesis

  3. @category and @protocol

  4. none

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The @property directive declares accessor methods in the interface, and @synthesize implements them in the implementation. This combination automatically generates getter and setter methods for instance variables.

Multiple choice technology
  1. @property <type> <name>;

  2. @property (<parameters>) <type> <name>;

  3. @property (<parameters>) <name>;

  4. none

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Property declarations require the @property directive followed by optional attributes in parentheses, then the type and name. The full syntax is @property (attributes) type name; where attributes like nonatomic, strong, etc. are optional but commonly used.

Multiple choice technology programming languages
  1. Compile error

  2. Runtime Exception

  3. No error

  4. Rintime Error

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code attempts to assign a superclass object (A instance) to a subclass reference (B b). Even though B extends A, an A object is not a B. Java forbids this unsafe assignment at compile-time. You cannot assign a parent class instance to a child class reference without explicit casting.

Multiple choice technology programming languages
  1. Compiler error

  2. Runtime Exception

  3. No errors

  4. Runtime Error

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Class B extends Class A, making B a subclass of A. In Java, assigning an instance of B to a reference variable of type A (A a = new B()) is a valid polymorphic upcast. This code compiles successfully and executes without generating any runtime exceptions or errors.

Multiple choice technology programming languages
  1. Yes, always it should implement

  2. No, Never

  3. No, Not when A is abstract

  4. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Concrete classes implementing an interface must implement all interface methods. However, abstract classes are exempt - they can declare abstract methods instead of implementing them. This allows subclasses to provide the actual implementations. The 'always' and 'never' options are too absolute.

Multiple choice technology programming languages
  1. volatile

  2. private

  3. default

  4. static

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Java, a top-level class cannot be declared private - it can only be public or package-private (no modifier). Private only applies to nested classes. Volatile, static, and default (package access) modifiers are valid for classes. The claimed answer B is CORRECT.

Multiple choice technology programming languages
  1. double is primitive datatypes. Double is a class

  2. Both are primitive dataType

  3. Both are class type

  4. double is class and Double is primitive Type

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In Java, 'double' is a primitive data type that stores 64-bit floating-point numbers, while 'Double' is a wrapper class that provides object representation of double values. This is part of Java's type system where primitives have direct memory representation and wrapper classes enable object-oriented operations. Options B, C, and D incorrectly classify these types.