Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What is a drawback of using reflection in Java?

  1. It can only call public methods

  2. It is slower than direct method calls

  3. It cannot access private fields

  4. It is not supported in modern Java versions

The correct answer is: It is slower than direct method calls

Reflection allows for the inspection and manipulation of code at runtime, which can be useful in some cases. However, it comes with a performance cost as it involves additional layers of abstraction and dynamic method lookups. Therefore, option B is correct as it states that a drawback of using reflection in Java is that it is slower than direct method calls. While option A, C, and D are incorrect because they do not identify a drawback of using reflection in Java. Option A is incorrect because reflection can actually access and call both public and private methods. Option C is incorrect because reflection does have the ability to access and modify private fields, although it requires additional permissions. Finally, option D is also incorrect as reflection is still supported in modern Java versions, although there are other alternatives that may be preferred in certain situations.