programming
beginner
10 sample questions
Object-oriented Programming (oop) MCQ Practice Test
Defining classes, objects, attributes, and methods.
Inheritance and polymorphism concepts.
Q1. Consider a scenario where you're designing a highly concurrent, distributed system using Java. You need to implement a "Leader Election" algorithm within a cluster of microservices, ensuring only one service acts as the leader at any given time. Which pattern, when leveraged correctly, best minimizes the risk of split-brain scenarios and provides robust leader election while efficiently handling network partitions and failures, and is most scalable for a large number of nodes?
-
A. Using a centralized database with pessimistic locking for leader status.
-
B. Employing a distributed consensus algorithm like Raft or Paxos, using a dedicated library. ✓
-
C. Implementing a simple leader election based on timestamps, with periodic heartbeats.
-
D. Utilizing a distributed hash table (DHT) to assign the leader role based on node ID.
Explanation: Centralized databases are single points of failure. Timestamp-based approaches are vulnerable to clock drift and split-brain. DHTs, while distributed, don't inherently solve the consensus problem which is necessary for robust leader election. Raft or Paxos are explicitly designed to handle network partitions and ensure a single leader is consistently elected even in failure scenarios, thereby minimizing split-brain problems and scaling better for a larger number of nodes. Dedicated libraries provide efficient and battle-tested implementations of these algorithms.
Q2. Consider a scenario where you need to implement the Flyweight design pattern to manage a large number of similar, immutable objects representing game characters. Each character has a unique name and shares a common set of attributes (e.g., texture, animation). Which of the following approaches BEST leverages the Flyweight pattern's intrinsic state/extrinsic state separation to minimize memory usage while maintaining character uniqueness?
-
A. Create a single character object for each unique name, storing common attributes externally in a HashMap keyed by character name.
-
B. Instantiate a new character object for each name, cloning the shared attributes and overwriting only the name, using a deep copy mechanism.
-
C. Utilize a factory method that returns a shared character object for the same attributes, using the character's name as an extrinsic state passed during rendering. ✓
-
D. Employ a singleton pattern, creating one character object per attribute set and using a unique identifier to differentiate characters during retrieval.
Explanation: The Flyweight pattern separates intrinsic (shared) and extrinsic (unique) states. Option 3 correctly identifies this: the factory provides the shared intrinsic state (texture, animation), while the name (extrinsic state) is passed separately during rendering, avoiding object duplication and minimizing memory usage. Options 1 and 2 fail to leverage shared object instances effectively. Option 4 is inappropriate; singletons aren't designed for large numbers of objects with varying extrinsic states.
Q3. You have a Python class named `Dog` with an attribute `breed`. Which code snippet correctly creates a `Dog` object named `myDog` and sets its breed to "Golden Retriever"?
-
A. myDog = Dog(breed = "Golden Retriever") ✓
-
B. Dog.breed = "Golden Retriever"
-
C. myDog.breed = Dog("Golden Retriever")
-
D. Dog myDog = new Dog("Golden Retriever");
Explanation: The correct way to create a Dog object and set its breed attribute is to use the class constructor, passing the 'breed' argument. Options 2 and 3 are incorrect as they attempt to modify the class itself or use incorrect syntax. Option 4 is Java syntax and not valid in Python.
Q4. Which of the following design patterns in OOP is used to handle the creation of objects in a way that allows for greater flexibility and control over the object creation process?
-
A. Factory Method Pattern ✓
-
B. Abstract Factory Pattern
-
C. Builder Pattern
-
D. Prototype Pattern
Explanation: The Factory Method Pattern is a creational design pattern that provides a way to create objects without specifying the exact class of object that will be created. It allows subclasses to decide which class to instantiate, allowing for greater flexibility and control over the object creation process.
Q5. In a class hierarchy with multiple inheritance (specifically addressing the diamond problem), how does C# handle method ambiguity when a derived class inherits the same method from two or more base classes?
-
A. C# automatically resolves the ambiguity by calling the method from the first base class listed in the inheritance order.
-
B. C# uses the `virtual` and `override` keywords, allowing the derived class to explicitly specify which method to use or provide a new implementation. ✓
-
C. C# does not allow multiple inheritance, thus avoiding the diamond problem.
-
D. C# uses the `sealed` keyword to prevent the method from being overridden in the derived class, thus resolving the ambiguity.
Explanation: C# resolves the diamond problem by using the `virtual` keyword in the base class to declare a method as overridable, and the `override` keyword in the derived class to provide a specific implementation or call a base class implementation. This allows developers to control the behavior of the method in the derived class and avoid ambiguity.
Q6. In a scenario where a class has multiple implementations of the same interface, which design pattern ensures that the correct implementation is chosen based on the runtime type of the object?
-
A. The Factory pattern ✓
-
B. The Strategy pattern
-
C. The Adapter pattern
-
D. The Bridge pattern
Explanation: The Factory pattern is used to provide a way to create objects without exposing the creation logic to the client, and it can be used to return a specific implementation of an interface based on the runtime type of the object.
Q7. In a scenario where a class has a circular dependency with another class, which of the following design patterns can be used to mitigate the issue?
-
A. Dependency Injection
-
B. Service Locator
-
C. MVP (Model-View-Presenter)
-
D. Mediator Pattern ✓
Explanation: The Mediator Pattern can help resolve circular dependencies by introducing a mediator object that acts as an intermediary between the dependent classes, allowing them to communicate without directly referencing each other.
Q8. In a 'Visitor' pattern, which of the following is the primary responsibility of the 'Object' being visited?
-
A. To maintain a reference to the 'Visitor' object.
-
B. To define an interface for accepting visitors. ✓
-
C. To perform some operation without knowing the details of the visitor.
-
D. To manage a collection of 'Visitor' objects.
Explanation: In the 'Visitor' pattern, the 'Object' being visited typically defines an interface for accepting visitors through methods such as 'accept', which takes a visitor object as a parameter. This interface allows the visitor to perform operations on the object without knowing the details of the object's class.
Q9. Given a class hierarchy with multiple inheritance, what is the correct order of method resolution in C++?
-
A. Depth-First Left-to-Right (DFLR) ✓
-
B. Depth-First Right-to-Left (DFRL)
-
C. Width-First Left-to-Right (WFLR)
-
D. Width-First Right-to-Left (WFRL)
Explanation: In C++, when a class inherits from multiple base classes, the order of method resolution is Depth-First Left-to-Right (DFLR). This means that the compiler will first look for the method in the current class, then in the leftmost base class, and then in the rightmost base class. This is specified in the C++ Standard (ISO/IEC 14882:2017).
Q10. In a scenario where a software system uses a combination of inheritance and composition, which of the following design patterns ensures that the child class can override the parent class's behavior while still maintaining a reference to the parent class's state?
-
A. The Proxy pattern
-
B. The Adapter pattern
-
C. The Decorator pattern ✓
-
D. The Facade pattern
Explanation: The Decorator pattern is a design pattern that allows a child class to override the parent class's behavior while still maintaining a reference to the parent class's state. This is achieved by wrapping the parent class with a decorator class that provides additional functionality without altering the parent class itself. In this scenario, the Decorator pattern ensures that the child class can override the parent class's behavior while still maintaining a reference to the parent class's state.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free