This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion question" Watch "Assertion question" New topic
Author

Assertion question

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477

Assuming that the setX method is never invoked, which statements are true?
a. With assertions enabled it prints 012345 followed by an AssertionError message.
b. With assertions enabled it prints 012.
c. With assertions disabled it prints: 012345
d. With assertions disabled it prints: 012
e. With assertions disabled it attempts to print an infinite sequence of numbers.
f. With assertions disabled the variable i1 is incremented with each pass through the loop.
g. 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.
B,e,g are correct.
But my ananlysis says b and d are correct.

Can anyone please explain e an g options for me.(step by step if possible)
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
With assertion disabled, the assert line will not be executed so i1 will not be incremented by 1. This will make do-while loop run infinite times. So option e is correct.

Option b is correct wen assertion is enabled.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
With Assertions disabled, assert (i1 = j1 + x1) < 6 is just ignored at run time, so it is equivalent to commenting that line.

do {
System.out.print(j1++);
// assert (i1 = j1 + x1) < 6;
} while (i1 < 3);}}
In this code, i1 is never modified, so while condition is always true and it loops infinite times.

option g is also correct since, assertions might be disabled at runtime, hence it should not include any code that must be performed in a normal execution.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thank you very much naseem and pramila.I got it on your help.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Pramila! i have a question for you.Have you formed any study group of SCJP1.4 preparation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion question
 
Similar Threads
Strange Assertion Behaviour
Assertion problem
Assertion Question from Dan's exam
dan exam doubt 12
Assertions