Java Reflection API: Beginner's Practical Guide
Explore the basics of Java Reflection API and its practical uses.
Questions in this quiz
15 questions on Java Reflection API: Beginner's Practical 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 enhance the performance of Java applications
- BTo inspect and modify runtime behavior of applications
- CTo perform compile-time checks
- DTo manipulate object fields directly
2.Which class in Java is commonly used to obtain metadata about a class using reflection?
- AReflection
- BClassLoader
- CObject
- DClass
3.How can you obtain a Class object for a given class named 'Example'?
- AExample.class
- BClass.forName('Example')
- CExample.getClass()
- Dnew Example()
4.Which method is used to create a new instance of a class using reflection?
- AinstanceOf()
- BcreateInstance()
- CgetInstance()
- DnewInstance()
5.What exception might be thrown when creating a new instance using reflection?
- AIllegalAccessException
- BBoth IllegalAccessException and InstantiationException
- CInstantiationException
- DNo exceptions are thrown
6.Which method is used to access a private field of a class using reflection?
- AgetPrivateField()
- BgetDeclaredField()
- CgetFieldByName()
- DgetField()
7.How can you change the value of a private field using reflection?
- ABy using setAccessible(true) on the Field object
- BBy using the setValue() method
- CBy using the modifyField() method
- DBy directly assigning a new value
8.Which method is used to invoke a method of a class using reflection?
- AexecuteMethod()
- BcallMethod()
- Cinvoke()
- DinvokeMethod()
9.What is required to invoke a private method using reflection?
- AThe method must be public
- BsetAccessible(true) must be called on the Method object
- CThe method must be static
- DNothing special is required
10.Which of the following is a potential drawback of using reflection?
- ACompile-time type safety
- BReduced performance
- CIncreased security
- DIncreased performance
11.Can reflection be used to change the value of a final field?
- AYes, always
- BOnly if the field is static
- CYes, but it may not work as expected
- DNo, never
12.Which package contains the classes for the Reflection API?
- Ajava.util
- Bjava.io
- Cjava.reflection
- Djava.lang.reflect
13.What is the use of the Method class in reflection?
- ATo access and invoke class methods
- BTo access class fields
- CTo access class constructors
- DTo load classes
14.Which method can be used to get all methods declared in a class, including private methods?
- AgetPrivateMethods()
- BgetDeclaredMethods()
- CgetAllMethods()
- DgetMethods()
15.What is a common use case for the Java Reflection API?
- ACompiling Java code
- BDebugging applications
- CCreating dynamic proxies
- DEncrypting data