🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Interview Questions: all 10 questions

Quiz: Java Interview Questions · Question 7 of 10

What does this print? (code)

What does this print?

class Config {
    private static Config instance;
    private Config() { }
    static Config get() {
        if (instance == null) instance = new Config();
        return instance;
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Config.get() == Config.get());
    }
}
  1. A.true
  2. B.false
  3. C.Compile error: constructors cannot be private
  4. D.NullPointerException
Answer & explanation

Correct answer: true

This is the Singleton pattern: the private constructor stops outside creation, and get() always returns the same object.

Can you get the rest right without peeking?

Take the timed Quiz: Java Interview Questions quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →