Question 1
A `PrinterManager` class in a large office system must ensure that only a single print queue instance exists at any time, so that all departments share the same queue and no print jobs are duplicated. Which implementation technique enforces this constraint in OOP?No clue? Show me the answer
Correct answer
Correct!
IncorrectStep-by-step walkthrough
Choose a solution method
Method #1Approach 1Step 1: Identify the design requirement
The system requires that only one
PrinterManagerinstance can ever exist, and all departments must share that same instance. This is the classic problem the Singleton pattern solves.Step 2: Recall the three Singleton steps
The Singleton pattern requires: (1) a private constructor to block external instantiation, (2) a private static variable holding the single instance, and (3) a public static method (e.g.,
getInstance()) that creates the instance on first call and returns it on every subsequent call.Step 3: Match to the correct option
The option that states "Make the constructor private, store a single instance as a private static variable, and expose it through a public static method" matches all three Singleton requirements precisely.
Step 4: Confirm the answer
This is the standard, well-recognised implementation of the Singleton pattern in both Java and Python. It is the only option that actually prevents multiple instances from being created.
Method #2Approach 2Step 1: Identify what is being asked
The question asks for the OOP technique that guarantees at most one instance of a class exists throughout a program.
Step 2: Eliminate: abstract class option
"Declare the class as abstract" is incorrect. An abstract class cannot be instantiated at all — subclasses could still each create their own instances, so this does not guarantee a single shared instance.
Step 3: Eliminate: aggregation option
"Use aggregation so that multiple departments hold a reference to the same externally created object" describes dependency injection or aggregation, not a pattern that enforces single instantiation. Nothing prevents a second instance from being created.
Step 4: Eliminate: override __eq__ option
"Override
__eq__" only changes how equality is evaluated — two separate objects can still exist in memory. It does nothing to prevent the creation of multiple instances.Step 5: Select the correct answer
The remaining option — private constructor + private static instance + public static accessor method — is the textbook Singleton implementation and the only one that truly restricts instantiation to a single object.
Question 2
Consider the following Python code: ```python class Instrument: def play(self): return "Playing a generic sound" class Guitar(Instrument): def play(self): return "Strumming chords" class Flute(Instrument): def play(self): return "Blowing a melody" ensemble = [Guitar(), Flute(), Guitar()] for inst in ensemble: print(inst.play()) ``` What OOP concept is primarily demonstrated by the loop that calls `play()` on every element of `ensemble`?No clue? Show me the answer
Correct answer
Correct!
IncorrectStep-by-step walkthrough
Choose a solution method
Method #1Approach 1Step 1: Observe the loop behaviour
The loop calls the same method
play()on each element ofensemble. The elements are a mixture ofGuitarandFluteobjects, which each overrideplay()with different implementations.Step 2: Apply the definition of polymorphism
Polymorphism is the ability of different objects to respond to the same method call in different ways. Here,
play()returns"Strumming chords"forGuitarand"Blowing a melody"forFlute— different results from an identical call.Step 3: Confirm it is method overriding (dynamic polymorphism)
Both
GuitarandFluteoverride theplay()method fromInstrument. The correct version is selected at runtime based on the object's actual type — this is dynamic (runtime) polymorphism.Step 4: Select the answer
The option "Polymorphism, because the same method call produces different behaviour depending on the actual object type" precisely matches the definition and the code behaviour.
Method #2Approach 2Step 1: Identify what is being asked
The question asks which OOP concept is primarily demonstrated by the loop calling
play()on a heterogeneous list of objects.Step 2: Eliminate: encapsulation
"Encapsulation" refers to hiding internal data behind an interface. While the method body is not shown to the caller, the question focuses on the varied runtime behaviour of
play(), not on data hiding. This is not the primary concept demonstrated.Step 3: Eliminate: abstraction
"Abstraction" relates to defining common interfaces (often via abstract classes). The base
Instrumentclass does provide a common interface, but the loop behaviour — different outputs from the same call — demonstrates polymorphism, not abstraction.Step 4: Eliminate: composition
"Composition" is a has-a relationship where one class owns instances of another. A list containing
Instrumentobjects is not composition in the OOP design sense — there is no ownership relationship between a class and a list variable.Step 5: Select the correct answer
Polymorphism is the only concept that directly explains why the same
play()call yields"Strumming chords"for one object and"Blowing a melody"for another.