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)
- Which of the following describes the correct use of MethodHandles.lookup() to access a class’s private method?Multiple choiceHard
- Given using Class.forName("com.example.MyClass") in Java, what outcome should you expect?Multiple choiceMedium
- Which method would you use to check if a class is an interface using Reflection?Multiple choiceEasy
- In what scenario would you prefer reflection for method invocation over direct method calls?Multiple choiceMedium
- Which of the following is NOT a drawback of using Java Reflection?Multiple choiceMedium
- If you use Method.invoke() to call a private method, what must you do first?Multiple choiceHard
- How does Java's reflection API impact type safety?Multiple choiceMedium
- Why do teams use the Method.invoke() in Java's reflection API in production code?Multiple choiceEasy
- How does MethodHandles.guardWithTest improve performance compared to traditional if-else statements?Multiple choiceMedium
- Why does MethodHandles.Lookup in the Java metaprogramming context matter in this design?Multiple choiceMedium
- What role does the MethodHandles.Lookup class play when working with MethodHandles?Multiple choiceMedium
- How does the JVM optimize MethodHandles for better performance compared to reflection?Multiple choiceMedium
- 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
- How would you explain a key advantage of using dynamic class loading for URL routing in a mini-framework in an interview?Multiple choiceHard
- Consider a method that changes its signature dynamically. How can Method.invoke() still be used to call this method?Multiple choiceHard
- How would you explain a major concern when dynamically invoking methods using Method.invoke() in a performance-critical application in an interview?Multiple choiceHard
- In what scenario might Reflection be necessary despite its performance overhead?Multiple choiceMedium
- Which of the following best describes the use of an Annotation Processor in Java?Multiple choiceHard
- How can you determine the parameter types of a method obtained via getDeclaredMethods()?Multiple choiceMedium
- How would you explain a common use case for reflection in Java applications in an interview?Multiple choiceEasy
- How can a mini-framework dynamically invoke a private method 'calculateDiscount' in an e-commerce application?Multiple choiceHard
- Given a class 'Product' with a private field 'price', how can you dynamically change its value using reflection?Multiple choiceMedium
- What must you check after invoking a method using Method.invoke() to handle its return value correctly?Multiple choiceMedium
- How would you explain a key advantage of using MethodHandles over the traditional reflection method invocation in a Java interview?Multiple choiceEasy
- Which of the following is NOT a valid use of MethodHandles?Multiple choiceEasy
- Why does the MethodHandles.publicLookup() method matter in practice?Multiple choiceEasy
- Why does the java.lang.reflect package in Java matter in this design?Multiple choiceEasy
- Compared with Method.invoke, what is a major advantage of MethodHandle?Multiple choiceHard
- When is runtime scanning with reflection still a good fit?Multiple choiceMedium
- How does Reflection handle method overloading when invoking a method?Multiple choiceMedium
- Why is it advisable to minimize the use of Reflection in performance-critical applications?Multiple choiceHard
- When using MethodHandle.invokeExact, why is it necessary to match the method signature exactly?Multiple choiceHard
- Which of the following is a correct way to access a private field 'id' of an object using Reflection?Multiple choiceMedium
- Why cache reflective metadata into route registries or handler objects after startup?Multiple choiceHard
- What happens if Method.invoke() is used on a method that throws a checked exception?Multiple choiceMedium
- What does the MethodType.methodType(String.class, String.class) represent in the context of MethodHandles?Multiple choiceMedium
- Why do teams use setAccessible(true) in Java reflection in production code?Multiple choiceEasy
- In the context of Java reflection, what is the primary purpose of calling setAccessible(true) on a Field object?Multiple choiceMedium
- Which Reflection method would you use to obtain a class’s superclass?Multiple choiceMedium
- How would you explain a potential security risk of using 'setAccessible(true)' with Reflection in an interview?Multiple choiceHard
- Why do teams use Reflection in Java frameworks in production code?Multiple choiceMedium
- What does the method call 'setAccessible(true)' achieve when applied to a Field object in Java?Multiple choiceEasy
- What does getDeclaredMethods() return when called on a class?Multiple choiceEasy
- What does a MethodType represent when working with MethodHandles?Multiple choiceMedium
- Why does MethodHandles.collectArguments when working with MethodHandles matter in practice?Multiple choiceEasy
- Which MethodHandles.Lookup method is typically used to access public methods across different classes?Multiple choiceEasy
- What does the Method.invoke() method do in the Reflection API?Multiple choiceEasy
- What string is produced when this MethodHandle invokes String.concat?Multiple choiceMedium
- Which access level for MethodHandle objects created using MethodHandles.publicLookup() is the default in Java?Multiple choiceEasy
- How do mature frameworks usually use reflection in request handling?Multiple choiceEasy