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