Singleton Pattern   «Prev  Next»

Consequences of the Singleton Pattern - Quiz

Each question is worth one point. Select the best answer for each question.
1. What kind of pattern is Singleton?
Please select the correct answer:
  A. object creational
  B. object structural
  C. class behavioral
  D. class creational

2. The collaborations in a design pattern describe:
Please select the correct answer:
  A. The classes that collaborate with one another to form the pattern
  B. How classes and objects outside the pattern interact with the pattern
  C. How functions in the pattern invoke other functions in the pattern
  D. How methods in the pattern invoke other methods in the pattern

3. Which of the following is a consequence of the Singleton?
Please select the correct answer:
  A. Pure static members lend themselves to subclassing
  B. Different subclasses can be configured randomly
  C. The Singleton object can be used by one object at a given time
  D. Access to the object is controlled

4. A Singleton class would best be used to represent:
Please select the correct answer:
  A. A parallel port
  B. A serial port
  C. An application configuration manager
  D. A monitor

5. Which of the following implementation techniques ensures that a Singleton remains a single instance in a multithreaded Java application?
Please select the correct answer:
  A. Declare the instance field as public so all threads can access the same reference directly.
  B. Use double-checked locking with a volatile instance field, or initialize the instance in a static initializer block.
  C. Create a new instance inside the getInstance() method each time it is called to ensure thread safety.
  D. Declare the getInstance() method as abstract so subclasses can each maintain their own instance.

6. A developer argues that a class with only static methods and static fields achieves the same result as a Singleton. Which of the following best explains why the Singleton pattern is preferred over a purely static class?
Please select the correct answer:
  A. A Singleton uses less memory than a static class because it does not allocate any fields.
  B. A Singleton can be subclassed and its single instance can be replaced with an instance of a subclass at runtime, whereas a purely static class cannot be subclassed in a useful way.
  C. A Singleton prevents all other objects from accessing its data, whereas a static class allows unrestricted access.
  D. A Singleton is always faster than a static class because it avoids the overhead of class loading.