Static & Initialization Order Questions with Answers

23 practice questions on Static & Initialization Order (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

When does a static initialisation block run?

  1. Only when main() is declared in that class
  2. Each time a static method of the class is called
  3. Once, when the class is initialised (on its first active use)
  4. Every time an object of the class is created

Answer: C. Once, when the class is initialised (on its first active use)

Explanation

The JVM runs static initialisers once per class loader, the first time the class is actively used, for example when an instance is created or a static field is read.

Step-by-step solution

  1. A static block runs when the class is initialised by the JVM.
  2. It executes once per class loader.
  3. Runs on first active use of the class.
  4. Active use includes object creation or static field access.
  5. So it runs once, when class is initialised.

Why the other options are wrong

  • A. The static block does not depend on main; it runs when the class is first initialised.
  • B. A static block runs once at class initialisation, not on every static method call.
  • D. Object creation runs constructors and instance initialisers, not the static block.

Tip: Remember: Static block runs once on first use of the class.

Question 2Easy

What happens when this code is compiled?

public class Main {
    int value = 5;

    public static void main(String[] args) {
        System.out.println(value);
    }
}
  1. NullPointerException
  2. 5
  3. 0
  4. Compilation error: non-static variable referenced from a static context

Answer: D. Compilation error: non-static variable referenced from a static context

Explanation

main is static and has no this object, so it cannot access the instance field value directly.

Step-by-step solution

  1. The variable 'value' is declared as non-static in the Main class.
  2. The 'main' method is static and cannot access non-static variables directly.
  3. Trying to print 'value' from a static context causes a compilation error.
  4. Java requires explicit object creation to access non-static fields.
  5. The code fails to compile because of this issue.
  6. Compilation error: non-static variable referenced from a static context.

Why the other options are wrong

  • A. No NullPointerException happens, since the code fails at compile time for the static context.
  • B. 5 would need value to be static, but value is an instance field and main cannot use it directly.
  • C. 0 is the default, but the code does not compile because value is accessed from a static method.

Tip: Remember: Static methods cannot access non-static variables directly.

Question 3Easy

Which statement about a static method is correct?

  1. It belongs to the class and cannot use this or call instance methods directly
  2. It can be overridden in a subclass like any instance method
  3. It can only access private fields
  4. It is created again for each object

Answer: A. It belongs to the class and cannot use this or call instance methods directly

Explanation

Static methods have no receiver object, so this is unavailable. Subclasses can only hide them, not override them.

Step-by-step solution

  1. A static method belongs to the class, not to any object.
  2. So it has no this and cannot directly call instance methods.
  3. Subclasses can only hide a static method, not override it.
  4. Option A is the only correct statement.

Why the other options are wrong

  • B. Static methods cannot be overridden, only hidden.
  • C. A static method can access any static member, not only private fields.
  • D. A static method exists once per class, not once per object.

Tip: Static means class-level: no this, no overriding, one copy shared by all objects.

Question 4Easy

What is the output?

public class Main {
    static int sq(int x) { return x * x; }

    public static void main(String[] args) {
        System.out.println(sq(3) + sq(-2));
    }
}
  1. -13
  2. 5
  3. 1
  4. 13

Answer: D. 13

Explanation

sq(3) = 9 and sq(-2) = 4, so the sum is 13.

Step-by-step solution

  1. sq(3) = 3 * 3 = 9.
  2. sq(-2) = (-2) * (-2) = 4.
  3. Sum of 9 and 4 is 13.
  4. So the output is 13.

Why the other options are wrong

  • A. The result is positive: 9 plus 4 is 13, since (-2) times (-2) is 4.
  • B. 5 would need sq(-2) to be 4 and sq(3) to be 1, but sq(3) is 9.
  • C. 1 would be the result only if both calls returned 1, but sq(3) is 9 and sq(-2) is 4.

Tip: Remember that squaring any non-zero number (positive or negative) always gives a positive result.

Question 5Medium

What is the output?

public class Main {
    static int x = 10;
    static { x = x * 2; }
    static int y = x + 1;

    public static void main(String[] args) {
        System.out.println(x + " " + y);
    }
}
  1. 20 11
  2. 10 21
  3. 20 21
  4. 10 11

Answer: C. 20 21

Explanation

Static initialisers and static field initialisers run in textual order: x = 10, then the block doubles it to 20, then y = 21.

Step-by-step solution

  1. X is initialized to 10.
  2. Static block sets x = 10 * 2 = 20.
  3. Y is assigned x + 1 = 20 + 1 = 21.
  4. Main method prints x and y: 20 21.

Why the other options are wrong

  • A. y is 21, not 11, since the static block already changed x to 20 before y is set.
  • B. x is 20 after the static block runs, so the first value is not 10.
  • D. x is 20 after the static block runs and y is 21, so both values are wrong here.

Tip: Remember static fields and blocks execute in source code order.

Question 6Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        int x = 0;
        x = x++;
        x = x++;
        System.out.println(x);
    }
}
  1. Compilation error
  2. 2
  3. 1
  4. 0

Answer: D. 0

Explanation

x++ evaluates to the old value 0 and then increments x, but the assignment stores the old value back. Each statement leaves x at 0.

