1Z0-819 Annotations 4 — Questions and Answers
Question 1: What is the purpose of the @Native meta-annotation in Java?
- It marks a method as callable from native C/C++ code
- It indicates that a constant field may be referenced from native code (Correct answer)
- It marks a class as having a native implementation
- It disables JIT compilation for the annotated method
Correct answer: It indicates that a constant field may be referenced from native code
@Native (java.lang.annotation.Native) marks a constant-valued field that may be referenced from native code, hinting to tools to generate header files.
Question 2: Which annotation was introduced in Java 9 to indicate that a module exports a package for use only by specific other modules?
- @Export
- @Qualified
- @Module
- None — this is a module-info.java keyword, not an annotation (Correct answer)
Correct answer: None — this is a module-info.java keyword, not an annotation
Qualified exports in module-info.java are declared with the 'exports ... to ...' syntax, not with a Java annotation.
Question 3: Given a repeatable annotation @Tag and its container @Tags, which method should you use to retrieve all @Tag instances from a method at runtime?
- method.getAnnotation(Tag.class)
- method.getDeclaredAnnotation(Tags.class)
- method.getAnnotationsByType(Tag.class) (Correct answer)
- method.getAnnotations()
Correct answer: method.getAnnotationsByType(Tag.class)
getAnnotationsByType(Tag.class) transparently unwraps the container and returns all repeated @Tag annotations as a flat array.
Question 4: Which of the following correctly defines a single-element annotation where the element is named 'value'?
- public @interface Timeout { long millis(); }
- public @interface Timeout { long value(); } (Correct answer)
- public @interface Timeout { value long; }
- public @interface Timeout { default long value = 0; }
Correct answer: public @interface Timeout { long value(); }
The special element name 'value' allows callers to omit the element name: @Timeout(500) instead of @Timeout(value=500).
Question 5: An annotation processor that generates new source files implements which interface from javax.annotation.processing?
- AnnotationProcessor
- Processor (Correct answer)
- ElementVisitor
- SourceGenerator
Correct answer: Processor
javax.annotation.processing.Processor is the interface annotation processors implement; AbstractProcessor provides a convenient base class.
Question 6: What does @SafeVarargs guarantee when applied to a method?
- The method does not throw any exceptions
- The method does not perform potentially unsafe operations on its varargs parameter (Correct answer)
- The method is thread-safe
- The varargs array is not null
Correct answer: The method does not perform potentially unsafe operations on its varargs parameter
@SafeVarargs is a programmer assertion that the method body does not perform heap-polluting operations on the generic varargs parameter.
Question 7: Which ElementType value must be included in @Target for an annotation to be usable as a type annotation (e.g., @NotNull List<@NotNull String>)?
- ElementType.FIELD
- ElementType.TYPE
- ElementType.TYPE_USE (Correct answer)
- ElementType.PARAMETER
Correct answer: ElementType.TYPE_USE
ElementType.TYPE_USE (added in Java 8) allows an annotation to appear wherever a type is used, including generic type arguments.
What is the purpose of the @Native meta-annotation in Java?