nerdexam
Oracle

1Z0-819 · Question #38

Given the declaration: @interface Resource { String name(); int priority() default 0; } Examine this code fragment: / Loc1 / /* class ProcessOrders { ... } Which two annotations may be applied at…

The correct answer is C. @Resource(name="Customer1", priority=100) D. @Resource(name="Customer1"). Options C and D are valid because the @Resource annotation declares two elements: name() with no default (making it required) and priority() with default 0 (making it optional). Both C and D supply the required name element and use only declared element names - C explicitly…

Annotations

Question

Given the declaration: @interface Resource { String name(); int priority() default 0; } Examine this code fragment: /* Loc1 / / class ProcessOrders { ... } Which two annotations may be applied at Loc1 in the code fragment? (Choose two.)

Options

  • A@Resource(proxy=100)
  • B@Resource(lookup="")
  • C@Resource(name="Customer1", priority=100)
  • D@Resource(name="Customer1")
  • E@Resource

How the community answered

(26 responses)
  • A
    4% (1)
  • B
    4% (1)
  • C
    77% (20)
  • E
    15% (4)

Explanation

Options C and D are valid because the @Resource annotation declares two elements: name() with no default (making it required) and priority() with default 0 (making it optional). Both C and D supply the required name element and use only declared element names - C explicitly sets both, while D omits priority and lets it fall back to its default of 0.

Why the distractors fail:

  • A (proxy=100) and B (lookup="") reference element names (proxy, lookup) that simply don't exist in the @interface declaration - using undeclared elements is a compile error.
  • E (@Resource with no elements) omits the required name() element, which has no default, so the compiler will reject it.

Memory tip: Think of annotation elements like method parameters with defaults - if an element has no default, it's mandatory. Also, you can never "invent" new element names; the annotation type is a contract and only its declared names are legal.

Topics

#Annotation syntax#Member requirements#Default values

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice