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 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.

Multiple choice technology programming languages
  1. Yes

  2. No

  3. In specific conditions

  4. Not sure

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

Java anonymous classes do not have a name, which prevents developers from declaring constructors, since a constructor must share the name of its declaring class. Instead, custom initialization is achieved using instance initializers or by passing parameters to the superclass constructor, so defining a constructor is not allowed.

Multiple choice technology programming languages
  1. Yes

  2. No

  3. In specific conditions

  4. Not sure

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

In Java, inner classes are non-static nested classes associated with an instance of the outer class. Consequently, they cannot declare static members (such as static methods or fields) unless they are compile-time constant fields (static final primitives/Strings). Hence, the general answer is no.