Last Updated: December 6, 2025
32 quizzes
Mark a method so the compiler checks that it correctly overrides a method from its superclass.
@void executeTask() {}Click an option to fill the blank:
Annotate a field so that tools know it should not be included in JSON serialization.
@private String internalSecret;Click an option to fill the blank:
Specify that a custom annotation should be discarded by the compiler and never reach the class file.
@Retention(RetentionPolicy.)public @interface SourceOnly {}Click an option to fill the blank:
What is a key characteristic of annotations in Java?
Which built-in annotation helps catch method signature mistakes when overriding?
Which statement about @Deprecated is true?
Which built-in annotation is used to silence specific compiler warnings?
What does @FunctionalInterface enforce?
Which element controls where an annotation can be applied (e.g., method, type, field)?
Which retention policy is required if you want to read annotations via reflection?
Which retention policy keeps annotations in class files but makes them unavailable at runtime?
Which package contains the core annotation meta-annotations like @Retention and @Target?
What is a typical use of annotation processing at compile time?
Which class do custom annotation processors typically extend?
In a custom annotation, what does an element declaration like 'int count() default 1;' represent?
Order the steps to define and use a runtime-visible custom method annotation processed via reflection.
Drag and drop to reorder, or use the arrows.
Order the steps to create and register a compile-time annotation processor.
Drag and drop to reorder, or use the arrows.
What is the output of this code?
1class Demo {
2 @Deprecated
3 public static String oldApi() {
4 return "old";
5 }
6 public static void main(String[] args) {
7 System.out.println(oldApi());
8 }
9}What is the output of this code?
1interface Runner {
2 void run();
3}
4
5@FunctionalInterface
6interface Task extends Runner {
7 @Override
8 void run();
9}
10
11public class Test {
12 public static void main(String[] args) {
13 Task t = () -> System.out.print("ok");
14 t.run();
15 }
16}What is the output of this code?
1import java.lang.annotation.*;
2
3@Retention(RetentionPolicy.RUNTIME)
4@interface Label {
5 String value();
6}
7
8@Label("demo")
9class Service {}
10
11public class Main {
12 public static void main(String[] args) {
13 Label label = Service.class.getAnnotation(Label.class);
14 System.out.println(label.value());
15 }
16}What is the output of this code?
1import java.lang.annotation.*;
2
3@Retention(RetentionPolicy.CLASS)
4@interface ClassOnly {}
5
6@ClassOnly
7class Component {}
8
9public class App {
10 public static void main(String[] args) {
11 Object a = Component.class.getAnnotation(ClassOnly.class);
12 System.out.println(a == null);
13 }
14}What is the output of this code?
1import java.lang.annotation.*;
2
3@Retention(RetentionPolicy.SOURCE)
4@interface SourceOnly {}
5
6public class Demo {
7 @SourceOnly
8 public static void main(String[] args) {
9 System.out.println("run");
10 }
11}Find the bug related to retention in this custom annotation used with reflection.
Click on the line(s) that contain the bug.
import java.lang.annotation.*; @Retention(RetentionPolicy.SOURCE)@Target(ElementType.METHOD)public @interface Logged { String value();} class Service { @Logged("start") public void doWork() {}} class Inspector { public static void inspect() { for (var m : Service.class.getDeclaredMethods()) { Logged l = m.getAnnotation(Logged.class); if (l != null) { System.out.println(l.value()); } } }}Find the bug in this annotation processor declaration.
Click on the line(s) that contain the bug.
import javax.annotation.processing.*;import javax.lang.model.SourceVersion; @SupportedAnnotationTypes("my.AutoValue")@SupportedSourceVersion(SourceVersion.RELEASE_19)public class AutoValueProcessor extends AbstractProcessor { @Override public boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, RoundEnvironment roundEnv) { return false; }}Match each meta-annotation with what it controls.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each annotation usage with its typical effect.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the custom annotation so it can be applied to fields and read at runtime.
import java.lang.annotation.*;@(RetentionPolicy.RUNTIME)@(ElementType.FIELD)public @interface ConfigKey { String value();}Click an option to fill blank 1:
Complete the annotation usage so the processor sees the correct fully qualified annotation name.
import my.annotations.AutoValue;@SupportedAnnotationTypes("")@SupportedSourceVersion(.RELEASE_17)public class AutoValueProcessor extends AbstractProcessor {}Click an option to fill blank 1:
Complete the runtime annotation processing code that invokes a method based on a custom annotation.
import java.lang.annotation.*;import java.lang.reflect.*;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@interface RunOnStartup {}class StartupService { @RunOnStartup public void init() { System.out.println("init"); }}class Runner { public static void runAll() throws Exception { StartupService s = new StartupService(); for (Method m : StartupService.class.getDeclaredMethods()) { if (m.isAnnotationPresent(.class)) { .invoke(s); } } }}Click an option to fill blank 1:
Complete the custom annotation that has a default value and is documented.
import java.lang.annotation.*;@@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface Role { String value() default ;}Click an option to fill blank 1:
Click the line where the retention policy prevents reflection from seeing the annotation.
Click on the line to select.
import java.lang.annotation.*; @Retention(RetentionPolicy.SOURCE)@Target(ElementType.TYPE)@interface Entity {} @Entityclass User {} public class Main { public static void main(String[] args) { System.out.println(User.class.getAnnotation(Entity.class)); }}Click the line that declares which annotations this processor handles.
Click on the line to select.
import javax.annotation.processing.*;import javax.lang.model.SourceVersion; @SupportedAnnotationTypes("example.Service")@SupportedSourceVersion(SourceVersion.RELEASE_17)public class ServiceProcessor extends AbstractProcessor {}