Challenge - 5 Problems
Static Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2:00remaining
Output 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
💻 code output
intermediate2:00remaining
Static 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
🔧 debug
advanced2:00remaining
Identify 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
🧠 conceptual
advanced2:00remaining
Static 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
💻 code output
expert3:00remaining
Static 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
