File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Question from Dan's exam 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 "Assertion Question from Dan Watch "Assertion Question from Dan New topic
Author

Assertion Question from Dan's exam

Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
class B {
private static int x1;
public void setX(int x) {x1 = x;}
public int getX() {return x1;}
public static void main(String[] args) {
int i1 = 0, j1 = 0;
do {
System.out.print(j1++);
assert (i1 = j1 + x1) < 6; //line 1
} while (i1 < 3);
}}
The correct ans are
1. With assertions enabled it prints 012.
2.With assertions disabled it attempts to print an infinite sequence of numbers.
3.As a rule, the boolean expression of an assert statement should not be used to perform actions that are required for normal operation of the program.
I understood ans 3. but not the other two. Could anyone explain it to me?
Also, please let me know how line 1 is executed.


Thanks,<br />Lakshmi.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Lakshmi -

For answer 2:
The code has a do-while loop that exits when i1 is no longer less than 3. Within the loop, i1 is not changed, so it will "do" the loop infinitely. (Only j1 is changed inside the loop without assertions.)
For answer 1:
Is there more code in the question? Without knowing what x is, if it's being manipulated on other parts of the code, I can't give an explanation of this part.
Hope that helps!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Jeff,
x is declared as private static , (2nd line). Other than that no other info about x is given..
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
With assertions enabled, the line gets executed, so i1 is incremented every single loop cycle and checked against 6. Once the condition of while loop is not satisfied any more (i1==3), the control gets out of the loop. Line 1 is used as assignment statement for i1 and overall as a boolean check. With assertions disabled, as Jeff said, the loop will never end. A perfect example how not to use the assertions mechanism.
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
thank you...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion Question from Dan's exam