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

public class QTest {

    public static void main(String[] args) {
        new QTest();

    static {
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
Sign Up Now
or Subscribe for future quizzes