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

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.

Multiple choice

Which design pattern is commonly used to dynamically add or remove responsibilities from an object?

  1. Facade

  2. Composite

  3. Decorator

  4. Proxy

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

The Decorator pattern allows the dynamic addition or removal of responsibilities from an object, providing a flexible way to modify the behavior of an object at runtime.

Multiple choice

Which design pattern provides a way to define a family of algorithms, encapsulate them, and make them interchangeable?

  1. Factory Method

  2. Strategy

  3. Singleton

  4. Decorator

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

The Strategy design pattern allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. This promotes flexibility and code reuse.

Multiple choice

Which design pattern allows you to add new functionality to an existing class without modifying its source code?

  1. Factory Method

  2. Strategy

  3. Decorator

  4. Adapter

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

The Decorator design pattern allows you to add new functionality to an existing class without modifying its source code, providing a flexible way to extend the behavior of objects.

Multiple choice

Which design pattern provides a way to create objects without specifying the exact class to instantiate?

  1. Factory Method

  2. Strategy

  3. Singleton

  4. Builder

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

The Factory Method design pattern provides a way to create objects without specifying the exact class to instantiate, allowing for flexible object creation and decoupling.

Multiple choice

Which design pattern allows you to create a single instance of a class and ensure that it is accessed globally?

  1. Factory Method

  2. Strategy

  3. Singleton

  4. Builder

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

The Singleton design pattern allows you to create a single instance of a class and ensure that it is accessed globally, promoting resource optimization and controlled access.

Multiple choice

Which design pattern allows you to create complex objects step-by-step, using a series of smaller, simpler steps?

  1. Factory Method

  2. Strategy

  3. Builder

  4. Adapter

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

The Builder design pattern allows you to create complex objects step-by-step, using a series of smaller, simpler steps, promoting flexibility and code readability.