What will be the result of compilation and execution of the following code?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.*;

@interface Anno{
    int value() default 5;
public class Test {
    public static void test(int i) { }
    public static void main(String [] args) {
        Method m = new Test().getClass().getMethod("test");
        Anno anno = m.getAnnotation(Anno.class);

Compilation error.

Method Class.getMethod() can throw NoSuchMethodException. It is a checked exception.

Therefore, it must be caught using try ... catch ... inside the main method or declared in the throws clause of method declaration.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes