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 web technology
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, 'extends' is valid only when both X and Y are classes, or when both X and Y are interfaces. A class cannot extend an interface (it implements it), and an interface cannot extend a class. This rule maintains the type system's consistency - inheritance relationships must be between the same kind of type.

Multiple choice technology web technology
  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

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

The code tests Java's method overload resolution with varargs. sifter(aa) calls sifter(A[]... a2) matching -4. sifter(ba) matches sifter(B[] b1) NOT sifter(B[]... b1) because Java prefers non-varargs over varargs when both match - giving -3. sifter(7) matches sifter(Object o) as 7 autoboxes to Integer then widens to Object - giving -4. Final result: -434.

Multiple choice technology web technology
  1. Protected

  2. Private

  3. Public

  4. Public & private

  5. Protected & Public

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

Java's access modifiers follow this order: private (most restrictive) → default (package) → protected → public (least restrictive). When overriding a method, the subclass cannot make it more restrictive than the parent. A protected method can be overridden as protected or public, but NOT private (too restrictive) or default (package-private).

Multiple choice technology web technology
  1. Non Static field

  2. Base class fields

  3. Transient Fields

  4. none

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

During Java serialization, transient fields are explicitly ignored and not included in the serialized output. The transient keyword is used specifically to mark fields that should not be serialized. Static fields are also not serialized, but the question specifically asks about which fields are ignored, and transient is the key mechanism for this purpose.

Multiple choice technology web technology
  1. must have empty constructor

  2. should have no public fields

  3. Both a and b

  4. None of a and b

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

Java Beans must have a no-argument constructor (empty constructor) to enable instantiation by containers and frameworks. They should follow encapsulation principles by having no public fields - all data access should be through getter and setter methods. Both requirements are part of the Java Bean specification, making option C correct.

Multiple choice technology web technology
  1. 500

  2. 100

  3. 0

  4. Null Pointer Exception

Reveal answer Fill a bubble to check yourself
A Correct answer
Multiple choice technology programming languages
  1. UX

  2. tcs

  3. null

  4. Exception

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

The output is null, not 'tcs' or 'UX'. The testPoly() method returns a TestPolymorphism2 object, but the return value is discarded - it's not assigned to TSuper. TSuper.name remains null because the superclass's name field was never initialized (the subclass's local name is different).

Multiple choice technology programming languages
  1. TestSup = 100 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

  2. TestSup = 500 ,TestPoly = 500 ,TP = 500 ,TS = 500 ,TSP = 500

  3. TestSup = 100 ,TestPoly = 400 ,TP = 400 ,TS = 100 ,TSP = 500

  4. TestSup = 500 ,TestPoly = 400 ,TP = 500 ,TS = 100 ,TSP = 400

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

Since the variable p is declared as static in the superclass TestSup, a single shared copy of p exists for both classes and all instances. Modifying p through any reference (TP, TS, TSP) updates this single value. The last assignment sets p to 500, so all print statements output 500.

Multiple choice technology programming languages
  1. p = 200.0, p1 = 30.0

  2. p = 200.0, p1 = 200.0

  3. p = 30.0, p1 = 30.0

  4. Compiler Error

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

Compiler error occurs because p1 is not declared - 'p1 = TSU.TestPoly(11, 9.0, 10);' lacks a type declaration. Should be 'double p1 = ...'. The code has nothing to do with polymorphism behavior - it's a simple syntax error.

Multiple choice technology programming languages
  1. TestSupe = 300 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  2. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 500 ,TSP = 500

  3. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  4. TestSupe = 300 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

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

In Java, static variables are NOT polymorphic - they are bound to the reference type, not the actual object type. When we assign TSP.p = 500, TSP is declared as TestSupe, so it sets TestSupe.p to 500. Similarly, TP.p = 400 sets TestPoly1.p to 400. When printing static variables using the class name (TestSupe.p or TestPoly1.p), we access the variable for that specific class. This is static variable hiding, not overriding.

Multiple choice technology programming languages
  1. A Java class encapsulating one or more advices.

  2. A child class inheriting properties of parent class

  3. A class that implements an interface

  4. Transaction handling capability of spring framework

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

In Aspect-Oriented Programming (AOP), particularly in Spring AOP, an aspect is a modularization of a concern that cuts across multiple objects, typically implemented as a regular Java class annotated or configured to encapsulate advices and pointcuts. The other options describe inheritance, interface implementation, or specific framework capabilities.

Multiple choice technology programming languages
  1. You need two unconnected objects to be able to send messages to each other.

  2. You need two connected objects to be able to send messages to each other.

  3. You need to create a new operation on an object and you will change the classes of elements on which it operates.

  4. You need to create a new operation on an object without changing the classes of elements on which it operates.

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

Visitor separates operations from object structure, adding operations WITHOUT modifying element classes. It uses double-dispatch: visitors visit elements, elements accept visitors. This avoids changing existing classes when adding new operations - central to Visitor's intent.

Multiple choice technology programming languages
  1. When you need to have objects notified of events but you don't know which objects would have such needs, or if you will need to add more objects to receive such notification, at a later date.

  2. You want one object to monitor when the state of another object but you don't want the object being monitored to need to send any messages regarding its state.

  3. When the instances of your class can be use interchangeable and you want to reduce the number of instances created in order to improve performance.

  4. When you need to co-ordinate state changes between other objects by using one object.

  5. You are building an online auction site to sell rare and collectable toys. You want customers to be notified of bids on items they are bidding for in as close to real time as possible. You would use the Observer pattern to notify the customer objects of c

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

Observer notifies subscribers of events without knowing who they are (A), and enables real-time notifications like auction bids (E). It's perfect when you need to notify unknown or changing numbers of objects. Option B describes polling not notification, C describes Flyweight (performance via sharing), and D describes Mediator (centralized coordination).

Multiple choice technology programming languages
  1. When you need classes to be notified of events but you don't know which classes or if you will need to add more at a later date.

  2. When the instances of your class can be used interchangeably and you want to reduce the number of instances created in order to improve performance.

  3. When the instances of your class cannot be used interchangeable and you need to convert them so that they are interchangeable

  4. When the instances of your class can be use interchangeable and you want to reduce the number of instances created in order to improve performance

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

Flyweight reduces memory by sharing intrinsic state among many objects. Objects must be interchangeable (same behavior) so instances can be shared freely. When instances differ in behavior, they can't be shared - Flyweight doesn't apply. Option B captures both conditions: interchangeability + performance via sharing.

Multiple choice technology programming languages
  1. When you need to co-ordinate state changes between other objects by using one object.

  2. When you need to add functionality to a class without changing its interface.

  3. When you need create a separation between abstractions and classes that implement those abstractions.

  4. You need a class that will be used in lots of different applications where the logic will only change slightly.

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

Mediator centralizes communication between objects, coordinating state changes through ONE object. Option A directly describes this: coordinating state changes using a central mediator. Adapter converts interfaces, Bridge separates abstraction from implementation, and Template Method varies algorithm steps - none focus on coordination.