Multiple choice

Which of the following statements is true?

  1. In an assert statement, the expression after the colon ( : ) can be any Java expression.

  2. If a switch block has no default, adding an assert default is considered appropriate.

  3. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.

  4. It is appropriate to handle assertion failures using a catch clause.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Adding an assertion statement to a switch statement that previously had no default case is considered an excellent use of the assert mechanism. Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal. Option C is incorrect because the expression after the colon must have a value. Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.