1Z0-819 Annotations 2 — Questions and Answers
Question 1: Which meta-annotation specifies that an annotation should be recorded in the Javadoc output?
- @Retention
- @Documented (Correct answer)
- @Inherited
- @Target
Correct answer: @Documented
@Documented causes the annotation to appear in the generated Javadoc for the annotated element.
Question 2: What does the @Inherited meta-annotation mean when applied to an annotation type?
- The annotation can be applied to subclasses only
- Subclasses automatically inherit the annotation from their superclass (Correct answer)
- The annotation type extends another annotation type
- The annotation applies to all members of the class
Correct answer: Subclasses automatically inherit the annotation from their superclass
@Inherited causes a class-level annotation to be automatically inherited by subclasses if they do not explicitly annotate themselves.
Question 3: Which of the following is a valid annotation element default value declaration?
- int value() default null;
- String name() default ""; (Correct answer)
- List<String> items() default {};
- Class<?> type() default void;
Correct answer: String name() default "";
Annotation element defaults must be constant expressions; an empty String literal is valid, but null and non-array/Class defaults have restrictions.
Question 4: What is a marker annotation?
- An annotation with exactly one element named 'value'
- An annotation with no elements (Correct answer)
- An annotation that can only be applied to methods
- An annotation that replaces Javadoc comments
Correct answer: An annotation with no elements
A marker annotation is an annotation type with no declared elements, used solely to mark a program element.
Question 5: Which ElementType constant allows an annotation to be placed on a formal parameter of a method?
- FIELD
- LOCAL_VARIABLE
- PARAMETER (Correct answer)
- METHOD
Correct answer: PARAMETER
ElementType.PARAMETER targets method and constructor formal parameters.
Question 6: Given: @interface Config { String[] tags() default {}; } — which usage is correct?
- @Config(tags = "fast")
- @Config(tags = {"fast", "stable"})
- @Config(tags = ["fast"])
- Both A and B (Correct answer)
Correct answer: Both A and B
A single-element array shorthand allows omitting braces, so both @Config(tags="fast") and @Config(tags={"fast","stable"}) are legal.
Question 7: Which statement about repeating annotations (introduced in Java 8) is correct?
- The annotation type itself must be annotated with @Repeatable pointing to its container annotation (Correct answer)
- The JVM stores each repeated annotation separately without a container
- Repeating annotations are only allowed on classes, not methods
- A repeated annotation cannot be retrieved via reflection
Correct answer: The annotation type itself must be annotated with @Repeatable pointing to its container annotation
@Repeatable requires specifying a container annotation type whose value() element holds an array of the repeatable annotation.
Which meta-annotation specifies that an annotation should be recorded in the Javadoc output?