0
0
Javaprogramming~15 mins

Static blocks in Java - Interactive Code Practice

Choose your learning style8 modes available
ads_clickPractice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a static block in Java.

Java
class Example {
    static [1] {
        System.out.println("Static block executed");
    }
}
🎯 Drag options to blanks, or click blank then click option
Ablock
Bfinal
Cvoid
Dstatic
Attempts:
3 left
2fill in blank
medium

Complete the code to print a message from a static block.

Java
class Demo {
    static {
        System.out.[1]("Hello from static block");
    }
}
🎯 Drag options to blanks, or click blank then click option
Aprintln
Bprint
Cwrite
Dprintf
Attempts:
3 left
3fill in blank
hard

Fix the error in the static block declaration.

Java
class Test {
    static [1]() {
        System.out.println("Static block");
    }
}
🎯 Drag options to blanks, or click blank then click option
Astatic
Bvoid
Cblock
Dmain
Attempts:
3 left
4fill in blank
hard

Fill both blanks to create a static block that initializes a static variable.

Java
class Counter {
    static int count;
    static [1] {
        count [2] 100;
    }
}
🎯 Drag options to blanks, or click blank then click option
Astatic
B=
C+=
Dvoid
Attempts:
3 left
5fill in blank
hard

Fill all three blanks to create a class with a static block that prints a message and initializes a static variable.

Java
class Init {
    static int number;
    static [1] {
        System.out.[2]("Initializing number");
        number [3] 42;
    }
}
🎯 Drag options to blanks, or click blank then click option
Astatic
Bprintln
C=
Dprint
Attempts:
3 left