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

Multiple choice technology programming languages
  1. The program has a compilation error because TempClass does not have a default constructor.

  2. The program has a compilation error because TempClass does not have a constructor with an int argument.

  3. The program compiles fine, but it does not run because class C is not public.

  4. The program compiles and runs fine.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. public variables

  2. private variables

  3. instance variables

  4. class variables

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. in line 4

  2. in line 8

  3. in both line 4 and line 8

  4. none

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Intercepting Filter

  2. Factory pattern

  3. Invoker pattern

  4. Double Dispatch pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Singleton pattern

  2. One Instance pattern

  3. No such class be formed

  4. Static classes are one instance classess

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Observer pattern

  2. DataAccess pattern

  3. Publisher Pattern

  4. Subscriber Pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Encapsulation

  2. Polymorphism

  3. Inheritance

  4. Data Access object

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Facade pattern

  2. Specialization pattern

  3. Value List Iterator pattern

  4. Factory pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Inheritance pattern

  2. Generalization pattern

  3. Specialization pattern

  4. Implementation pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Invoker pattern

  2. Double Dispatch pattern

  3. Factory pattern

  4. Intercepting Filter

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.