Computer Knowledge
Object-Oriented Programming
2,686 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
-
Use the setXXX() method defined for the wrapper class.
-
Use the parseXXX() method defined for the wrapper class.
-
Use the equals() method defined for the wrapper class.
-
The value encapsulated by a wrapper class is immutable.
D
Correct answer
Explanation
Java wrapper classes are immutable, meaning their values cannot be changed after instantiation. Methods like setXXX() do not exist on wrapper classes. To work with a different value, you must create a new wrapper instance. This design ensures thread safety and allows for caching of frequently used values.
-
-
– variable 1 : String
-
variable1 : String
-
None of the above.
C
Correct answer
Explanation
In UML class diagrams, the # symbol specifically denotes protected visibility for attributes and operations. Public attributes use + and private attributes use -. The protected visibility allows access within the same package and to subclasses.
-
Inheritance.
-
Encapsulation.
-
Overloading.
-
Yielding.
-
SessionFactory
-
Session
-
Transaction
-
None of the Above.
A
Correct answer
Explanation
SessionFactory is a thread-safe, immutable object that is created once during application initialization and shared across all threads. It is heavyweight and maintains the configuration, mappings, and connection pool. Session objects are not thread-safe and should be created per thread/request.
-
decouple an abstraction from its implementation so that the two can vary independently
-
One class takes in another class, both of which extend the same abstract class, and adds functionality.
-
Assembles group of objects with same signature
-
One class controls the creation of and access to objects in another class.
B
Correct answer
Explanation
The Decorator pattern attaches additional responsibilities to an object dynamically. It wraps a component (both implement the same interface) to add behavior without modifying the original class. Option A describes Bridge pattern (decoupling abstraction from implementation), Option C describes Composite pattern (tree structures of objects), and Option D describes Facade pattern (simplified interface to a complex subsystem).
-
The reusable and variable parts of a class are broken into two classes to save resources
-
One class controls the creation of and access to objects in another class.
-
One class takes in another class, both of which extend the same abstract class, and adds functionality.
-
One class has a method that performs a complex process calling several other classes.
D
Correct answer
Explanation
The Facade pattern provides a simplified interface to a complex subsystem. One class (the facade) offers a unified interface that delegates to multiple underlying classes, hiding their complexity. Option A describes Bridge pattern, Option C describes Decorator pattern, and Option B is too vague but could describe Proxy or Factory patterns.
-
Adapter, Bridge and Decorator.
-
Singleton, Builder and Factory Method.
-
Facade, Flyweight and Proxy.
-
Iterator, Observer and Template.
B
Correct answer
Explanation
Creational patterns deal with object creation mechanisms. Singleton ensures only one instance exists, Builder constructs complex objects step-by-step, and Factory Method lets subclasses decide which class to instantiate. The other options list Structural patterns (A), Structural patterns (C), and Behavioral patterns (D).
-
Façade Pattern.
-
Decorator Pattern.
-
Proxy Pattern.
-
Adapter Pattern.
C
Correct answer
Explanation
A Proxy pattern acts as a surrogate or placeholder for another object to control access to it. Proxies typically implement the same interface as the real subject, allowing them to be used interchangeably. They can add lazy initialization, access control, or logging without changing the real subject's code.
-
Strategy Pattern.
-
Template Pattern.
-
Observer Pattern.
-
None of the above.
B
Correct answer
Explanation
The Template Method pattern defines the skeleton of an algorithm in a base class but defers some steps to subclasses. This allows the invariant parts (unchanging steps) to be implemented once in the base class while letting subclasses customize the variable parts. Strategy pattern defines a family of algorithms, and Observer pattern defines a subscription mechanism.
-
Composite Pattern
-
Strategy Pattern
-
Observer Pattern
-
Factory Pattern
A
Correct answer
Explanation
The Composite pattern allows you to compose objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions uniformly. 'Recursive composition' refers to composites that can contain other composites (nested structures), building complex objects from simpler ones using the same interface at every level.
-
Façade Pattern.
-
Adapter Pattern
-
Strategy Pattern.
-
Factory pattern.
C
Correct answer
Explanation
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. The algorithm can vary independently from the clients that use it, and different strategies can be selected at runtime. This is exactly 'dynamic selection of algorithm implementations'. Facade and Adapter are structural patterns concerned with interfaces, not algorithm selection.
-
Singleton Pattern
-
Façade Pattern.
-
Factory Pattern.
-
Adapter Pattern.
C
Correct answer
Explanation
The Factory Pattern is specifically designed to defer object instantiation to subclasses or factory methods, allowing the decision of which compatible class to instantiate to be made at runtime based on conditions or configuration. Singleton ensures only one instance exists, Facade provides a simplified interface to a complex subsystem, and Adapter makes incompatible interfaces work together - none of these primarily address runtime instantiation decisions.
-
private, final.
-
protected, static
-
public, static
-
public, final.
C
Correct answer
Explanation
For a Singleton pattern, the getInstance() method must be static to be accessible without an object reference, and typically public to allow access. However, the code shown creates a new Connection() on every call, which violates Singleton principles - it should return the same single instance. A proper Singleton needs a private constructor and a static field holding the single instance.
-
Command
-
Interpreter
-
Mediator.
-
Iterator.
D
Correct answer
Explanation
The Iterator Pattern provides a way to access elements of an aggregate object sequentially without exposing its underlying representation. It decouples traversal from the aggregate structure. Command encapsulates requests, Interpreter handles grammar evaluation, and Mediator centralizes communication - none provide sequential traversal like Iterator.
-
Use the setXXX() method defined for the wrapper class.
-
Use the parseXXX() method defined for the wrapper class.
-
Use the equals() method defined for the wrapper class.
-
The value encapsulated by a wrapper class is immutable.
D
Correct answer
Explanation
Wrapper classes in Java (Integer, Double, etc.) are immutable - once created, their value cannot be changed. Methods like setXXX() don't exist on wrapper classes. To 'change' a value, you must create a new wrapper object with the new value. This is a fundamental design choice in Java's type system.