Java Reflection API: Beginner's Deep Dive
Explore the basics of Java Reflection API and its applications.
Questions in this quiz
15 questions on Java Reflection API: Beginner's Deep Dive. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary purpose of the Java Reflection API?
- ATo enhance the performance of Java applications
- BTo manipulate the runtime behavior of applications
- CTo manage memory allocation
- DTo perform operations on files
2.Which class in Java is primarily used for obtaining class metadata using reflection?
- AField
- BClassLoader
- CClass
- DObject
3.How can you obtain the Class object of a class named 'Example' using reflection?
- Anew Example().getClass()
- BClass.forName("Example")
- CExample.class
- DExample.getClass()
4.Which method is used to create an instance of a class using reflection?
- AnewInstance()
- BbuildInstance()
- CgetInstance()
- DcreateInstance()
5.What exception is thrown if a class is not found when using Class.forName()?
- AIllegalAccessException
- BClassNotFoundException
- CInstantiationException
- DNoSuchMethodException
6.Which method is used to access a private field of a class using reflection?
- AgetFieldPrivate()
- BgetPrivateField()
- CgetDeclaredField()
- DgetField()
7.How can you change the accessibility of a private field to access it using reflection?
- Afield.setPublic(true)
- Bfield.setVisibility(true)
- Cfield.setAccessible(true)
- Dfield.setPrivate(false)
8.Which method is used to invoke a method of a class using reflection?
- Ainvoke()
- Bexecute()
- CcallMethod()
- DinvokeMethod()
9.What is the return type of the getMethods() method in the Class class?
- AList<Method>
- BMap<String, Method>
- CMethod[]
- DSet<Method>
10.Which of the following is NOT a use case of Java Reflection?
- ADynamic class loading
- BRuntime method invocation
- CCompile-time error checking
- DAccessing private fields
11.What is a potential drawback of using Java Reflection?
- AIncreased performance
- BSlower performance
- CEnhanced security
- DCompile-time type safety
12.Which method is used to retrieve all declared constructors of a class using reflection?
- AgetConstructors()
- BgetAllConstructors()
- CgetConstructorList()
- DgetDeclaredConstructors()
13.How can you check if a method is annotated with a specific annotation using reflection?
- Amethod.isAnnotatedWith(MyAnnotation.class)
- Bmethod.hasAnnotation(MyAnnotation.class)
- Cmethod.isAnnotationPresent(MyAnnotation.class)
- Dmethod.checkAnnotation(MyAnnotation.class)
14.Which package contains the Java Reflection API?
- Ajava.io
- Bjava.lang.reflect
- Cjava.util
- Djava.reflection
15.What is the output of the following code snippet? Class<?> clazz = String.class; System.out.println(clazz.getName());
- Aclass java.lang.String
- BString
- Cjava.lang
- Djava.lang.String