nerdexam
Oracle

1Z0-819 · Question #149

Which of the following lines in this code do not compile? (Choose all that apply.) enum WeatherType { COLD, WARM; } @Documented @interface Weather { // Line 1 String humidity(); // Line 2 boolean…

The correct answer is C. Line 3 E. Line 5. Lines C and E fail to compile for distinct reasons. Line 3 (boolean rainy() = false;) uses = to assign a default value, which is illegal in annotation declarations - the correct syntax requires the default keyword: boolean rainy() default false;. Line 5 (private static final…

Annotations

Question

Which of the following lines in this code do not compile? (Choose all that apply.) enum WeatherType { COLD, WARM; } @Documented @interface Weather { // Line 1 String humidity(); // Line 2 boolean rainy() = false; // Line 3 public abstract static int value = 1; // Line 4 private static final int humidity = 5; // Line 5 }

Options

  • ALine 1
  • BLine 2
  • CLine 3
  • DLine 4
  • ELine 5

How the community answered

(28 responses)
  • A
    7% (2)
  • B
    7% (2)
  • C
    71% (20)
  • D
    14% (4)

Explanation

Lines C and E fail to compile for distinct reasons. Line 3 (boolean rainy() = false;) uses = to assign a default value, which is illegal in annotation declarations - the correct syntax requires the default keyword: boolean rainy() default false;. Line 5 (private static final int humidity = 5;) violates a fundamental rule: annotation type members (like interface members) must be public, so the private modifier causes a compile error.

The distractors are wrong for these reasons: Line 1 is valid - @interface is the correct syntax for declaring an annotation type, and @Documented is a legal meta-annotation. Line 2 is valid - annotation elements look like abstract method signatures, and String humidity(); is a perfectly legal element declaration. Line 4, though it looks suspicious, compiles because annotation types (like interfaces) allow constant field declarations with public static modifiers, and abstract is treated redundantly (all annotation elements are implicitly abstract and public).

Memory tip: Think of annotation types as "interface cousins" - members must be public, default values need the default keyword (not =), and elements are implicitly abstract. Any private member or =-based default immediately flags as a compile error.

Topics

#annotation syntax#default values#access modifiers#field declarations

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice