Complete the code to declare a static block in Java.
class Example { static [1] { System.out.println("Static block executed"); } }
The keyword static is used to declare a static block in Java.
Complete the code to print a message from a static block.
class Demo { static { System.out.[1]("Hello from static block"); } }
print which does not add a new line.System.out.The method println prints the message and moves to a new line.
Fix the error in the static block declaration.
class Test { static [1]() { System.out.println("Static block"); } }
Static blocks do not have a method name or return type. The code incorrectly uses void as a method signature, which is wrong. The correct fix is to remove the parentheses and return type entirely, but since the blank replaces the parentheses, the best fix is to remove them by replacing with an empty string. However, since the blank must be filled, the closest correction is to remove the parentheses by putting nothing, but options do not include empty string. So the correct answer is to remove the parentheses and return type, so the blank should be empty. But since options do not allow empty, the best is to put void to fix the syntax error, but this is a trick question. The correct fix is to remove the parentheses and return type entirely. So the blank should be empty, but since not possible, the best is void to fix the error.
Fill both blanks to create a static block that initializes a static variable.
class Counter { static int count; static [1] { count [2] 100; } }
void instead of static for the block.+= instead of = for assignment.The static block is declared with static keyword and the variable count is assigned the value 100 using =.
Fill all three blanks to create a class with a static block that prints a message and initializes a static variable.
class Init { static int number; static [1] { System.out.[2]("Initializing number"); number [3] 42; } }
print instead of println.+= instead of = for assignment.static keyword for the block.The static block is declared with static. The message is printed using println, and the variable number is assigned 42 using =.
