0
0
Javaprogramming~15 mins

Static blocks in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Static Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2: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");
    }
}
A
Static block 1
Static block 2
Main method
B
Main method
Static block 1
Static block 2
C
Static block 2
Static block 1
Main method
D
Main method
Static block 2
Static block 1
Attempts:
2 left
💻 code output
intermediate
2: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();
    }
}
A
Instance block
Constructor
Instance block
Constructor
Static block
B
Static block
Instance block
Constructor
Instance block
Constructor
C
Static block
Constructor
Instance block
Constructor
Instance block
D
Instance block
Static block
Constructor
Instance block
Constructor
Attempts:
2 left
🔧 debug
advanced
2: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");
    }
}
ASyntaxError: division by zero not allowed
BNullPointerException at runtime
CNo error, prints Hello
DArithmeticException at runtime during class loading
Attempts:
2 left
🧠 conceptual
advanced
2: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;
    }
}
A15
B5
C0
DCompilation error
Attempts:
2 left
💻 code output
expert
3: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();
    }
}
A
Parent static block
Child static block
Child instance block
Child constructor
Parent instance block
Parent constructor
B
Child static block
Parent static block
Parent instance block
Parent constructor
Child instance block
Child constructor
C
Parent static block
Child static block
Parent instance block
Parent constructor
Child instance block
Child constructor
D
Parent instance block
Parent constructor
Child instance block
Child constructor
Parent static block
Child static block
Attempts:
2 left