• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions mock exam question

 
Poonam Thawani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0): "assertion failed";
5. System.out.println("finished");
6. }
7. }
What is the result and why?
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is output.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is C. once AssertionError is thrown the string given in the assert's 2nd statement will be printed
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes answer is C

Got
Exception in thread "main" java.lang.AssertionError: assertion failed
at Test.main(Test.java:4)
When run in -ea mode and compiled with -source 1.4

Printed Finished
When run in -da mode

ea stand for enable assertion and da stands for disable assertion.
[ December 29, 2005: Message edited by: jiju ka ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic