Introduction to Advanced Java Reflection Techniques
Explore the basics of using Java Reflection to inspect and manipulate classes at runtime.
Questions in this quiz
15 questions on Introduction to Advanced Java Reflection Techniques. Work through them here, then start the quiz to answer them and see your score.
1.What is Java Reflection primarily used for?
- ATo encrypt Java applications
- BTo compile Java code
- CTo inspect classes, methods, and fields at runtime
- DTo manage memory allocation
2.Which class in Java is commonly used to obtain metadata about a class at runtime?
- AObject
- BClass
- CRuntime
- DClassLoader
3.How can you obtain a Class object for a given class in Java?
- AUsing System.getClass()
- BUsing the 'new' keyword
- CUsing Class.forName()
- DUsing Object.getClass()
4.Which method is used to get all declared methods of a class using reflection?
- AgetClassMethods()
- BgetDeclaredMethods()
- CgetAllMethods()
- DgetMethods()
5.What does the getFields() method return when used with reflection?
- AAll fields except inherited ones
- BOnly public fields
- CAll fields including private
- DOnly static fields
6.Which of the following is true about invoking a method using reflection?
- AYou cannot pass parameters
- BYou must know the method name at compile time
- CYou can invoke private methods
- DIt is faster than direct method calls
7.What exception is thrown if the class name is not found when using Class.forName()?
- ANoSuchMethodException
- BIllegalAccessException
- CInstantiationException
- DClassNotFoundException
8.How can you change the value of a private field using reflection?
- AUsing setFieldValue()
- BUsing setAccessible(true) on the field
- CUsing setPrivateField()
- DUsing changeField()
9.Which method retrieves the superclass of a class using reflection?
- AgetBaseClass()
- BgetSuperclass()
- CgetParentClass()
- DgetInheritedClass()
10.What is a potential downside of using reflection in Java?
- AEnhanced readability
- BCompile-time type safety
- CIncreased performance
- DSecurity restrictions
11.Which method is used to create a new instance of a class using reflection?
- AcreateInstance()
- BmakeInstance()
- Cinstantiate()
- DnewInstance()
12.What is required to access a private method using reflection?
- AMethod must be public
- BsetAccessible(true) must be called
- CMethod must be static
- DMethod must be final
13.Can reflection be used to change the value of a final field?
- AYes, always
- BNo, never
- CYes, but it requires special handling
- DOnly if the field is static
14.Which package contains the classes for Java Reflection?
- Ajava.lang.reflect
- Bjava.reflect
- Cjava.util
- Djava.io
15.What is the purpose of the Method.invoke() method in reflection?
- ATo compile a method
- BTo rename a method
- CTo delete a method
- DTo execute a method