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 technology architecture
  1. Prototype

  2. Factory method

  3. Abstract factory

  4. Proxy

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

The Prototype pattern specifies object creation by cloning a prototypical instance instead of creating new instances from scratch. It's useful when creating a new object is expensive or complex, and you want to create new objects by copying this prototype and then modifying as needed.

Multiple choice technology architecture
  1. Decorator

  2. Observer

  3. State

  4. Strategy

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

The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Decorator

  2. Observer

  3. State

  4. Strategy

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

The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Meditator

  2. Template method

  3. Adaptor

  4. Bridge

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

The Adapter pattern converts the interface of a class into another interface clients expect. Option A should be 'Mediator' not 'Meditator' (typo). Template method defines skeleton of algorithm, Bridge separates abstraction from implementation. Adaptor (Adapter) is correct.

Multiple choice technology architecture
  1. Mediator

  2. Memento

  3. Strategy

  4. Decorator

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

The Decorator pattern attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality. Mediator defines how objects interact, Memento captures/restores state, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Prototype

  2. Builder

  3. Template method

  4. Momento

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

The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. Prototype clones objects, Template method defines algorithm skeleton, and Momento (Memento) captures/restores state.

Multiple choice technology architecture
  1. Visitor

  2. Momento

  3. Mediator

  4. Bridge

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

The Mediator pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly and letting you vary their interaction independently. Visitor separates operations from object structure, Momento captures state, and Bridge separates abstraction from implementation.

Multiple choice technology
  1. return super.hashCode();

  2. return name.hashCode() + age * 7;

  3. return name.hashCode() + comment.hashCode() / 2;

  4. return name.hashCode() + comment.hashCode() / 2 - age * 3;

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

The hashCode method must use the same fields as equals() to maintain the hashCode contract. The equals method compares age and name, so option B correctly uses only those fields. Options C and D incorrectly include 'comment' which is not part of equals comparison, violating the contract.

Multiple choice technology web technology
  1. True

  2. False

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

Abstract methods cannot be static because abstract methods are designed to be overridden in subclasses, while static methods belong to the class itself and are not inherited for overriding. Making a method both abstract and static would be a contradiction in terms.

Multiple choice technology web technology
  1. True

  2. False

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

Only objects of classes that implement the Cloneable interface can be cloned using the clone() method. Attempting to clone an object that does not implement this interface throws a CloneNotSupportedException.