B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering Object Oriented Programming 4 — Questions and Answers
Question 1: In the Observer design pattern, what role does the 'Subject' play?
- It stores a list of observers and notifies them of state changes (Correct answer)
- It defines the interface for all concrete observers
- It acts as an intermediary between observers to reduce coupling
- It creates new observer instances on demand
Correct answer: It stores a list of observers and notifies them of state changes
The Subject (or Publisher) maintains references to its observers and broadcasts notifications whenever its state changes.
Question 2: What is the difference between aggregation and composition in OOP?
- Aggregation implies inheritance; composition implies interface implementation
- In composition the child cannot exist independently; in aggregation it can (Correct answer)
- Composition uses interfaces; aggregation uses abstract classes
- Aggregation is compile-time; composition is runtime
Correct answer: In composition the child cannot exist independently; in aggregation it can
Composition is a strong ownership relationship where the part's lifetime depends on the whole, while aggregation is a weaker association where parts can exist independently.
Question 3: What does the Liskov Substitution Principle (LSP) require?
- All methods of a subclass must be marked final
- Objects of a subclass should be replaceable for objects of the parent class without breaking correctness (Correct answer)
- Subclasses must not add new public methods
- Every class must implement at least one interface
Correct answer: Objects of a subclass should be replaceable for objects of the parent class without breaking correctness
LSP states that wherever a base type is expected, a subtype can be used without altering the correctness of the program.
Question 4: Which keyword in C++ ensures a method cannot be overridden in derived classes?
- static
- const
- final (Correct answer)
- override
Correct answer: final
In C++11 and later, the 'final' specifier on a virtual function prevents any further derived class from overriding that function.
Question 5: What is a pure virtual function in C++?
- A function with no parameters
- A virtual function with no body, declared with = 0 (Correct answer)
- A static member function defined in a base class
- A function that returns void
Correct answer: A virtual function with no body, declared with = 0
A pure virtual function (= 0) makes its class abstract; derived classes must provide an implementation unless they too are abstract.
Question 6: What is the purpose of the Factory Method design pattern?
- To ensure only one instance of a class is created
- To define an interface for creating objects but let subclasses decide which class to instantiate (Correct answer)
- To wrap a set of interfaces into a single simplified interface
- To convert the interface of a class into another interface clients expect
Correct answer: To define an interface for creating objects but let subclasses decide which class to instantiate
The Factory Method pattern delegates the instantiation decision to subclasses, promoting loose coupling between the creator and the created objects.
Question 7: In Java, what happens if a subclass does not override an abstract method from its abstract parent?
- The method is inherited with a default empty implementation
- The subclass must also be declared abstract (Correct answer)
- A runtime NullPointerException occurs when the method is called
- The compiler silently ignores the unimplemented method
Correct answer: The subclass must also be declared abstract
If a concrete subclass fails to implement all inherited abstract methods, the compiler requires the subclass itself to be declared abstract.
In the Observer design pattern, what role does the 'Subject' play?