Java Reflection API: Beginner's Practical Use Cases
Explore the basics of Java Reflection API and its practical applications.
Questions in this quiz
15 questions on Java Reflection API: Beginner's Practical Use Cases. 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 perform operations on files
- BTo manage memory allocation
- CTo handle exceptions in Java
- DTo manipulate classes, methods, and fields at runtime
2.Which class in Java is commonly used to obtain class information via reflection?
- AMethod
- BObject
- CClassLoader
- DClass
3.How can you obtain a 'Class' object for a given class named 'Example'?
- AExample.class
- Bnew Example().getClass()
- CClass.forName('Example')
- DExample.getClass()
4.Which method is used to create a new instance of a class using reflection?
- AbuildInstance()
- BnewInstance()
- CgetInstance()
- DcreateInstance()
5.What exception might be thrown when using 'Class.forName()'?
- AIOException
- BClassNotFoundException
- CIllegalArgumentException
- DNullPointerException
6.Which method retrieves all public methods of a class?
- AgetPublicMethods()
- BgetMethods()
- CgetAllMethods()
- DgetDeclaredMethods()
7.How can you access a private field of a class using reflection?
- ABy using getField()
- BBy using getFieldPrivate()
- CBy using getDeclaredField()
- DBy using getPrivateField()
8.What must be done to modify a private field using reflection?
- AUse the modifyField() method
- BUse the setField() method
- CNothing, it can be modified directly
- DSet the field accessible using setAccessible(true)
9.Which method is used to invoke a method on an object using reflection?
- AcallMethod()
- BexecuteMethod()
- CrunMethod()
- Dinvoke()
10.What is returned by the 'getFields()' method?
- AAll fields of a class
- BOnly private fields of a class
- CAll static fields of a class
- DOnly public fields of a class
11.Which of the following is a limitation of using Java Reflection?
- AIt increases performance
- BIt can only access static fields
- CIt breaks encapsulation
- DIt can only be used with interfaces
12.What is the output type of 'getDeclaredMethods()'?
- AArray of Class objects
- BArray of Field objects
- CArray of Method objects
- DArray of String objects
13.Which of the following can be achieved using Java Reflection?
- AOptimize bytecode
- BAccess private methods
- CCompile Java code
- DManage memory
14.What is the purpose of 'getConstructors()' in reflection?
- ATo get all constructors of a class
- BTo get only public constructors of a class
- CTo get static constructors of a class
- DTo get private constructors of a class
15.Why should reflection be used cautiously in Java?
- AIt is deprecated
- BIt can only be used with primitive types
- CIt is not supported in all Java versions
- DIt can lead to performance overhead