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 of the following is NOT a common section found in API documentation?

  1. Introduction

  2. Function reference

  3. Class reference

  4. Troubleshooting guide

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is an initial object in a category?

  1. An object with a unique morphism from every other object

  2. An object with a unique morphism to every other object

  3. An object with no incoming morphisms

  4. An object with no outgoing morphisms

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is a terminal object in a category?

  1. An object with a unique morphism from every other object

  2. An object with a unique morphism to every other object

  3. An object with no incoming morphisms

  4. An object with no outgoing morphisms

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

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

  1. Factory Method

  2. Strategy

  3. Observer

  4. Singleton

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the purpose of the Observer design pattern?

  1. To define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

  2. To create a single instance of a class and provide a global point of access to that instance.

  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 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.

Multiple choice

Which design pattern is used to create a single instance of a class and provide a global point of access to that instance?

  1. Factory Method

  2. Strategy

  3. Observer

  4. Singleton

Reveal answer Fill a bubble to check yourself
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.

Multiple choice

What is the purpose of the Factory Method design pattern?

  1. To define a family of algorithms, encapsulate each one, and make them interchangeable.

  2. To create a single instance of a class and provide a global point of access to that instance.

  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
D Correct answer
Explanation

The Factory Method pattern defines an interface for creating objects, but lets subclasses decide which class to instantiate.

Multiple choice

What is the purpose of the Adapter design pattern?

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

  2. To define a family of algorithms, encapsulate each one, and make them interchangeable.

  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 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.

Multiple choice

Which design pattern is used to define a family of related classes and 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
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 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.