Skills101
← All practice quizzes

Quiz: Creational Patterns in Java

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

How Java programs create objects cleanly: thread-safe Singletons (holder idiom and enum), a Factory that hides which class is created, and a Builder for objects with many optional fields.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Why is a Singleton's constructor made private?

  2. Question 2

    What is wrong with this lazy Singleton? static AppConfig get() { if (instance == null) instance = new AppConfig(); return instance; }

  3. Question 3

    In double-checked locking, why must the instance field be volatile?

  4. Question 4

    What does this print?

    class Counter {
        private static final Counter INSTANCE = new Counter();
        private int value = 0;
        private Counter() {}
        static Counter get() { return INSTANCE; }
        int next() { return ++value; }
    }
    
    public class Main {
        public static void main(String[] args) {
            Counter.get().next();
            Counter.get().next();
            System.out.println(Counter.get().next());
        }
    }
    
  5. Question 5

    Which Singleton version is also protected against reflection and serialization creating a second instance?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →