Design Patterns  «Prev  Next»

Singleton Pattern - Quiz

Each question is worth one point. Select the best answer or answers for each question.
1. The general purpose of the Singleton pattern is to:
Please select the correct answer:
  A. Ensure that no more than one instance of a class exists.
  B. Ensure that only one instance of a class exists at the same time.
  C. Separate objects in a single class from objects in another class.
  D. Control creation of objects in a single class or another class.

2. The participants in a design pattern are:
Please select the correct answer:
  A. The methods used in the pattern
  B. The other patterns that participate with the pattern
  C. The classes and objects used in the pattern
  D. The fields used in the pattern

3. The consequences of a design pattern are:
Please select the correct answer:
  A. The pitfalls of using the particular pattern
  B. The results of choosing a pattern
  C. The time a program using the pattern takes to run
  D. The time it takes to design a program using the pattern

4. Which of the following is specifically in the realm of applicability of the Singleton?
Please select the correct answer:
  A. The class has only a single member method.
  B. The class has only a single field.
  C. The class should have exactly one instance.
  D. The class cannot be subclassed.

5. In the Singleton pattern, what is the role of the static getInstance() method?
Please select the correct answer:
  A. To create a new instance of the Singleton class every time it is called.
  B. To provide a global access point that returns the unique instance, creating it on the first call and returning the existing instance on all subsequent calls.
  C. To prevent any other class from importing or referencing the Singleton class.
  D. To replace the constructor so that the Singleton class cannot inherit from a superclass.

6. A developer needs a class that manages a shared print spooler. Only one spooler should ever exist in the application, and any part of the application must be able to retrieve it without holding a direct reference. Which aspect of the Singleton pattern satisfies both requirements?
Please select the correct answer:
  A. The private constructor prevents instantiation, and the public static getInstance() method provides the shared access point.
  B. The abstract getInstance() method forces subclasses to implement their own spooler.
  C. The Singleton pattern delegates spooler creation to a separate Factory class.
  D. The Singleton pattern uses interface inheritance to ensure only one spooler type exists at runtime.