Step-by-step solution

  1. X starts at 0.
  2. X++ yields the old value 0 and briefly sets x to 1, but the assignment x = stores 0 back.
  3. The second line does the same, so x is 0 again.
  4. Println prints 0, which is option D.

Why the other options are wrong

  • A. The code compiles; x = x++ is legal Java.
  • B. 2 would need the increments to survive, but the assignment overwrites them.
  • C. 1 would need one increment to survive, but each assignment overwrites it.

Tip: x = x++ leaves x unchanged, because the assignment overwrites the increment.

Question 7Medium

What is the output?

class Config {
    static final String NAME = "app";
    static { System.out.print("init "); }
}

class Holder {
    static int n = 7;
    static { System.out.print("holder "); }
}

public class Main {
    public static void main(String[] args) {
        System.out.print(Config.NAME + " ");
        System.out.print(Holder.n);
    }
}
  1. app 7
  2. init app holder 7
  3. app holder 7
  4. init app 7

Answer: C. app holder 7

Explanation

Config.NAME is a compile-time constant, so it is inlined and does not trigger Config's initialisation. Reading Holder.n (not a constant) initialises Holder first.

Step-by-step solution

  1. Config.NAME is a static final String constant, so 'app' is inlined and class Config is never initialised.
  2. Print writes 'app ' with no initialisation message.
  3. Holder.n is a non-constant static read, so Holder initialises first and prints 'holder '.
  4. Then 7 is printed, giving app holder 7 (option C).

Why the other options are wrong

  • A. Reading Holder.n runs Holder's static block, so 'holder ' is printed before 7.
  • B. Config is never initialised, so 'init ' is never printed.
  • D. Holder's static block does run and print 'holder '.

Tip: Reading a compile-time constant does not load its class; reading any other static field does.

Question 8Medium

What is the output?

class Outer {
    static class Nested {
        String hi() { return "nested"; }
    }
    class Inner {
        String hi() { return "inner"; }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Nested n = new Outer.Nested();
        Outer.Inner i = new Outer().new Inner();
        System.out.println(n.hi() + " " + i.hi());
    }
}
  1. nested nested
  2. nested inner
  3. Compilation error
  4. inner nested

Answer: B. nested inner

Explanation

A static nested class can be created without an Outer object. A non-static inner class needs an enclosing instance, supplied by new Outer().new Inner().

Step-by-step solution

  1. Static Nested is accessed with Outer.Nested()
  2. Non-static Inner requires Outer instance via new Outer().new Inner()
  3. n.hi() returns 'nested'
  4. i.hi() returns 'inner'
  5. Output is nested inner.

Why the other options are wrong

  • A. The Inner class hi() returns inner, so the second value cannot be nested.
  • C. The code compiles, since Outer.Nested is static and new Outer().new Inner() is valid syntax.
  • D. The values are swapped: n.hi() gives nested and i.hi() gives inner, so the order is nested inner.

Tip: Static nested can be created directly, inner needs outer instance.

Question 9Hard

What is the output?

public class Main {
    static int a = b() + 1;
    static int c = 10;

    static int b() { return c; }

    public static void main(String[] args) {
        System.out.println(a + " " + c);
    }
}
  1. 1 0
  2. 11 10
  3. 1 10
  4. 11 11

Answer: C. 1 10

Explanation

a is initialised first, and b() reads c before c's initialiser has run, so it returns the default 0. a = 1, then c becomes 10.

Step-by-step solution

  1. Static initialisers run top to bottom, so a is set first.
  2. a = b() + 1 calls b(), which returns c, but c's initialiser has not run yet.
  3. C is still the default 0, so b() returns 0 and a = 0 + 1 = 1.
  4. Then c = 10, so main prints 1 10, which is option C.

Why the other options are wrong

  • A. C is 10 by the time main prints it, not 0.
  • B. b() sees c as 0, not 10, so a is 1, not 11.
  • D. A is 1, not 11, because c was still 0 when b() ran.

Tip: A static initialiser that reads a later static field sees that field's default value, not its declared value.

Question 10Hard

What is the output?

class Base {
    static String s = init("Base.s");
    String i = init("Base.i");
    static String init(String n) { System.out.print(n + " "); return n; }
}

class Child extends Base {
    static String s2 = init("Child.s");
    String i2 = init("Child.i");
}

public class Main {
    public static void main(String[] args) {
        System.out.print("main ");
        new Child();
    }
}
  1. main Base.s Child.s Base.i Child.i
  2. Base.s Child.s main Base.i Child.i
  3. main Base.s Base.i Child.s Child.i
  4. main Child.s Base.s Base.i Child.i

Answer: A. main Base.s Child.s Base.i Child.i

Explanation

Classes are initialised lazily, on first active use, so "main" prints first. Then Base and Child static fields run, followed by instance fields parent-first.

Step-by-step solution

  1. main prints first because classes load lazily.
  2. new Child() initialises Base statics, then Child statics.
  3. Then instance fields run parent first.
  4. Output: main Base.s Child.s Base.i Child.i.

Why the other options are wrong

  • B. Static init happens at first use, which is after main starts.
  • C. All statics run before any instance fields.
  • D. The parent class initialises before the child.

Tip: Statics once, parent before child.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics