1Z0-819 Annotations 3 — Questions and Answers
Question 1: What RetentionPolicy value causes an annotation to be discarded by the compiler and never stored in the .class file?
- RUNTIME
- CLASS
- SOURCE (Correct answer)
- COMPILE
Correct answer: SOURCE
RetentionPolicy.SOURCE annotations are stripped out during compilation and do not appear in the bytecode.
Question 2: Which method of java.lang.reflect.AnnotatedElement returns all annotations directly present on an element, excluding inherited ones?
- getAnnotations()
- getDeclaredAnnotations() (Correct answer)
- getAnnotationsByType()
- findAnnotation()
Correct answer: getDeclaredAnnotations()
getDeclaredAnnotations() returns only annotations directly present on the element, ignoring @Inherited annotations from superclasses.
Question 3: An annotation element type must be one of the permitted types. Which of the following is NOT a permitted annotation element type?
- A primitive type
- A String
- An enum constant type
- A generic type parameter (e.g., T) (Correct answer)
Correct answer: A generic type parameter (e.g., T)
Annotation element types must be primitives, String, Class, enum, another annotation type, or arrays thereof — generic type parameters are not permitted.
Question 4: What happens if you annotate an annotation type with @Target(ElementType.TYPE)?
- The annotation can only be applied to interfaces
- The annotation can be applied to classes, interfaces, enums, and annotation types (Correct answer)
- The annotation can be applied to any program element
- The annotation can only be applied to abstract classes
Correct answer: The annotation can be applied to classes, interfaces, enums, and annotation types
ElementType.TYPE covers all type declarations: classes, interfaces, enums, and annotation types.
Question 5: Which built-in Java annotation suppresses compiler warnings for unchecked generics casts?
- @SuppressWarnings("all")
- @SuppressWarnings("unchecked") (Correct answer)
- @SafeVarargs
- @SuppressWarnings("generics")
Correct answer: @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") tells the compiler to suppress warnings related to unchecked type casts and raw type usage.
Question 6: The @FunctionalInterface annotation is declared with which retention policy?
- SOURCE
- CLASS
- RUNTIME (Correct answer)
- NONE
Correct answer: RUNTIME
@FunctionalInterface has RUNTIME retention, allowing frameworks to inspect it via reflection.
Question 7: Which of the following annotation declarations is syntactically INVALID?
- public @interface MyAnno {}
- @interface MyAnno extends java.lang.annotation.Annotation {} (Correct answer)
- @interface MyAnno { int count() default 1; }
- @Retention(RUNTIME) @interface MyAnno {}
Correct answer: @interface MyAnno extends java.lang.annotation.Annotation {}
Annotation types implicitly extend java.lang.annotation.Annotation; explicitly writing 'extends' in the declaration is a compile error.
What RetentionPolicy value causes an annotation to be discarded by the compiler and never stored in the .class file?