Challenge - 5 Problems
Static Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of static block execution order
What is the output of this Java program that uses static blocks and a main method?
Java
public class Test { static { System.out.println("Static block 1"); } public static void main(String[] args) { System.out.println("Main method"); } static { System.out.println("Static block 2"); } }
Attempts:
2 left
💡 Hint
Static blocks run once when the class is loaded, before main method runs, in the order they appear.
✗ Incorrect
Static blocks execute in the order they appear in the class, before the main method runs. So first "Static block 1", then "Static block 2", then "Main method".
❓ Predict Output
intermediateStatic block and instance block output
What is the output when this Java program runs?
Java
public class Demo { static { System.out.println("Static block"); } { System.out.println("Instance block"); } public Demo() { System.out.println("Constructor"); } public static void main(String[] args) { new Demo(); new Demo(); } }
Attempts:
2 left
💡 Hint
Static block runs once when class loads; instance block runs before constructor each time an object is created.
✗ Incorrect
Static block runs once at class load. Then for each new object, instance block runs before constructor. So output is static block once, then instance block and constructor twice.
🔧 Debug
advancedIdentify the error in static block usage
What error does this Java code produce?
Java
public class ErrorTest { static { int x = 10 / 0; } public static void main(String[] args) { System.out.println("Hello"); } }
Attempts:
2 left
💡 Hint
Division by zero in static block causes error when class loads, before main runs.
✗ Incorrect
The static block runs when the class loads. Division by zero causes ArithmeticException at runtime during class loading, so program fails before main.
🧠 Conceptual
advancedStatic block and static variable initialization
Consider this Java class. What is the value of static variable 'count' after the class loads?
Java
public class Counter { static int count; static { count = 5; } static { count += 10; } }
Attempts:
2 left
💡 Hint
Static blocks run in order and can modify static variables.
✗ Incorrect
First static block sets count to 5, second adds 10, so final count is 15 after class loads.
❓ Predict Output
expertStatic block with inheritance and output order
What is the output when running this Java program?
Java
class Parent { static { System.out.println("Parent static block"); } { System.out.println("Parent instance block"); } public Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { static { System.out.println("Child static block"); } { System.out.println("Child instance block"); } public Child() { System.out.println("Child constructor"); } } public class TestInheritance { public static void main(String[] args) { new Child(); } }
Attempts:
2 left
💡 Hint
Static blocks run top to bottom in inheritance before main; instance blocks and constructors run in order of inheritance when object created.
✗ Incorrect
Static blocks run first: Parent then Child. Then creating Child object runs Parent instance block and constructor, then Child instance block and constructor.
