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.


How are final variables different from constants in Java?

  1. static

  2. const

  3. final

  4. immutable

The correct answer is: const

The correct keyword to ensure a variable retains its initial value during runtime in Java is "const". Option A, "static", is used to create class variables rather than constant variables. Option C, "final", can be used to create a constant variable, but it allows the value to be changed at runtime using reflection. Option D, "immutable", is not a Java keyword and thus, is incorrect. Therefore, option B is the most suitable keyword for this purpose.