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
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.
Which design pattern is commonly used to dynamically add or remove responsibilities from an object?
-
Facade
-
Composite
-
Decorator
-
Proxy
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.
Which design pattern provides a way to define a family of algorithms, encapsulate them, and make them interchangeable?
-
Factory Method
-
Strategy
-
Singleton
-
Decorator
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.
Which design pattern allows you to add new functionality to an existing class without modifying its source code?
-
Factory Method
-
Strategy
-
Decorator
-
Adapter
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.
Which design pattern provides a way to create objects without specifying the exact class to instantiate?
-
Factory Method
-
Strategy
-
Singleton
-
Builder
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.
Which design pattern allows you to create a single instance of a class and ensure that it is accessed globally?
-
Factory Method
-
Strategy
-
Singleton
-
Builder
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.
Which design pattern allows you to create complex objects step-by-step, using a series of smaller, simpler steps?
-
Factory Method
-
Strategy
-
Builder
-
Adapter
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.