Skip to content
Sharp

Java Annotations · interview question

How can you check if a method in Java is annotated with a specific annotation at runtime?

Multiple choiceMediumchapter-16-curatedpremiumjava-annotationscustom-annotations
Choose one answer
Answer and explanation

Correct answer

D. Using method.isAnnotationPresent(MyAnnotation.class)

Why

The isAnnotationPresent method checks if the specified annotation is present on the element, returning true or false.

Test yourself on Java Annotations

A free test with instant, explained results. No account needed.

Start free test
All Java Annotations questions →