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…
Question
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)- A4% (1)
- B4% (1)
- C77% (20)
- E15% (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@interfacedeclaration - using undeclared elements is a compile error. - E (
@Resourcewith no elements) omits the requiredname()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
Community Discussion
No community discussion yet for this question.