B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering Object Oriented Programming 5 — Questions and Answers
Question 1: What is the primary benefit of using interfaces to program against rather than concrete classes?
- It allows use of multiple constructors
- It enables coding to a contract, reducing coupling to specific implementations (Correct answer)
- It automatically provides thread safety
- It eliminates the need for exception handling
Correct answer: It enables coding to a contract, reducing coupling to specific implementations
Programming to an interface decouples client code from concrete implementations, making it easier to swap, mock, or extend without changing the client.
Question 2: In Python, how is multiple inheritance resolved when two parent classes define the same method?
- A runtime TypeError is raised
- Python uses the Method Resolution Order (MRO) following the C3 linearization algorithm (Correct answer)
- The method from the last listed parent class is always used
- Python requires explicit disambiguation via super()
Correct answer: Python uses the Method Resolution Order (MRO) following the C3 linearization algorithm
Python's MRO (viewable via ClassName.__mro__) uses C3 linearization to determine a consistent method lookup order across multiple base classes.
Question 3: What is the difference between shallow copy and deep copy of an object?
- Shallow copy copies only primitive fields; deep copy copies all fields including objects (Correct answer)
- Shallow copy duplicates the object and all referenced objects recursively; deep copy only copies the top-level object
- Shallow copy is performed by the compiler; deep copy requires JVM intervention
- Shallow copy creates a new reference to the same object; deep copy has no practical use
Correct answer: Shallow copy copies only primitive fields; deep copy copies all fields including objects
A shallow copy copies primitive values and object references (not the referenced objects), while a deep copy recursively duplicates all referenced objects as well.
Question 4: Which SOLID principle is violated when a module depends directly on a concrete implementation rather than an abstraction?
- Single Responsibility Principle
- Open/Closed Principle
- Interface Segregation Principle
- Dependency Inversion Principle (Correct answer)
Correct answer: Dependency Inversion Principle
The Dependency Inversion Principle states high-level modules should depend on abstractions, not concrete low-level modules.
Question 5: What is covariant return type in the context of method overriding?
- The overriding method returns a supertype of the original return type
- The overriding method returns a subtype of the original return type (Correct answer)
- The return type must match exactly with no variance allowed
- The method return type changes based on input parameters
Correct answer: The overriding method returns a subtype of the original return type
Covariant return types allow an overriding method to return a more specific (derived) type than declared in the parent, supported in Java since version 5.
Question 6: In OOP, what is the role of the 'this' (or 'self') keyword?
- It refers to the class itself as a static context
- It refers to the current object instance within an instance method (Correct answer)
- It is used to call the superclass constructor
- It holds a reference to the most recently created object
Correct answer: It refers to the current object instance within an instance method
'this' (Java/C++) or 'self' (Python) is an implicit reference to the calling object, used to disambiguate instance variables from local variables and to pass the object itself as an argument.
Question 7: What distinguishes the Decorator design pattern from simple inheritance?
- Decorator modifies class structure at compile time; inheritance does so at runtime
- Decorator attaches responsibilities to individual objects dynamically without affecting other instances of the same class (Correct answer)
- Decorator requires multiple inheritance; standard inheritance does not
- Decorator can only be applied to abstract classes
Correct answer: Decorator attaches responsibilities to individual objects dynamically without affecting other instances of the same class
The Decorator pattern wraps individual object instances to add behavior at runtime, avoiding the class explosion that arises from using inheritance to cover every combination of features.
What is the primary benefit of using interfaces to program against rather than concrete classes?