← 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());
}
}
- A.true
- B.false
- C.Compile error: constructors cannot be private
- 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 →