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
-
public
-
private
-
class
-
All of the above.
C
Correct answer
Explanation
To declare a class in Java and similar languages, the 'class' keyword is required. Public and private are access modifiers that come before 'class', not the keyword itself. 'All of the above' is incorrect because only 'class' is the required keyword for declaration.
-
The program has a compilation error because TempClass does not have a default constructor.
-
The program has a compilation error because TempClass does not have a constructor with an int argument.
-
The program compiles fine, but it does not run because class C is not public.
-
The program compiles and runs fine.
B
Correct answer
Explanation
In Java, constructors cannot have a return type. Because TempClass(int j) is declared with a void return type, it is treated as a regular method rather than a constructor. Consequently, the class has no parameterized constructor, and calling new TempClass(2) in main results in a compilation error.
-
public variables
-
private variables
-
instance variables
-
class variables
D
Correct answer
Explanation
Class variables (also called static variables) are shared across all instances of a class - they belong to the class itself, not to individual objects. Instance variables are unique to each object. Public and private are access modifiers, not variable types.
-
in line 4
-
in line 8
-
in both line 4 and line 8
-
none
A
Correct answer
Explanation
The square method in line 4 only depends on its parameter n and doesn't access any instance variables of the class, making it a perfect candidate for a static utility method. Conversely, getAge in line 8 returns the instance field age, meaning it requires an object instance and cannot be declared static.
-
Singleton
-
Factory
-
Builder
-
Prototype
-
Factory
-
Abstract Factory
-
Singleton
-
Builder
-
Intercepting Filter
-
Factory pattern
-
Invoker pattern
-
Double Dispatch pattern
-
None of the above
D
Correct answer
Explanation
The Visitor pattern is a behavioral design pattern that allows you to add new operations to existing object structures without modifying the structures. It uses a technique called double dispatch, where the operation executed depends on both the type of visitor and the type of element being visited. The other options (Intercepting Filter, Factory, Invoker) are different design patterns altogether.
-
Singleton pattern
-
One Instance pattern
-
No such class be formed
-
Static classes are one instance classess
-
None of the above
A
Correct answer
Explanation
The Singleton pattern ensures a class has only one instance and provides a global point of access to that instance. It involves a private constructor, a static variable to hold the single instance, and a static method that returns this instance (creating it if necessary). 'One Instance' and 'Static classes' are not recognized design patterns for this purpose.
-
Observer pattern
-
DataAccess pattern
-
Publisher Pattern
-
Subscriber Pattern
-
None of the above
A
Correct answer
Explanation
The Observer pattern defines a one-to-many dependency between objects so that when one object (the subject) changes state, all its dependents (observers) are notified and updated automatically. This is commonly used in event handling systems. 'DataAccess', 'Publisher', and 'Subscriber' are not standard design pattern names for this purpose.
-
Encapsulation
-
Polymorphism
-
Inheritance
-
Data Access object
-
None of the above
D
Correct answer
Explanation
The Data Access Object (DAO) pattern abstracts and encapsulates all access to a data source, separating data persistence logic from business logic. This creates a dedicated layer for database operations, making the code more maintainable and testable. Encapsulation, Polymorphism, and Inheritance are OOP principles, not patterns for data access separation.
-
No pattern could be identified
-
Polymorphism
-
Facade pattern
-
Visitor pattern
-
Observer pattern
-
Facade pattern
-
Specialization pattern
-
Value List Iterator pattern
-
Factory pattern
-
None of the above
D
Correct answer
Explanation
The Factory pattern encapsulates object creation logic and hides implementation details from client code. Clients request objects from a factory without knowing which concrete class was instantiated or how the creation process works. Facade simplifies interfaces, Specialization isn't a standard pattern, and Value List Iterator is for pagination.
-
Inheritance pattern
-
Generalization pattern
-
Specialization pattern
-
Implementation pattern
-
None of the above
A
Correct answer
Explanation
Inheritance allows a derived class to acquire all methods and properties of a base class and can override or modify their behavior. This matches the requirement of having the same interface as another class with the ability to modify operations. While 'Inheritance pattern' isn't a standard GoF name, it describes the fundamental OOP concept. Generalization and Specialization are UML relationships, not standalone patterns.
-
Invoker pattern
-
Double Dispatch pattern
-
Factory pattern
-
Intercepting Filter
-
None of the above
B
Correct answer
Explanation
The Double Dispatch pattern (Visitor pattern family) allows an operation to vary based on both the object type and the operation type. It enables a class to perform different operations on different object types by dispatching twice - once on the object and once on the visitor. This is ideal when a class must operate on diverse object types with type-specific behavior. Invoker is Command pattern, Factory creates objects, Intercepting Filter modifies requests.