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
A
Correct answer
Explanation
A constructor can be declared as private. This is a common design pattern in Java, frequently used to prevent instantiation or to implement the Singleton pattern.
A
Correct answer
Explanation
Overloaded methods are completely independent methods that share the same name but have different parameter lists. They are free to have different access modifiers.
-
Prototype
-
Factory method
-
Abstract factory
-
Proxy
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.
-
Decorator
-
Observer
-
State
-
Strategy
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.
-
Decorator
-
Observer
-
State
-
Strategy
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.
-
Meditator
-
Template method
-
Adaptor
-
Bridge
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.
-
Mediator
-
Memento
-
Strategy
-
Decorator
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.
-
Prototype
-
Builder
-
Template method
-
Momento
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.
-
Visitor
-
Momento
-
Mediator
-
Bridge
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.
-
return super.hashCode();
-
return name.hashCode() + age * 7;
-
return name.hashCode() + comment.hashCode() / 2;
-
return name.hashCode() + comment.hashCode() / 2 - age * 3;
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.
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.
A
Correct answer
Explanation
In Java, the Hashtable class extends the abstract class Dictionary. Although Dictionary is obsolete, it remains the direct superclass of Hashtable.
A
Correct answer
Explanation
The java.lang package is automatically and implicitly imported by the Java compiler into every compilation unit, meaning classes like String or System do not require explicit import statements.
A
Correct answer
Explanation
Marker interfaces are interfaces with no methods that serve to mark a class for a specific purpose. Serializable, Cloneable, and Remote are classic examples - they tell the JVM or framework to treat the implementing class specially (e.g., make it serializable).
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.