Bird
Raised Fist0
Javaprogramming~20 mins

Static blocks in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict 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
💡 Hint
Static blocks run once when the class is loaded, before main method runs, in the order they appear.
❓ Predict 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
💡 Hint
Static block runs once when class loads; instance block runs before constructor each time an object is created.
🔧 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
💡 Hint
Division by zero in static block causes error when class loads, before main runs.
🧠 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
💡 Hint
Static blocks run in order and can modify static variables.
❓ Predict 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
💡 Hint
Static blocks run top to bottom in inheritance before main; instance blocks and constructors run in order of inheritance when object created.

Practice

(1/5)
1. What is the main purpose of a static block in Java?
easy
A. To handle exceptions during runtime
B. To create instance variables for each object
C. To initialize static variables or run code once when the class loads
D. To define methods that can be overridden

Solution

  1. Step 1: Understand static block behavior

    A static block runs once when the class is loaded, before any object is created.
  2. Step 2: Identify its purpose

    It is mainly used to initialize static variables or run setup code that should execute once.
  3. Final Answer:

    To initialize static variables or run code once when the class loads -> Option C
  4. Quick Check:

    Static block purpose = initialize static variables [OK]
Hint: Static blocks run once at class load for static setup [OK]
Common Mistakes:
  • Thinking static blocks run for each object
  • Confusing static blocks with constructors
  • Assuming static blocks handle exceptions only
2. Which of the following is the correct syntax for a static block in Java?
easy
A. static() { System.out.println("Hello"); }
B. static { System.out.println("Hello"); }
C. static void { System.out.println("Hello"); }
D. static class { System.out.println("Hello"); }

Solution

  1. Step 1: Recall static block syntax

    A static block is declared with the keyword static followed by braces containing code, without parentheses or return types.
  2. Step 2: Check options

    static { System.out.println("Hello"); } matches the correct syntax: static { ... }. Others incorrectly add parentheses, return types, or keywords.
  3. Final Answer:

    static { System.out.println("Hello"); } -> Option B
  4. Quick Check:

    Static block syntax = static { ... } [OK]
Hint: Static blocks have no parentheses or return type [OK]
Common Mistakes:
  • Adding parentheses after static keyword
  • Using void or other return types
  • Trying to declare static block as a method
3. What will be the output of the following Java code?
class Test {
    static int x;
    static {
        x = 5;
        System.out.print("A");
    }
    static {
        x = 10;
        System.out.print("B");
    }
    public static void main(String[] args) {
        System.out.print(x);
    }
}
medium
A. AB10
B. BA10
C. AB5
D. BA5

Solution

  1. Step 1: Understand static block execution order

    Static blocks run in the order they appear when the class loads. First block prints "A" and sets x=5, second prints "B" and sets x=10.
  2. Step 2: Trace main method output

    Main prints the value of x after static blocks run, which is 10 (last assignment). So output is "AB10".
  3. Final Answer:

    AB10 -> Option A
  4. Quick Check:

    Static blocks run in order, last x=10 [OK]
Hint: Static blocks run top to bottom before main [OK]
Common Mistakes:
  • Assuming static blocks run after main
  • Confusing order of static blocks
  • Expecting x to be 5 instead of 10
4. Identify the error in the following code snippet:
class Example {
    static {
        System.out.println("Start");
    }
    static void block() {
        System.out.println("Inside method");
    }
}
medium
A. No error, code is valid
B. Static block must have a method name
C. Static block cannot contain print statements
D. Static block cannot be followed by a static method

Solution

  1. Step 1: Check static block rules

    Static blocks can contain code like print statements and do not require a method name.
  2. Step 2: Verify static method declaration

    Static methods can appear after static blocks without any issue.
  3. Final Answer:

    No error, code is valid -> Option A
  4. Quick Check:

    Static blocks and methods can coexist [OK]
Hint: Static blocks can have code; methods are separate [OK]
Common Mistakes:
  • Thinking static blocks need method names
  • Assuming print statements are invalid in static blocks
  • Believing static blocks and methods conflict
5. Consider this Java class:
class Config {
    static int value;
    static {
        value = 100;
        System.out.print("Init1-");
    }
    static {
        value += 50;
        System.out.print("Init2-");
    }
    static {
        if (value > 100) {
            value = 200;
            System.out.print("Init3-");
        }
    }
    public static void main(String[] args) {
        System.out.print(value);
    }
}

What is the output when this program runs?
hard
A. Init2-Init3-200
B. Init1-Init2-150
C. Init1-Init3-200
D. Init1-Init2-Init3-200

Solution

  1. Step 1: Trace static blocks in order

    First block sets value=100 and prints "Init1-". Second adds 50 (value=150) and prints "Init2-". Third checks if value>100 (true), sets value=200 and prints "Init3-".
  2. Step 2: Check main output

    Main prints the final value, which is 200 after all static blocks run.
  3. Final Answer:

    Init1-Init2-Init3-200 -> Option D
  4. Quick Check:

    Static blocks run sequentially, final value=200 [OK]
Hint: Static blocks run top to bottom, last changes persist [OK]
Common Mistakes:
  • Ignoring the condition in third static block
  • Assuming static blocks run in random order
  • Forgetting value updates in each block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard