← Quiz: Java Classes and Objects: all 10 questions
Quiz: Java Classes and Objects · Question 2 of 10
What does this print? (code)
What does this print?
class A {
static int count = 0;
A() { count++; }
}
public class Main {
public static void main(String[] args) {
new A(); new A(); new A();
System.out.println(A.count);
}
}
- A.1
- B.3
- C.0
- D.Error
Answer & explanation
Correct answer: 3
count is static (shared), and each constructor call increments it.
Can you get the rest right without peeking?
Take the timed Quiz: Java Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →