Java Reflection API: Beginner's Guide
An introductory quiz on understanding and using the Java Reflection API.
Questions in this quiz
15 questions on Java Reflection API: Beginner's Guide. 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 manage memory allocation
- BTo perform runtime type introspection
- CTo compile Java code
- DTo handle exceptions
2.Which class is commonly used to obtain a Class object in Java Reflection?
- AReflection
- BObject
- CClass
- DMethod
3.How can you get the name of a class using Reflection?
- AclassInstance.name()
- BclassInstance.getName()
- CclassInstance.getClass().getName()
- DclassInstance.getClassName()
4.Which method is used to create an instance of a class using Reflection?
- AcreateInstance()
- BgetInstance()
- CnewInstance()
- Dinstantiate()
5.What exception might be thrown when using newInstance() in Reflection?
- ANullPointerException
- BInstantiationException
- CIOException
- DClassNotFoundException
6.Which method retrieves all public methods of a class using Reflection?
- AgetPublicMethods()
- BgetAllMethods()
- CgetMethods()
- DgetDeclaredMethods()
7.How can you access a private field of a class using Reflection?
- AUse getDeclaredField() and setAccessible(true)
- BUse getField() and setAccessible(true)
- CUse getPrivateField()
- DUse getField()
8.What is the return type of the getFields() method in Reflection?
- AField[]
- BMethod[]
- CObject[]
- DClass[]
9.Which method is used to invoke a method using Reflection?
- Acall()
- Bexecute()
- CinvokeMethod()
- Dinvoke()
10.What does the getDeclaredMethods() method return?
- AAll methods of the class
- BOnly public methods
- CAll declared methods regardless of access level
- DOnly static methods
11.Which Reflection method can be used to obtain the superclass of a class?
- AgetSuperclass()
- BgetBaseClass()
- CgetParentClass()
- DgetAncestorClass()
12.Can Reflection be used to change the value of a final field?
- AYes, using setFinal(false)
- BYes, using changeValue()
- CYes, using setAccessible(true)
- DNo, final fields cannot be changed
13.What is a potential drawback of using Reflection in Java?
- AIncreased performance
- BSimplified code
- CCompile-time safety
- DSecurity restrictions
14.Which method is used to get the modifiers of a class or member in Reflection?
- AgetModifiers()
- BgetPermissions()
- CgetAccessModifiers()
- DgetVisibility()
15.What is the output of the following code snippet? `Class<?> clazz = String.class; System.out.println(clazz.getName());`
- AString
- Bjava.lang.String
- Cclass String
- Djava.String