Bird
0
0

Which of the following code snippets correctly declares a static block in Java?

easy📝 Syntax Q3 of 15
Java - Static Keyword
Which of the following code snippets correctly declares a static block in Java?
Astatic void { System.out.println("Static block executed"); }
Bstatic() { System.out.println("Static block executed"); }
Cstatic { System.out.println("Static block executed"); }
Dvoid static { System.out.println("Static block executed"); }
Step-by-Step Solution
Solution:
  1. Step 1: Understand static block syntax

    A static block is declared using the keyword static followed by a pair of braces {} without any method name or return type.
  2. Step 2: Analyze options

    static { System.out.println("Static block executed"); } correctly uses static { ... }. Options B, C, and D incorrectly add parentheses or return types which are invalid for static blocks.
  3. Final Answer:

    static { System.out.println("Static block executed"); } -> Option C
  4. Quick Check:

    Static block syntax is static { ... } [OK]
Quick Trick: Static blocks use 'static' keyword with braces only [OK]
Common Mistakes:
  • Adding parentheses after static keyword
  • Declaring return type for static block
  • Using method-like syntax for static block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes