What happens when you try to compile and run this code:

public class QTest {
    {
        System.out.print("1");
    }

    public static void main(String[] args) {
        System.out.print("2");
        new QTest();
    }

    static {
        System.out.print("3");
    }
}
Explanation
Static initialization blocks are executed before calling the main method and before the first instance of the class in which they are declared. Non-static initialization blocks are executed when the class is instantiated. Therefore, in this example, the first "System.out.print("3");" will be executed and then starts to work the method main - "System.out.print("2");" will be executed and finally, during the instillation of the exemplar of QTest System.out.print("1");" will be executed.

The correct answer - "The console will indicate 321".

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes