1Z0-819 Annotations 5 — Questions and Answers
Question 1: What is the effect of applying @Deprecated to an API element in Java 11?
- The element is removed from the classpath at compile time
- The compiler emits a warning when the element is used, and it appears in Javadoc as deprecated (Correct answer)
- The element throws DeprecatedException at runtime if called
- The element is hidden from IDE code completion only
Correct answer: The compiler emits a warning when the element is used, and it appears in Javadoc as deprecated
@Deprecated causes the compiler to issue a deprecation warning at use sites and marks the element in Javadoc.
Question 2: In Java 11, @Deprecated gained which additional element to indicate that the deprecated API may be removed in a future release?
- removed = true
- forRemoval = true (Correct answer)
- scheduled = true
- terminal = true
Correct answer: forRemoval = true
The forRemoval=true element, added in Java 9, signals that the deprecated API is planned for future removal and triggers an even stronger compiler warning.
Question 3: Which statement about annotation inheritance is accurate?
- @Inherited causes annotations to be inherited by implementing classes of an interface
- @Inherited only works for class-level annotations, not method-level ones (Correct answer)
- @Inherited allows annotation inheritance across packages only
- @Inherited applies to all ElementType targets automatically
Correct answer: @Inherited only works for class-level annotations, not method-level ones
@Inherited only propagates class-level annotations from superclass to subclass; it does not apply to interface implementations or method-level annotations.
Question 4: Which of the following annotation element types allows you to embed one annotation type inside another?
- Class<?>
- Enum
- Another annotation type (Correct answer)
- String
Correct answer: Another annotation type
An annotation element can itself be another annotation type, enabling composition of annotations.
Question 5: What compile-time error occurs when you use @Override on a method that does not actually override a superclass or interface method?
- RuntimeException: method not found in supertype
- Compile error: method does not override or implement a method from a supertype (Correct answer)
- Compile warning only, not an error
- NoSuchMethodError at runtime
Correct answer: Compile error: method does not override or implement a method from a supertype
@Override causes a compile-time error if the annotated method doesn't match any method signature in a superclass or implemented interface.
Question 6: RetentionPolicy.CLASS means annotations are stored in the .class file but NOT available at runtime. Which default retention policy applies when @Retention is omitted?
- SOURCE
- CLASS (Correct answer)
- RUNTIME
- NONE
Correct answer: CLASS
If @Retention is not specified, the default retention policy is CLASS — the annotation is recorded in the bytecode but not accessible via reflection at runtime.
Question 7: Which of the following correctly uses a type annotation in Java 11?
- @NotNull public String getName()
- public @NotNull String getName() (Correct answer)
- public String @NotNull getName()
- public String getName() @NotNull
Correct answer: public @NotNull String getName()
For a method return type annotation, the annotation is placed immediately before the type it annotates: public @NotNull String getName().
What is the effect of applying @Deprecated to an API element in Java 11?