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
What is the purpose of the Builder design pattern?
-
To define a family of related classes and provide a way to create objects without specifying their exact class.
-
To convert the interface of a class into another interface clients expect.
-
To provide a way to add or remove objects from a collection dynamically.
-
To define an interface for creating objects, but let subclasses decide which class to instantiate.
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.
Which design pattern is used to provide a way to create objects without specifying their exact class?
-
Factory Method
-
Abstract Factory
-
Builder
-
Prototype
D
Correct answer
Explanation
The Prototype pattern lets you create new objects by copying an existing object.
What is the purpose of the Decorator design pattern?
-
To attach additional responsibilities to an object dynamically.
-
To convert the interface of a class into another interface clients expect.
-
To provide a way to add or remove objects from a collection dynamically.
-
To define an interface for creating objects, but let subclasses decide which class to instantiate.
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.
What is the purpose of the Memento design pattern?
-
To provide a way to undo or redo actions.
-
To convert the interface of a class into another interface clients expect.
-
To provide a way to add or remove objects from a collection dynamically.
-
To define an interface for creating objects, but let subclasses decide which class to instantiate.
A
Correct answer
Explanation
The Memento pattern provides a way to save and restore the state of an object without violating encapsulation.
Which design pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation?
-
Iterator
-
Composite
-
Observer
-
Singleton
A
Correct answer
Explanation
The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
What is the purpose of the Composite design pattern?
-
To provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
-
To convert the interface of a class into another interface clients expect.
-
To provide a way to add or remove objects from a collection dynamically.
-
To define an interface for creating objects, but let subclasses decide which class to instantiate.
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.
Which design pattern is used to provide a way to define a family of related classes and create objects without specifying their exact class?
-
Factory Method
-
Abstract Factory
-
Builder
-
Prototype
B
Correct answer
Explanation
The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes.
What is the primary goal of the Open-Closed Principle?
-
Extensibility without modification
-
Encapsulation of data and behavior
-
Separation of concerns
-
Minimizing dependencies between modules
A
Correct answer
Explanation
The Open-Closed Principle aims to design software that can be extended with new features without modifying existing code.
Which design principle ensures that derived classes can be substituted for their base classes without breaking the program?
-
Liskov Substitution Principle
-
Interface Segregation Principle
-
Dependency Inversion Principle
-
Composition over Inheritance
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.
What is the purpose of the Interface Segregation Principle?
-
Defining a clear and concise interface for clients
-
Reducing the number of dependencies between modules
-
Promoting loose coupling and high cohesion
-
Encouraging the use of inheritance for code reuse
B
Correct answer
Explanation
The Interface Segregation Principle aims to minimize the number of dependencies between modules by defining smaller, more specific interfaces.
Which design principle advocates for using composition over inheritance to achieve code reuse?
-
Single Responsibility Principle
-
Open-Closed Principle
-
Liskov Substitution Principle
-
Composition over Inheritance
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.
Which design pattern is commonly used to define a common interface for a set of related classes?
-
Abstract Factory
-
Builder
-
Prototype
-
Strategy
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.
Which design pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable?
-
Abstract Factory
-
Builder
-
Prototype
-
Strategy
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.
Which design pattern is commonly used to create a lightweight copy of an object?
-
Abstract Factory
-
Builder
-
Prototype
-
Strategy
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.
Which design pattern is commonly used to provide a unified interface to a set of interfaces in a subsystem?
-
Facade
-
Composite
-
Decorator
-
Proxy
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.