• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"assert i%2==0 : i--;"question required

 
VINCE CARTER
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)

1.public class AssertTest
2.{
3.public static void main(String args[])
4.{
5.for(int i=0;i <10;i++)
6.{
7.try
8.{
9.assert i%2==0 : i--;
10.System.out.println("Even number : " + i);
11.}
12.catch(AssertionError ae)
13.{
14.System.out.println("Odd number : " + ++i);
15.}
16.}
17.}
18.}
Answer:It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd).
Why ?
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. for(int i=0;i <10;i++)
6. {
7. try
8. {
9. assert i%2==0 : i--;
10. System.out.println("Even number : " + i);
11. }
12. catch(AssertionError ae)
13. {
14. System.out.println("Odd number : " + ++i);
15. }
16. }
17. }
18. }


Iteration 1:
i = 0
line 9: 0%2 == 0 - true - no assertion error thrown.

Iteration 2:
i=1
line 9: 1%2 == 0 - FALSE - assertion error thrown
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding something to the last reply........

Iteration 1:
i = 0
line 9: 0%2 == 0 - true - no assertion error thrown.

Iteration 2:
i=1
line 9: 1%2 == 0 - FALSE - assertion error thrown
line 12: Assertion Error has been caught and ++i has been printed...means i=1.

Ater the normal loop operation starts again......
Iteration 3:
i=2
line 9: 2%2 == 0 - true - no assertion error thrown.

Iteration 4:
i=3
line 12: Assertion Error has been caught and ++i has been printed...means i=3.

This way you can find the other iterations also..

very nice question.....

Kaps
 
amitkumar kamboj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there

actually logically your program is wrong
because the class Error and its subclass in java is not mean to catch them they represent the error in your code

and it is poor programming practice to catch the Runtime Exceptions and Error's

you should only catch the check Exception i.e. include IOException
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Amit you are right that its not a good programming practice. But if you study Assertions, you will find that we can catch AssertionError if thrown in the code. So, if Sun Exam developer thinks that the examinee should know this fact then they are going to test your knowledge.

So, you should atleast know this concept.

Kaps
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
poor programming practice to catch the Runtime Exceptions and Error's


Agreed but there are exceptions to this rule. The problem is NumberFormatException is a RuntimeException, and this exception is often thrown when parsing user input (Integer.parseInt(args[0]) for example). So a RuntimeException is not ALWAYS caused by a programming error as such. If you do not catch this exception, and display a meaningful error message, the clueless user is going to be confused by a possibly complex stack trace.
 
Tom Screen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I decided it was time for me to register just to add a reply to this thread! I was wondering where you came across this piece of code. If you are studying for your SCJP exam then you should really be aware that this code flaunts the 'good practice' guidelines of assertions - that is they should not produce a side effect. In this case your assertion will decrement the i variable if the condition of the assertion evaluates to false.

Hope this helps too!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom said:
I was wondering where you came across this piece of code


Unfortunately, VINCE CARTER (and his namesake PETER CARTER) has not responded to our requests to quote the source of his code when he posts

I'm pondering on the option to close his questions if he continues.

Anyway, welcome to Javaranch Tom! Keep on postin'
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amitkumar kamboj:
it is poor programming practice to catch the Runtime Exceptions


Why???
 
VINCE CARTER
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even number : 0
Odd number : 1
Even number : 2
Odd number : 3
Even number : 4
Odd number : 5
Even number : 6
Odd number : 7
Even number : 8
Odd number : 9

Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic