wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Assertion" Watch "Doubt in Assertion" New topic
Author

Doubt in Assertion

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Hi,
This is another question from mughal book :roll:

Which statements are true regarding the execution of the following code?

public class Q3a0a {
public static void main(String[] args) {
int j = 5;

for (int i = 0; i<j; i++) {
assert i < j-- : i > 0;
System.out.println(i*j);
}
}
}

Select the two correct answers.

a)An AssertionError will be thrown if assertions are enabled at runtime.

b)The last number printed is 4 if assertions are disabled at runtime.

c)The last number printed is 20 if assertions are disabled at runtime.

d)The last number printed is 4 if assertions are enabled at runtime.

e)The last number printed is 20 if assertions are enabled at runtime.

answers are: c, d
Why answer a is not correct?
Than you


SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
If you compile and run the program with assertions turned on you'll see that a is not correct, since no AssertionError ever gets thrown. The reason that it never gets thrown is that each time the program execution begins an iteration through the for loop, it must first be true that i < j. Then the first statement in the for loop checks the truth value of the statement i < j-- which will always yield the same result as the previous test i < j because the postfix decrement operator causes j-- to be evaluated to the original value of j before the value of j is changed to a lower number.

For example, the first time through the loop, i = 0 and j = 5. i < j evaluates to 0 < 5 which evaluates to true. i < j-- evaluates to 0 < 5 which evaluates to true, after which the value of j becomes 4. After a couple more iterations, i = 2 and j = 3, so i < j evaluates to 2 < 3 which evaluates to true, so we enter the loop again. i < j-- evaluates to 2 < 3 which evaluates to true, and then the value of j becomes 2. On the next iteration, i = 3 and j = 2, so i < j evaluates to 3 < 2 which evaluates to false, so the body of the loop does not execute. We exit the loop and the program terminates.


SCJA 1.0 (98%), SCJP 1.4 (98%)
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Hi Joseph,

I understood why an assertion error will not be thrown from your explanation, but why the answers c & d are correct?How do we get these values as outputs?
Shrikant Pandhare
Greenhorn

Joined: Jul 29, 2005
Posts: 15
hey ..
as joseph explained if u trace through the loop values u will fine if assertion is enabled .. j is decremented and i is incremented so 4 is last values printed.. But if assertion is disabled then assert statement is not executed .. meaning j is not decremented and hence last value is 20
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi ,

i have one doubt ....when assertions are enabled then its ok ..answer is 4..but when they are disabled what happens exaclty ..how does the compiler treat the assert .......will it ignore r what .....

thanks & regards
srikanth


Thanks & Regards<br /> <br />-Srikanth
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Originally posted by srikanth reddy:
hi ,

i have one doubt ....when assertions are enabled then its ok ..answer is 4..but when they are disabled what happens exaclty ..how does the compiler treat the assert .......will it ignore r what .....

thanks & regards
srikanth


Assertions are disabled by default (until they are enabled), this code will run as though it was written like:

public class Q3a0a {
public static void main(String[] args) {
int j = 5;

for (int i = 0; i<j; i++) {
System.out.println(i*j);
}
}
}
cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Assertion