Computer Knowledge

Object-Oriented Programming

2,239 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 architecture
  1. Facade pattern

  2. Specialization pattern

  3. Value List Iterator pattern

  4. Factory pattern

  5. None of the above

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

The Factory pattern encapsulates object creation logic and hides implementation details from client code. Clients request objects from a factory without knowing which concrete class was instantiated or how the creation process works. Facade simplifies interfaces, Specialization isn't a standard pattern, and Value List Iterator is for pagination.

Multiple choice technology architecture
  1. Inheritance pattern

  2. Generalization pattern

  3. Specialization pattern

  4. Implementation pattern

  5. None of the above

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

Inheritance allows a derived class to acquire all methods and properties of a base class and can override or modify their behavior. This matches the requirement of having the same interface as another class with the ability to modify operations. While 'Inheritance pattern' isn't a standard GoF name, it describes the fundamental OOP concept. Generalization and Specialization are UML relationships, not standalone patterns.

Multiple choice technology architecture
  1. Invoker pattern

  2. Double Dispatch pattern

  3. Factory pattern

  4. Intercepting Filter

  5. None of the above

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

The Double Dispatch pattern (Visitor pattern family) allows an operation to vary based on both the object type and the operation type. It enables a class to perform different operations on different object types by dispatching twice - once on the object and once on the visitor. This is ideal when a class must operate on diverse object types with type-specific behavior. Invoker is Command pattern, Factory creates objects, Intercepting Filter modifies requests.

Multiple choice technology architecture
  1. Generalization pattern

  2. Encapsulation

  3. Inheritance

  4. Polymorphism

  5. None of the above

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

Polymorphism allows methods with the same name to behave differently based on the object type they operate on. Through method overriding in inheritance or interface implementation, the same method name can have type-specific implementations. Generalization is a UML relationship, Encapsulation hides data, and Inheritance is the mechanism that enables polymorphism but isn't the behavior itself.

Multiple choice technology programming languages
  1. B

  2. A

  3. Compilation fails

  4. none of these

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

Compilation fails because _a() returns void, but System.out.println(aa._a()) attempts to print a void return value. Void methods cannot be used in print statements - they must be called separately. The code would compile if _a() returned a value or if the print statement didn't try to capture the return.

Multiple choice technology programming languages
  1. A

  2. B

  3. BA

  4. AB

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

Instantiating A1 invokes the superclass constructor B1() which outputs 'B'. The subclass method void A1() looks like a constructor but is a regular method due to its void return type, meaning it is not executed during class instantiation.

Multiple choice technology programming languages
  1. A2

  2. B2

  3. Compilation fails

  4. runtime error

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

Static methods in Java are resolved at compile-time based on the declared reference type, not the runtime object type. Since reference variable a is of type A2, calling a.doprint() executes A2's static method, not the subclass B2's method.

Multiple choice technology programming languages
  1. Public.

  2. Private.

  3. Protected.

  4. static.

  5. Internal.

  6. ProtectedInternal.

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

In C#, constants (marked with 'const' keyword) are implicitly static and cannot be marked with the 'static' keyword explicitly. Access modifiers like public, private, protected, and internal are all valid for constants - they control visibility. ProtectedInternal is also valid. Attempting to mark a constant as static would cause a compilation error.

Multiple choice technology programming languages
  1. BinaryFormatter and SOAPFormatter

  2. BinaryFormatter and XmlSerializer

  3. XmlSerializer.

  4. BinaryFormatter , SOAP Formatter, XmlSerializer

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

BinaryFormatter and SOAPFormatter both require the [Serializable] attribute to serialize and deserialize objects. These formatters work with the full object graph and need the attribute to know which types can be safely serialized. XmlSerializer does NOT require [Serializable] - it only serializes public fields/properties and works differently, requiring parameterless constructors instead. This is why option A is correct and option D (which includes XmlSerializer) is incorrect.

Multiple choice technology programming languages
  1. True.

  2. False

  3. True , if the Serializable attribute is not used.

  4. The members have to be protected.

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

The .NET XmlSerializer requires all classes, fields, and properties to be declared public to perform XML serialization. Private, protected, or internal members are completely ignored during serialization, making the public declaration requirement true and correct.

Multiple choice technology programming languages
  1. .Net doesnot support multiple inheritance. Hence cannot inherit all the 3, results in error.

  2. .Net allows to inherit only 1 class and 1 interface, hence error.

  3. The class has to be inherited first and then the interfaces.

  4. The interface has to be inherited first then class.

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

In .NET, a class can inherit from exactly one base class but implement multiple interfaces. The syntax requires the base class to be listed first, followed by interfaces: class Derived : Base, I1, I2. Option A is incorrect because interfaces circumvent the multiple inheritance restriction. Option B is wrong because .NET allows implementing multiple interfaces. Option D is incorrect because the base class must precede interfaces in the declaration.