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

  2. Factory Method

  3. Builder

  4. Composite

  5. Recursive Builder

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

Composite composes objects into tree structures, treating individual and group objects uniformly. Recursive access to nested structures built from other objects is the signature use case. Builder constructs objects stepwise, while Composite accesses them hierarchically.

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.

Multiple choice technology programming languages
  1. Composite

  2. Facade

  3. Decorator

  4. Adapter

  5. Mediator

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

The Facade pattern provides a simplified interface to a complex subsystem, exactly as described in the scenario. It hides the complexities of secure programming behind a straightforward framework, allowing other programmers to use security features without understanding intricate implementation details. The Facade pattern acts as a front-facing interface that masks more complex underlying structures. Other patterns like Adapter, Decorator, and Composite serve different purposes - converting interfaces, adding responsibilities dynamically, and composing tree structures respectively.

Multiple choice technology programming languages
  1. The factory method makes objects that should be used together. This is not the case for the abstract factory.

  2. The abstract factory pattern provides an interface for creating a family of objects whereas factory method provides an interface for creating one object.

  3. In the abstract factory pattern the objects that the factory makes are to be used together. This is not necessarily true in the factory method pattern.

  4. The factory method pattern is used when the class does not know the class of the object it must create. But in the abstract factory this is known in advance.

  5. The factory method and abstract factory are essentially the same pattern but two different names are used to describe them depending on the circumstances when they are implemented.

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

The Abstract Factory pattern creates families of related or dependent objects through a factory interface, while Factory Method creates a single object through a method. Abstract Factory is best when products need to be compatible within a family. Factory Method defers object creation to subclasses. Option B correctly captures this distinction - Abstract Factory handles multiple related objects, Factory Method handles one object at a time. Other options either reverse the definitions or incorrectly claim the patterns are essentially the same.

Multiple choice technology programming languages
  1. Factory Method

  2. Prototype

  3. Builder

  4. Abstract Factory

  5. Singleton

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

The Abstract Factory pattern is specifically designed for creating families of related or dependent objects without specifying their concrete classes. When you need multiple objects that work together as a family, Abstract Factory provides a consistent interface for creating them. Factory Method creates single objects, Prototype clones existing objects, Builder constructs complex objects step-by-step, and Singleton ensures only one instance exists - none of these handle families of dependent objects.

Multiple choice technology programming languages
  1. Exception

  2. I am in Q I am in Q with Argument

  3. I am in Q

  4. Compiler Error

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

Java constructors must call either this() or super() as the first statement, but not both. The code S() { this(10); super(); } attempts to call both this(10) and super() in the same constructor, which is illegal. The compiler rejects this because super() is automatically inserted by the compiler if no this() or super() call exists, but if this() is present, the compiler won't insert super(). Additionally, the code references classes R and S but TestConstructor4 creates Q, and uses class Q without declaring it - multiple compilation errors exist.

Multiple choice technology programming languages
  1. I am in U

  2. Compiler Error

  3. I am in V

  4. I am in U I am in V

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

When V() is instantiated, the default constructor of V calls super() implicitly, which invokes U's constructor. U's constructor prints "I am in U". The method void V() in class V is NOT a constructor - it has a return type (void), so it's just a regular method that never gets called during object creation. Only U's constructor executes. This is a common trick question testing whether you recognize that constructors cannot have return types.

Multiple choice technology programming languages
  1. I am in A I am in B I am in C

  2. I am in B I am in C

  3. I am in C I am in B

  4. I am in C

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

When C c = new C() executes, Java's constructor chaining rules apply. C() implicitly calls super() which invokes B(), which implicitly calls super() to invoke A(). Each constructor prints its message: A prints "I am in A", then B prints "I am in B", then C prints "I am in C". The output is the concatenation of all three messages in order: "I am in A I am in B I am in C". This demonstrates how Java ensures parent constructors complete before child constructors execute.

Multiple choice technology programming languages
  1. I am in P null I am in Q

  2. Compiler Error

  3. I am in P I am in Q

  4. I am in Q

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

Class Q extends P, and P has only a parameterized constructor P(String a). When Q() is called, it needs to call super() implicitly, but class P has no no-argument constructor. The compiler error occurs because the default constructor in Q attempts to call a nonexistent no-argument constructor in P. To fix this, Q() must explicitly call super(String) with an argument, or P needs to provide a no-argument constructor. This tests understanding that constructors aren't inherited and that subclass constructors must match available parent constructors.

Multiple choice technology programming languages
  1. I am in M, No argument Constructor I am in M, Constructor with Argument

  2. I am in M, Constructor with Argument I am in M, No argument Constructor

  3. Compiler Error

  4. Exception

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

The constructor M() attempts to call this(10) after already executing System.out.println(). In Java, this() and super() calls must be the first statement in a constructor. This code violates that rule by having the print statement before this(10). The compiler error occurs because constructor chaining calls must precede any other code in the constructor body. Additionally, this(10) and this() cannot be used in the same constructor as they would create circular initialization.

Multiple choice technology programming languages
  1. protected

  2. private

  3. public

  4. friendly

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

To answer this question, the user needs to know the concept of access modifiers in object-oriented programming. Access modifiers are keywords that determine the accessibility of classes, fields, methods, and other members of a class from other classes and packages. A top-level class is a class that is not a nested class or an inner class.

Now, let's go through each option and explain whether it is right or wrong:

A. protected: A protected access modifier can be applied to class members but not to top-level classes. Protected members can be accessed from within the class itself, its subclasses, and classes in the same package.

B. private: A private access modifier can be applied to class members but not to top-level classes. Private members can only be accessed from within the class itself.

C. public: A public access modifier can be applied to top-level classes, as well as class members. Public members can be accessed from anywhere in the program.

D. friendly: There is no such access modifier as "friendly". It may be a typo or a misunderstanding of the default access modifier, which is applied when no access modifier is specified. The default access modifier allows access to members from within the same package.

Therefore, the correct answer is:

The Answer is: C

Multiple choice technology programming languages
  1. EmpId is 100

  2. Compiler error

  3. EmpId is 0

  4. Exception

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

The program fails to compile because TestInheritance1 is package-private and cannot be imported outside its package. Additionally, the non-static instance variable 'empId' is accessed directly inside the static main method without an object instance.