wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes while( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "while( )" Watch "while( )" New topic
Author

while( )

Shilpa Shanbhag
Greenhorn

Joined: Mar 06, 2002
Posts: 3
Hi Everyone !
This is a question from Ashoka Mock Exam http://www.geocities.com/ashok_12_66/exam_applet.htm
What will happen if you try to compile and run this code ?
1. public class Condition {
2. static boolean condition = true;
3. public static void main ( String [ ] args) {
4. int i = 0 ;
5. if (condition) i++ ;
6. while (condition) {
7. i++ ;
8. if (i>4) break ;
9. }
10. System.out.println(i) ;
11. }
A. The code does not compile.
B. If condition in line 5 is replaced by false the code will not compile.
C. If condition in line 6 is replaced by false the code will not compile.
D. If break in line 8 is replaced by return the output will be 5.
E. The output of this program will be 5.
The answer given is C and E.
My answer was E. I tried compiling the code using
while(!condition) {.....} and the code compile.
Do they have questions like these in the real exam ?
Shilpa
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
dont replace it with !condition .
replace it with false :
while(false).
then it wont compile and C and E are cthe correct answers.
Shilpa Shanbhag
Greenhorn

Joined: Mar 06, 2002
Posts: 3
I just got the answer....anyways THANX for the reply Roy !
Shilpa
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by swati gupta:
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.

condition is a variable. false is a literal. It is quite possible that !condition would return true if some assignment had taken place earlier that would have set condition to false. However, there is no case where the literal false could be true. Therefore, that will give you a compile-time error.
I hope that helps,
Corey


SCJP Tipline, etc.
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
Thanks a lot! Now i understand this.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by swati gupta:
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.

While (!condition) is evaluated at runtime, there is no guarantee that condition won't be changed by some assignment
while (false) is evaluated at compile time, so you know from the beginning that your code will be unreachable.
I hope it helped you!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
Thanks a lot.
I remember reading something similar (where hardcoding the value causes the compiler to check the value) in Khalid's Mugahl book but don't remember it was in which topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while( )
 
Similar Threads
A SCJP exam question (about array variable assign)
statement not reachable??
static variables
SCJP 5: Mock questions
Help.....Questions from Mock Exam