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
-
A pointcut
-
a join Point
-
an Aspect
-
None
C
Correct answer
Explanation
An Aspect is a Java class that encapsulates one or more advices (logic like logging, security, transactions) applied to multiple points in an application. A pointcut defines WHERE advice is applied, and a join point is a specific execution point where advice can be applied. The aspect class contains both pointcuts and advices.
A
Correct answer
Explanation
Java is an object-oriented programming language that fully supports polymorphism (method overloading and overriding) and inheritance (extending classes). These are fundamental OOP concepts in Java.
-
Parent Class
-
Class
-
Child Class
-
None Of Above
A
Correct answer
Explanation
In Java, a superclass is the parent class that is being extended. When class B extends class A, class A is the superclass (parent) and class B is the subclass (child). Superclass provides attributes and methods that subclasses inherit.
-
Instance of Class
-
Static Variable
-
Keyword
-
None Of above
A
Correct answer
Explanation
An object is a concrete instance of a class. When you write 'MyClass obj = new MyClass()', obj is the object/instance and MyClass is the class template. Objects have state (fields) and behavior (methods).
-
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 an object (both implement the same interface) to add behavior without modifying the original class, providing a flexible alternative to subclassing for extending functionality.
-
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 exposes a high-level interface that coordinates multiple underlying classes, making the subsystem easier to use without exposing its complexity.
-
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 defines an interface for creating objects but lets subclasses decide which class to instantiate. Options A and C are structural patterns, while D are behavioral patterns.
-
Decorator Pattern.
-
Façade Pattern.
-
Proxy Pattern.
-
Adapter Pattern.
C
Correct answer
Explanation
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. A proxy typically has the same interface as the real object because it implements a common interface or extends an abstract class, enabling it to act as a direct stand-in.
-
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 method, deferring some steps to subclasses. It implements invariant parts of an algorithm once in the base class while allowing subclasses to override the variable parts. This promotes code reuse and enforces algorithm structure.
-
Composite Pattern
-
Strategy Pattern
-
Observer Pattern
-
Factory Pattern
A
Correct answer
Explanation
The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly through recursive composition, enabling complex objects to be built from simpler ones using the same interface.
-
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. It allows different implementations of an algorithm to be selected dynamically at runtime without changing the client code that uses the strategy, promoting flexibility and separation of concerns.
-
Singleton Pattern
-
Façade Pattern.
-
Factory Pattern.
-
Adapter Pattern.
C
Correct answer
Explanation
Factory Pattern is specifically designed to defer instantiation decisions to subclasses or factory methods, allowing the runtime selection of which compatible class to instantiate without the client knowing the concrete class. Singleton ensures only one instance exists. Facade provides a simplified interface to a complex subsystem. Adapter makes incompatible interfaces work together.
-
private, final.
-
protected, static
-
public, static
-
public, final.
-
Command
-
Interpreter
-
Mediator.
-
Iterator.
D
Correct answer
Explanation
Iterator Pattern provides a way to access elements of an aggregate object sequentially without exposing its underlying representation - exactly what the question describes. Command encapsulates requests. Interpreter evaluates language expressions. Mediator defines centralized communication between objects.