Reflection API

1.What is the primary purpose of the Java Reflection API?
  • A. To allow a program to examine or modify its own structure and behavior at runtime.
  • B. To provide a mechanism for inter-process communication.
  • C. To manage memory allocation for objects.
  • D. To encrypt and decrypt data securely.

To allow a program to examine or modify its own structure and behavior at runtime.

To allow a program to examine or modify its own structure and behavior at runtime.

2.Which class is used to get information about a class at runtime?
  • A. Object
  • B. Class
  • C. Metadata
  • D. Info

Class

Class

3.How can you create an instance of a class using reflection?
  • A. Using the new keyword directly.
  • B. Using Class.newInstance() or Constructor.newInstance().
  • C. By directly manipulating the bytecode.
  • D. Reflection does not allow object instantiation.

Using Class.newInstance() or Constructor.newInstance().

Using Class.newInstance() or Constructor.newInstance().

4.Which method is used to invoke a private method of a class using reflection?
  • A. invokePrivateMethod()
  • B. getMethod() with setAccessible(true)
  • C. callPrivate()
  • D. Reflection cannot invoke private methods.

getMethod() with setAccessible(true)

getMethod() with setAccessible(true)

5.What is a common use case for the Reflection API?
  • A. To write highly optimized arithmetic operations.
  • B. To develop frameworks like Spring, JUnit, and ORMs.
  • C. To manage database connections efficiently.
  • D. To create graphical user interfaces.

To develop frameworks like Spring, JUnit, and ORMs.

To develop frameworks like Spring, JUnit, and ORMs.