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

What is the purpose of the Builder design pattern?

  1. To define a family of related classes and provide a way to create objects without specifying their exact class.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.

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

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.

Multiple choice

Which design pattern is used to provide a way to create objects without specifying their exact class?

  1. Factory Method

  2. Abstract Factory

  3. Builder

  4. Prototype

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

The Prototype pattern lets you create new objects by copying an existing object.

Multiple choice

What is the purpose of the Decorator design pattern?

  1. To attach additional responsibilities to an object dynamically.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.

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

The Decorator pattern attaches additional responsibilities to an object dynamically. This lets you extend the functionality of an object without changing its structure.

Multiple choice

What is the purpose of the Memento design pattern?

  1. To provide a way to undo or redo actions.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.

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

The Memento pattern provides a way to save and restore the state of an object without violating encapsulation.

Multiple choice

Which design pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation?

  1. Iterator

  2. Composite

  3. Observer

  4. Singleton

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

The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Multiple choice

What is the purpose of the Composite design pattern?

  1. To provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.

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

The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.

Multiple choice

Which design pattern is used to provide a way to define a family of related classes and create objects without specifying their exact class?

  1. Factory Method

  2. Abstract Factory

  3. Builder

  4. Prototype

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

The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes.

Multiple choice

What is the primary goal of the Open-Closed Principle?

  1. Extensibility without modification

  2. Encapsulation of data and behavior

  3. Separation of concerns

  4. Minimizing dependencies between modules

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

The Open-Closed Principle aims to design software that can be extended with new features without modifying existing code.

Multiple choice

Which design principle ensures that derived classes can be substituted for their base classes without breaking the program?

  1. Liskov Substitution Principle

  2. Interface Segregation Principle

  3. Dependency Inversion Principle

  4. Composition over Inheritance

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

The Liskov Substitution Principle states that subclasses should be substitutable for their base classes without altering the correctness of the program.

Multiple choice

What is the purpose of the Interface Segregation Principle?

  1. Defining a clear and concise interface for clients

  2. Reducing the number of dependencies between modules

  3. Promoting loose coupling and high cohesion

  4. Encouraging the use of inheritance for code reuse

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

The Interface Segregation Principle aims to minimize the number of dependencies between modules by defining smaller, more specific interfaces.

Multiple choice

Which design principle advocates for using composition over inheritance to achieve code reuse?

  1. Single Responsibility Principle

  2. Open-Closed Principle

  3. Liskov Substitution Principle

  4. Composition over Inheritance

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

Composition over Inheritance suggests that instead of inheriting from a base class, objects should be composed from other objects, promoting flexibility and code reuse.

Multiple choice

Which design pattern is commonly used to define a common interface for a set of related classes?

  1. Abstract Factory

  2. Builder

  3. Prototype

  4. Strategy

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

The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes, promoting interchangeability and decoupling.

Multiple choice

Which design pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable?

  1. Abstract Factory

  2. Builder

  3. Prototype

  4. Strategy

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

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing the algorithm to be selected at runtime.

Multiple choice

Which design pattern is commonly used to create a lightweight copy of an object?

  1. Abstract Factory

  2. Builder

  3. Prototype

  4. Strategy

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

The Prototype pattern provides a mechanism to create new objects by copying an existing object, allowing for efficient object creation and reducing the cost of object instantiation.

Multiple choice

Which design pattern is commonly used to provide a unified interface to a set of interfaces in a subsystem?

  1. Facade

  2. Composite

  3. Decorator

  4. Proxy

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

The Facade pattern provides a unified interface to a set of interfaces in a subsystem, simplifying the interaction with the subsystem and promoting loose coupling.