B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering Object Oriented Programming 3 — Questions and Answers
Question 1: What problem does the 'diamond problem' describe in OOP?
- Memory leak caused by circular references
- Ambiguity when a class inherits from two classes that share a common ancestor (Correct answer)
- Stack overflow from deep recursion in constructors
- Name collision between overloaded methods
Correct answer: Ambiguity when a class inherits from two classes that share a common ancestor
The diamond problem arises in multiple inheritance when two parent classes derive from the same base, creating ambiguity about which version of a member to inherit.
Question 2: Which access modifier in Java makes a member accessible only within its own package and by subclasses?
- private
- public
- protected (Correct answer)
- default (package-private)
Correct answer: protected
The 'protected' modifier allows access within the same package and by subclasses in any package, making it broader than private but narrower than public.
Question 3: What is method overloading?
- Redefining a parent class method in a subclass with the same signature
- Defining multiple methods in the same class with the same name but different parameter lists (Correct answer)
- Calling a method through an interface reference
- Hiding a static parent method with a static child method
Correct answer: Defining multiple methods in the same class with the same name but different parameter lists
Overloading is compile-time polymorphism where the compiler selects the correct method based on the number or types of arguments provided.
Question 4: In the context of design patterns, what category does the Singleton pattern belong to?
- Structural
- Behavioral
- Creational (Correct answer)
- Architectural
Correct answer: Creational
Singleton is a Creational pattern because it controls the creation process, ensuring only one instance of a class exists.
Question 5: What does 'composition over inheritance' mean as a design guideline?
- Always use interfaces instead of classes
- Prefer building complex behavior by combining objects rather than extending class hierarchies (Correct answer)
- Avoid using constructors and use factory methods instead
- Limit classes to at most one parent class
Correct answer: Prefer building complex behavior by combining objects rather than extending class hierarchies
Composition over inheritance advocates assembling behavior from loosely coupled components, yielding greater flexibility and avoiding brittle class hierarchies.
Question 6: Which statement about interfaces vs. abstract classes in Java is correct?
- A class can extend multiple abstract classes but only one interface
- Interfaces can contain instance variables; abstract classes cannot
- A class can implement multiple interfaces but can only extend one abstract class (Correct answer)
- Abstract classes require all methods to be abstract
Correct answer: A class can implement multiple interfaces but can only extend one abstract class
Java supports single class inheritance but allows a class to implement multiple interfaces, providing a form of multiple inheritance for type contracts.
Question 7: What is the role of a destructor in OOP?
- To initialize object state when created
- To copy one object's state to another
- To release resources held by an object when it is destroyed (Correct answer)
- To serialize an object to persistent storage
Correct answer: To release resources held by an object when it is destroyed
A destructor (or finalizer) is invoked when an object's lifetime ends, allowing cleanup of resources such as file handles, network connections, or allocated memory.
What problem does the 'diamond problem' describe in OOP?