MCQs on Java Reflection API | Inspecting Java Classes

Understanding the Java Reflection API is essential for inspecting and manipulating classes, methods, fields, and annotations at runtime. These MCQs cover the fundamentals of Reflection, its usage, and key concepts like dynamic method invocation and class metadata inspection.


Introduction to Reflection

  1. What is Java Reflection API used for?
    • a) To compile Java code dynamically
    • b) To allow runtime inspection and manipulation of classes, methods, and fields
    • c) To improve the performance of Java code
    • d) To provide security features
  2. Which class is used as the entry point for Reflection in Java?
    • a) Method
    • b) Field
    • c) Class
    • d) Object
  3. What does the Class.forName() method do in Java Reflection?
    • a) Returns the name of the class
    • b) Loads the class into memory
    • c) Creates an instance of the class
    • d) Finds a method by its name
  4. Which method of the Class class can be used to obtain the name of a class?
    • a) getName()
    • b) getClass()
    • c) getSimpleName()
    • d) getCanonicalName()
  5. Which of the following methods can be used to get a Constructor object for a class using Reflection?
    • a) getConstructor()
    • b) getDeclaredConstructor()
    • c) getMethods()
    • d) Both a and b

Reflecting on Class Metadata

  1. Which method is used to retrieve all methods declared in a class using Reflection?
    • a) getDeclaredMethods()
    • b) getMethods()
    • c) getDeclaredField()
    • d) getDeclaredConstructors()
  2. How can you retrieve the superclass of a class using Reflection?
    • a) getSuperclass()
    • b) getParent()
    • c) getSuper()
    • d) getSuperClass()
  3. What does the isInstance() method of the Class class check?
    • a) Whether an object is an instance of the class
    • b) Whether a method exists in the class
    • c) Whether a class is a subclass of another class
    • d) Whether the class is an interface
  4. Which method is used to check if a class has a specified method in Java Reflection?
    • a) getMethod()
    • b) getDeclaredMethod()
    • c) hasMethod()
    • d) isMethodPresent()
  5. Which of the following methods can be used to retrieve the modifiers of a class (e.g., public, private)?
    • a) getModifiers()
    • b) getDeclaredModifiers()
    • c) getAccessModifiers()
    • d) getVisibility()

Dynamic Method Invocation

  1. Which method of the Method class is used to invoke a method dynamically via Reflection?
    • a) invoke()
    • b) call()
    • c) execute()
    • d) run()
  2. How can you pass parameters to a method dynamically when using Reflection?
    • a) By passing arguments as an array to invoke()
    • b) By setting parameters directly
    • c) By using the Method.setParams() method
    • d) By using the Method.invokeParams() method
  3. What exception is thrown if the number or type of arguments does not match when invoking a method using Reflection?
    • a) NoSuchMethodException
    • b) InvocationTargetException
    • c) IllegalAccessException
    • d) IllegalArgumentException
  4. Which method is used to invoke a constructor dynamically in Java Reflection?
    • a) newInstance()
    • b) invokeConstructor()
    • c) callConstructor()
    • d) createObject()
  5. Which of the following is a valid use case of Java Reflection?
    • a) Dynamically invoking methods based on user input
    • b) Precompiling code for better performance
    • c) Preventing reflection-based security vulnerabilities
    • d) Optimizing garbage collection

Annotations and Reflection

  1. What is the role of the @Retention annotation in Java Reflection?
    • a) To specify how long an annotation is retained in the code
    • b) To define the scope of a variable
    • c) To mark methods for reflection
    • d) To provide security in reflection-based code
  2. Which annotation is used to mark methods for use in Java Reflection?
    • a) @Method
    • b) @Reflectable
    • c) @Override
    • d) @Target
  3. How can you get an annotation from a class using Reflection?
    • a) getAnnotation()
    • b) getAnnotations()
    • c) getDeclaredAnnotation()
    • d) findAnnotation()
  4. How can you check if a class is annotated with a specific annotation using Reflection?
    • a) By calling isAnnotationPresent()
    • b) By calling hasAnnotation()
    • c) By checking @Annotations
    • d) By calling containsAnnotation()
  5. What does the @Target annotation specify in Java Reflection?
    • a) The types of elements an annotation can be applied to (methods, fields, etc.)
    • b) The package the annotation belongs to
    • c) The runtime behavior of the class
    • d) The visibility of an annotation
  6. Which method retrieves the annotations present on a method using Reflection?
    • a) getDeclaredAnnotations()
    • b) getAnnotations()
    • c) getAnnotation()
    • d) getMethods()
  7. What type of value is returned by getDeclaredFields() in Java Reflection?
    • a) The method names of a class
    • b) The fields declared in a class
    • c) The constructors of a class
    • d) The class’s metadata
  8. Can Reflection be used to modify the values of final fields?
    • a) Yes, by using the setAccessible(true) method
    • b) No, final fields are immutable
    • c) Yes, with the Field.set() method
    • d) Only in certain cases when the field is static
  9. What does the setAccessible(true) method allow you to do?
    • a) Access private methods and fields
    • b) Increase the performance of reflection
    • c) Make the method return faster
    • d) Block reflection-based security risks
  10. How can you make a method in a class accessible for Reflection if it is private?
    • a) By using setAccessible(true)
    • b) By using makePrivate()
    • c) By using makeAccessible()
    • d) By using accessMethod()
  11. How does the Field class relate to Reflection in Java?
    • a) It provides methods for accessing and modifying fields of a class
    • b) It defines methods for reflection-based annotations
    • c) It handles method invocation in reflection
    • d) It manages thread synchronization in reflection
  12. What is the main benefit of using Reflection in Java?
    • a) It allows runtime manipulation of classes, methods, and fields
    • b) It speeds up method execution
    • c) It provides security features
    • d) It reduces memory usage
  13. Can Java Reflection be used to access private fields?
    • a) Yes, by using setAccessible(true)
    • b) No, private fields are completely inaccessible
    • c) Yes, but only if the class is part of the same package
    • d) Yes, but only during compile-time
  14. What happens if a class or method does not exist while using Reflection?
    • a) It throws a NoSuchMethodException
    • b) It silently ignores the issue
    • c) It throws an IllegalAccessException
    • d) It returns null
  15. How can Reflection improve flexibility in Java applications?
    • a) By allowing dynamic method invocation and class modification at runtime
    • b) By optimizing code for performance
    • c) By securing the application
    • d) By enforcing code structure

Answer Key

QnoAnswer
1b) To allow runtime inspection and manipulation of classes, methods, and fields
2c) Class
3b) Loads the class into memory
4a) getName()
5d) Both a and b
6a) getDeclaredMethods()
7a) getSuperclass()
8a) Whether an object is an instance of the class
9a) getMethod()
10a) getModifiers()
11a) invoke()
12a) By passing arguments as an array to invoke()
13a) NoSuchMethodException
14a) newInstance()
15a) Dynamically invoking methods based on user input
16a) To specify how long an annotation is retained in the code
17d) @Target
18a) getAnnotation()
19a) By calling isAnnotationPresent()
20a) The types of elements an annotation can be applied to (methods, fields, etc.)
21b) getAnnotations()
22b) The fields declared in a class
23a) Yes, by using the setAccessible(true) method
24a) Access private methods and fields
25a) By using setAccessible(true)
26a) It provides methods for accessing and modifying fields of a class
27a) It allows runtime manipulation of classes, methods, and fields
28a) Yes, by using setAccessible(true)
29a) It throws a NoSuchMethodException
30a) By allowing dynamic method invocation and class modification at runtime

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top