B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering Object Oriented Programming 2 — Questions and Answers
Question 1: Which design principle states that a class should have only one reason to change?
- Open/Closed Principle
- Single Responsibility Principle (Correct answer)
- Liskov Substitution Principle
- Dependency Inversion Principle
Correct answer: Single Responsibility Principle
The Single Responsibility Principle (SRP) dictates that each class should encapsulate only one concern so changes to one responsibility don't affect others.
Question 2: In Java, what does the 'super' keyword do when used in a constructor?
- Creates a new superclass instance
- Calls the parent class constructor (Correct answer)
- Declares a superclass reference
- Overrides the parent method
Correct answer: Calls the parent class constructor
'super()' in a constructor explicitly invokes the parent class's constructor and must be the first statement in the subclass constructor.
Question 3: Which UML relationship represents an 'is-a' association between classes?
- Aggregation
- Composition
- Generalization (Correct answer)
- Dependency
Correct answer: Generalization
Generalization (shown as a solid line with a hollow arrowhead) models inheritance, expressing that a subclass 'is-a' type of the superclass.
Question 4: What is the output of calling a virtual method through a base class pointer pointing to a derived object in C++?
- Base class version is called
- Derived class version is called (Correct answer)
- Compilation error occurs
- Runtime exception is thrown
Correct answer: Derived class version is called
Virtual dispatch ensures the most-derived override is called at runtime, which is the core mechanism of polymorphism in C++.
Question 5: What is the purpose of an abstract class in OOP?
- To prevent any instantiation and force subclasses to implement certain methods (Correct answer)
- To allow multiple inheritance without ambiguity
- To restrict access to private members
- To define static utility methods
Correct answer: To prevent any instantiation and force subclasses to implement certain methods
Abstract classes serve as templates; they may contain abstract methods that concrete subclasses must implement, and cannot be instantiated directly.
Question 6: Which concept allows different classes to respond to the same message in different ways?
- Encapsulation
- Polymorphism (Correct answer)
- Abstraction
- Aggregation
Correct answer: Polymorphism
Polymorphism lets objects of different types be treated uniformly through a common interface while each class provides its own specific implementation.
Question 7: In Python, what is a 'dunder' or magic method?
- A method defined inside an inner class
- A method decorated with @staticmethod
- A method whose name starts and ends with double underscores (Correct answer)
- A private method prefixed with a single underscore
Correct answer: A method whose name starts and ends with double underscores
Dunder methods like __init__, __str__, and __len__ have special meaning to Python and are invoked automatically in specific contexts.
Which design principle states that a class should have only one reason to change?