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.*;


@Retention(RetentionPolicy.RUNTIME)
@interface Anno{
    int value() default 5;
}
public class Test {
    @Anno(10)
    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);
        System.out.println(anno.value());
    }
}
Explanation

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
Cosmo
Sign Up Now
or Subscribe for future quizzes