Java Programming - Assertions - Discussion

Discussion Forum : Assertions - Pointing out the correct statements (Q.No. 5)
5.
Which of the following statements is true?
In an assert statement, the expression after the colon ( : ) can be any Java expression.
If a switch block has no default, adding an assert default is considered appropriate.
In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
It is appropriate to handle assertion failures using a catch clause.
Answer: Option
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.

Discussion:
5 comments Page 1 of 1.

Achu said:   4 years ago
It is incorrect because the expression after the colon must have a value. Can anyone show an example?

Ford Prefect said:   9 years ago
What Jirka said. A is a correct answer to this because Java does not support void expressions.

Daniil said:   1 decade ago
About D:

You can fine process an error in try/catch clause.

Just write "catch (Error e)" or "catch (Throwable e)" but not "catch (Exception e)".

Jirka said:   1 decade ago
According to the official Java tutorial every expression returns a value, therefore option A is correct. If option A shall be incorrect, it shall use the term "statement" instead of expression.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

Himani said:   1 decade ago
Please tell me the meaning of assert in java.

Post your comments here:

Your comments will be displayed after verification.