Java Programming - Assertions - Discussion
Discussion Forum : Assertions - Pointing out the correct statements (Q.No. 5)
5.
Which of the following statements is true?
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.
Himani said:
1 decade ago
Please tell me the meaning of assert in java.
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.
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.
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)".
You can fine process an error in try/catch clause.
Just write "catch (Error e)" or "catch (Throwable e)" but not "catch (Exception e)".
Ford Prefect said:
9 years ago
What Jirka said. A is a correct answer to this because Java does not support void expressions.
Achu said:
4 years ago
It is incorrect because the expression after the colon must have a value. Can anyone show an example?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers