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.


Can 'return' statements be used in case statements without a default case?

  1. Yes, without any complaints

  2. Yes, but with compiler complaints

  3. No, it's not allowed

  4. It depends on the enum

The correct answer is: Yes, but with compiler complaints

The return statement can be used in case statements, even without a default case, but the compiler will issue a warning if the case statements do not cover all possible values. This is because without a default case, there is a possibility that not all values will be accounted for and the program may not function correctly. Therefore, it is considered good practice to have a default case in case statements. Answer B is correct because although the return statement can be used, the compiler will issue a warning. Options A, C, and D do not accurately describe the use of return statements in case statements. Option A is incorrect because although the return statement can be used, the compiler may still issue a warning. Option C is incorrect because return statements can be used in case statements. Option D is incorrect because whether or not the return statement can be used depends on the situation and not just the enum.