🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: static, final and Nested Classes: all 10 questions

Quiz: static, final and Nested Classes · Question 8 of 10

What does this print? (code)

What does this print?

public class Main {
    static int x = 10;
    public static void main(String[] args) {
        Main a = new Main();
        Main b = new Main();
        a.x = 20;
        System.out.println(b.x + Main.x);
    }
}
  1. A.20
  2. B.30
  3. C.40
  4. D.10
Answer & explanation

Correct answer: 40

x is static, so a.x, b.x and Main.x are the same variable. After a.x = 20 it is 20 everywhere: 20 + 20 = 40.

Can you get the rest right without peeking?

Take the timed Quiz: static, final and Nested Classes quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →