🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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);
    }
}
  1. A.1
  2. B.3
  3. C.0
  4. 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 →