Skip to content
Sharp

Reflection API interview questions

Breaking Encapsulation, Dynamic Class Loading, Method.invoke, MethodHandles

77 questions with answers and explanations · 77 flashcards

Questions (page 2 of 2)

  1. Why is Method.invoke() considered less type-safe compared to direct method calls?Multiple choiceEasy
  2. Which method is used to retrieve all declared methods of a class using Reflection?Multiple choiceEasy
  3. What exception must you handle or declare when using Method.invoke()?Multiple choiceMedium
  4. Which of the following methods would you use to obtain a Field object for a private field 'username' in a class User using reflection?Multiple choiceEasy
  5. How does using getDeclaredConstructor().newInstance() differ from using new MyClass()?Multiple choiceMedium
  6. Why is letting reflective calls spread through core business logic an architectural risk?Multiple choiceMedium
  7. What happens if you try to use Reflection to access a private field in a class without calling 'setAccessible(true)'?Multiple choiceHard
  8. Which Java package contains the classes for Reflection?Multiple choiceEasy
  9. Which of the following MethodHandles.Lookup methods is used to create a MethodHandle for a private method in a class?Multiple choiceMedium
  10. How would you explain a major reason for the performance overhead when using reflection in an interview?Multiple choiceMedium
  11. How can reflection be used to call a method with a signature that has changed dynamically?Multiple choiceHard
  12. Given a class instance, how can you obtain all methods declared in that class using reflection?Multiple choiceMedium
  13. Which Java Reflection API method can be used to get all declared methods of a class, including private methods?Multiple choiceEasy
  14. In which scenario would you need to use setAccessible(true) before calling Method.invoke()?Multiple choiceMedium
  15. What does invoking getDeclaredFields() on a class evaluate to?Multiple choiceMedium
  16. Why are MethodHandles generally preferred over traditional reflection methods in performance-critical applications?Multiple choiceEasy
  17. Which of the following is a consequence of using reflection to invoke methods in Java?Multiple choiceMedium
  18. Why does an annotation processor like Lombok in Java matter in this design?Multiple choiceHard
  19. How does MethodHandle.invokeExact differ from MethodHandle.invoke in terms of method signature compatibility?Multiple choiceHard
  20. How does using annotations with RetentionPolicy.RUNTIME differ from RetentionPolicy.SOURCE?Multiple choiceMedium
  21. Why would you use MethodHandles.collectArguments when working with MethodHandles?Multiple choiceHard
  22. When using Method.invoke(), what is the effect of passing null as the first argument?Multiple choiceHard
  23. When building a mini-framework, why might you use MethodHandles.lookup() instead of reflection?Multiple choiceHard
  24. What potential issue arises when using setAccessible(true) in frameworks mapping private database fields?Multiple choiceHard
  25. How would you use MethodHandles.Lookup to access a private field in a class?Multiple choiceHard
  26. When using Reflection to access a private field in a superclass, which method should you use?Multiple choiceHard
  27. How can frameworks dynamically map URL routes to private method handlers using Java Reflection?Multiple choiceMedium