Question discussion

What is the output of the following Java program?class A { static { System.out.print("S"); } { System.out.print("I"); } A() { System.out.pr…

1141Medium DSSSB TGT Computer Science 0 replies 2 views
0

What is the output of the following Java program?

class A {
    static { System.out.print("S"); }
    { System.out.print("I"); }
    A() { System.out.print("C"); }
}
public class Main {
    public static void main(String[] args) {
        new A(); new A();
    }
}
  • ASICSIC
  • BSICIC
  • CICIC
  • DSSIICC
Correct answer: B

Explanation

The static block runs once when the class is loaded (S); each object creation runs the instance initialiser (I) and then the constructor (C).

Discussion 0

Nobody has replied yet. Start the discussion!