This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding assertion" Watch "Regarding assertion" New topic

Regarding assertion

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
public class Asserttest2
public static void main(String[] args)

for(int i=0;i<10;i++){
assert (i%2==0):i--;
}catch(AssertionError e){
System.out.println("odd"+ ++i);

choices are:

1)it will print odd and even numbers from 0 to 9 corectly(0 even and 1 odd)
2)it will print odd and even numbers from 0 to 9 incoreectly(0 odd and 1 even.
3)compiler error at line 9
4)compiler error at line 10
5)it will result in infinite loop.

when i run the program,i got the result as


I am totally confused.How it works. the output does not matches the choices also.please explain.
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Pandoo,

Let me first tell you the correct answer is choice 1
and secondly output is
Even 0
odd 1
Even 2
odd 3
Even 4
odd 5
Even 6
odd 7
Even 8
odd 9

The reason you got the output that you have typed in your message is because
you ran your program using java Asserttest2 which has Assertion set to DISABLED by default.
Hence for assertions to work run your program using command java -ea Asserttest2 which will enable the assertions and you will get the expected output.


Thanks,<br />-Rancy
B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
i am getting the same output even if assertions are enabled
Amresh Singh

Joined: Nov 12, 2006
Posts: 8
I checked this with -ea option in Eclipse IDE, by entering -ea option in VM arguments block...It's working fine i.e. Even0 Odd1........
B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37

sorry i ran the program with assertion enabled in Program argument block in eclipse.
May i know the difference in giving the arguments in program arg block and VM arg block in eclipse?
I agree. Here's the link:
subject: Regarding assertion
It's not a secret anymore!