AlgoMaster Logo

Annotations - Quiz

Last Updated: December 6, 2025

1 min read

Annotations Exercises

32 quizzes

1
Code Completion

Mark a method so the compiler checks that it correctly overrides a method from its superclass.

java
1
@
2
void executeTask() {}

Click an option to fill the blank:

2
Code Completion

Annotate a field so that tools know it should not be included in JSON serialization.

java
1
@
2
private String internalSecret;

Click an option to fill the blank:

3
Code Completion

Specify that a custom annotation should be discarded by the compiler and never reach the class file.

java
1
@Retention(RetentionPolicy.)
2
public @interface SourceOnly {}

Click an option to fill the blank:

4
Multiple Choice

What is a key characteristic of annotations in Java?

5
Multiple Choice

Which built-in annotation helps catch method signature mistakes when overriding?

6
Multiple Choice

Which statement about @Deprecated is true?

7
Multiple Choice

Which built-in annotation is used to silence specific compiler warnings?

8
Multiple Choice

What does @FunctionalInterface enforce?

9
Multiple Choice

Which element controls where an annotation can be applied (e.g., method, type, field)?

10
Multiple Choice

Which retention policy is required if you want to read annotations via reflection?

11
Multiple Choice

Which retention policy keeps annotations in class files but makes them unavailable at runtime?

12
Multiple Choice

Which package contains the core annotation meta-annotations like @Retention and @Target?

13
Multiple Choice

What is a typical use of annotation processing at compile time?

14
Multiple Choice

Which class do custom annotation processors typically extend?

15
Multiple Choice

In a custom annotation, what does an element declaration like 'int count() default 1;' represent?

16
Sequencing

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.

17
Sequencing

Order the steps to create and register a compile-time annotation processor.

Drag and drop to reorder, or use the arrows.

18
Output Prediction

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}
19
Output Prediction

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}
20
Output Prediction

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}
21
Output Prediction

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}
22
Output Prediction

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}
23
Bug Spotting

Find the bug related to retention in this custom annotation used with reflection.

Click on the line(s) that contain the bug.

java
1
import java.lang.annotation.*;
2
 
3
@Retention(RetentionPolicy.SOURCE)
4
@Target(ElementType.METHOD)
5
public @interface Logged {
6
    String value();
7
}
8
 
9
class Service {
10
    @Logged("start")
11
    public void doWork() {}
12
}
13
 
14
class Inspector {
15
    public static void inspect() {
16
        for (var m : Service.class.getDeclaredMethods()) {
17
            Logged l = m.getAnnotation(Logged.class);
18
            if (l != null) {
19
                System.out.println(l.value());
20
            }
21
        }
22
    }
23
}
24
Bug Spotting

Find the bug in this annotation processor declaration.

Click on the line(s) that contain the bug.

java
1
import javax.annotation.processing.*;
2
import javax.lang.model.SourceVersion;
3
 
4
@SupportedAnnotationTypes("my.AutoValue")
5
@SupportedSourceVersion(SourceVersion.RELEASE_19)
6
public class AutoValueProcessor extends AbstractProcessor {
7
    @Override
8
    public boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations,
9
                           RoundEnvironment roundEnv) {
10
        return false;
11
    }
12
}
25
Matching

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.

26
Matching

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.

27
Fill in the Blanks

Complete the custom annotation so it can be applied to fields and read at runtime.

java
1
import java.lang.annotation.*;
2
3
@(RetentionPolicy.RUNTIME)
4
@(ElementType.FIELD)
5
public @interface ConfigKey {
6
String value();
7
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the annotation usage so the processor sees the correct fully qualified annotation name.

java
1
import my.annotations.AutoValue;
2
3
@SupportedAnnotationTypes("")
4
@SupportedSourceVersion(.RELEASE_17)
5
public class AutoValueProcessor extends AbstractProcessor {}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the runtime annotation processing code that invokes a method based on a custom annotation.

java
1
import java.lang.annotation.*;
2
import java.lang.reflect.*;
3
4
@Retention(RetentionPolicy.RUNTIME)
5
@Target(ElementType.METHOD)
6
@interface RunOnStartup {}
7
8
class StartupService {
9
@RunOnStartup
10
public void init() {
11
System.out.println("init");
12
}
13
}
14
15
class Runner {
16
public static void runAll() throws Exception {
17
StartupService s = new StartupService();
18
for (Method m : StartupService.class.getDeclaredMethods()) {
19
if (m.isAnnotationPresent(.class)) {
20
.invoke(s);
21
}
22
}
23
}
24
}

Click an option to fill blank 1:

30
Fill in the Blanks

Complete the custom annotation that has a default value and is documented.

java
1
import java.lang.annotation.*;
2
3
@
4
@Retention(RetentionPolicy.RUNTIME)
5
@Target(ElementType.TYPE)
6
public @interface Role {
7
String value() default ;
8
}

Click an option to fill blank 1:

31
Hotspot Selection

Click the line where the retention policy prevents reflection from seeing the annotation.

Click on the line to select.

java
1
import java.lang.annotation.*;
2
 
3
@Retention(RetentionPolicy.SOURCE)
4
@Target(ElementType.TYPE)
5
@interface Entity {}
6
 
7
@Entity
8
class User {}
9
 
10
public class Main {
11
    public static void main(String[] args) {
12
        System.out.println(User.class.getAnnotation(Entity.class));
13
    }
14
}
32
Hotspot Selection

Click the line that declares which annotations this processor handles.

Click on the line to select.

java
1
import javax.annotation.processing.*;
2
import javax.lang.model.SourceVersion;
3
 
4
@SupportedAnnotationTypes("example.Service")
5
@SupportedSourceVersion(SourceVersion.RELEASE_17)
6
public class ServiceProcessor extends AbstractProcessor {
7
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.