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 1 of 2)

  1. Which of the following describes the correct use of MethodHandles.lookup() to access a class’s private method?Multiple choiceHard
  2. Given using Class.forName("com.example.MyClass") in Java, what outcome should you expect?Multiple choiceMedium
  3. Which method would you use to check if a class is an interface using Reflection?Multiple choiceEasy
  4. In what scenario would you prefer reflection for method invocation over direct method calls?Multiple choiceMedium
  5. Which of the following is NOT a drawback of using Java Reflection?Multiple choiceMedium
  6. If you use Method.invoke() to call a private method, what must you do first?Multiple choiceHard
  7. How does Java's reflection API impact type safety?Multiple choiceMedium
  8. Why do teams use the Method.invoke() in Java's reflection API in production code?Multiple choiceEasy
  9. How does MethodHandles.guardWithTest improve performance compared to traditional if-else statements?Multiple choiceMedium
  10. Why does MethodHandles.Lookup in the Java metaprogramming context matter in this design?Multiple choiceMedium
  11. What role does the MethodHandles.Lookup class play when working with MethodHandles?Multiple choiceMedium
  12. How does the JVM optimize MethodHandles for better performance compared to reflection?Multiple choiceMedium
  13. When using Method.invoke() to call a method on an object, which of the following statements is true regarding the performance and type safety of the operation?Multiple choiceMedium
  14. How would you explain a key advantage of using dynamic class loading for URL routing in a mini-framework in an interview?Multiple choiceHard
  15. Consider a method that changes its signature dynamically. How can Method.invoke() still be used to call this method?Multiple choiceHard
  16. How would you explain a major concern when dynamically invoking methods using Method.invoke() in a performance-critical application in an interview?Multiple choiceHard
  17. In what scenario might Reflection be necessary despite its performance overhead?Multiple choiceMedium
  18. Which of the following best describes the use of an Annotation Processor in Java?Multiple choiceHard
  19. How can you determine the parameter types of a method obtained via getDeclaredMethods()?Multiple choiceMedium
  20. How would you explain a common use case for reflection in Java applications in an interview?Multiple choiceEasy
  21. How can a mini-framework dynamically invoke a private method 'calculateDiscount' in an e-commerce application?Multiple choiceHard
  22. Given a class 'Product' with a private field 'price', how can you dynamically change its value using reflection?Multiple choiceMedium
  23. What must you check after invoking a method using Method.invoke() to handle its return value correctly?Multiple choiceMedium
  24. How would you explain a key advantage of using MethodHandles over the traditional reflection method invocation in a Java interview?Multiple choiceEasy
  25. Which of the following is NOT a valid use of MethodHandles?Multiple choiceEasy
  26. Why does the MethodHandles.publicLookup() method matter in practice?Multiple choiceEasy
  27. Why does the java.lang.reflect package in Java matter in this design?Multiple choiceEasy
  28. Compared with Method.invoke, what is a major advantage of MethodHandle?Multiple choiceHard
  29. When is runtime scanning with reflection still a good fit?Multiple choiceMedium
  30. How does Reflection handle method overloading when invoking a method?Multiple choiceMedium
  31. Why is it advisable to minimize the use of Reflection in performance-critical applications?Multiple choiceHard
  32. When using MethodHandle.invokeExact, why is it necessary to match the method signature exactly?Multiple choiceHard
  33. Which of the following is a correct way to access a private field 'id' of an object using Reflection?Multiple choiceMedium
  34. Why cache reflective metadata into route registries or handler objects after startup?Multiple choiceHard
  35. What happens if Method.invoke() is used on a method that throws a checked exception?Multiple choiceMedium
  36. What does the MethodType.methodType(String.class, String.class) represent in the context of MethodHandles?Multiple choiceMedium
  37. Why do teams use setAccessible(true) in Java reflection in production code?Multiple choiceEasy
  38. In the context of Java reflection, what is the primary purpose of calling setAccessible(true) on a Field object?Multiple choiceMedium
  39. Which Reflection method would you use to obtain a class’s superclass?Multiple choiceMedium
  40. How would you explain a potential security risk of using 'setAccessible(true)' with Reflection in an interview?Multiple choiceHard
  41. Why do teams use Reflection in Java frameworks in production code?Multiple choiceMedium
  42. What does the method call 'setAccessible(true)' achieve when applied to a Field object in Java?Multiple choiceEasy
  43. What does getDeclaredMethods() return when called on a class?Multiple choiceEasy
  44. What does a MethodType represent when working with MethodHandles?Multiple choiceMedium
  45. Why does MethodHandles.collectArguments when working with MethodHandles matter in practice?Multiple choiceEasy
  46. Which MethodHandles.Lookup method is typically used to access public methods across different classes?Multiple choiceEasy
  47. What does the Method.invoke() method do in the Reflection API?Multiple choiceEasy
  48. What string is produced when this MethodHandle invokes String.concat?Multiple choiceMedium
  49. Which access level for MethodHandle objects created using MethodHandles.publicLookup() is the default in Java?Multiple choiceEasy
  50. How do mature frameworks usually use reflection in request handling?Multiple choiceEasy