Skills101
← All practice quizzes

Quiz: Object Initialization Order

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

What exactly runs, and in what order, when you write new in Java? Static blocks, field initialisers, instance blocks, constructors, this() and super() — with the inheritance cases that interviewers love to ask.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What does this print?

    class A {
        static StringBuilder log = new StringBuilder();
        static { log.append("S"); }
        { log.append("I"); }
        A() { log.append("C"); }
    }
    
    public class Main {
        public static void main(String[] args) {
            new A();
            new A();
            System.out.println(A.log);
        }
    }
    
  2. Question 2

    When does a class's static block run?

  3. Question 3

    A class has a field initialiser and an instance block. Which runs first?

  4. Question 4

    What does this print?

    class P {
        static String log = "";
        P() { log += "P "; }
    }
    
    class Q extends P {
        Q() { log += "Q "; }
        Q(int x) { this(); log += "Q" + x; }
    }
    
    public class Main {
        public static void main(String[] args) {
            new Q(5);
            System.out.println(P.log);
        }
    }
    
  5. Question 5

    Parent Person has only the constructor Person(String name). What happens with class Student extends Person { Student() { } }?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →