Home / Java Programming / Assertions :: Discussion

Discussion :: Assertions

  1. Which of the following statements is true?

  2. A.
    In an assert statement, the expression after the colon ( : ) can be any Java expression.
    B.
    If a switch block has no default, adding an assert default is considered appropriate.
    C.
    In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
    D.
    It is appropriate to handle assertion failures using a catch clause.

    View Answer

    Workspace

    Answer : Option B

    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.


Be The First To Comment