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 of the following is NOT a common section found in API documentation?
-
Introduction
-
Function reference
-
Class reference
-
Troubleshooting guide
D
Correct answer
Explanation
A troubleshooting guide is not typically a common section found in API documentation. It is more commonly found in user manuals or technical documentation, where it provides tips and solutions for common issues and problems.
What is an initial object in a category?
-
An object with a unique morphism from every other object
-
An object with a unique morphism to every other object
-
An object with no incoming morphisms
-
An object with no outgoing morphisms
A
Correct answer
Explanation
An initial object in a category is an object that has a unique morphism from every other object in the category, representing a universal starting point.
What is a terminal object in a category?
-
An object with a unique morphism from every other object
-
An object with a unique morphism to every other object
-
An object with no incoming morphisms
-
An object with no outgoing morphisms
B
Correct answer
Explanation
A terminal object in a category is an object that has a unique morphism to every other object in the category, representing a universal ending point.
Which design pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable?
-
Factory Method
-
Strategy
-
Observer
-
Singleton
B
Correct answer
Explanation
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. This lets the algorithm vary independently from clients that use it.
What is the purpose of the Observer design pattern?
-
To define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
-
To create a single instance of a class and provide a global point of access to that instance.
-
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 Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Which design pattern is used to create a single instance of a class and provide a global point of access to that instance?
-
Factory Method
-
Strategy
-
Observer
-
Singleton
D
Correct answer
Explanation
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.
What is the purpose of the Factory Method design pattern?
-
To define a family of algorithms, encapsulate each one, and make them interchangeable.
-
To create a single instance of a class and provide a global point of access to that instance.
-
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.
D
Correct answer
Explanation
The Factory Method pattern defines an interface for creating objects, but lets subclasses decide which class to instantiate.
What is the purpose of the Adapter design pattern?
-
To convert the interface of a class into another interface clients expect.
-
To define a family of algorithms, encapsulate each one, and make them interchangeable.
-
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 Adapter pattern converts the interface of a class into another interface clients expect. This lets classes work together that couldn't otherwise because of incompatible interfaces.
Which design pattern is used to define a family of related classes and provide a way to 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 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.