*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple Assignment question!!!! 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 "Simple Assignment question!!!!" Watch "Simple Assignment question!!!!" New topic
Author

Simple Assignment question!!!!

Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
What do u think is the answer to this question(ofcourse without compiling
public class isAssigned
{
public static void main(String[] args)
{
int k,m=6;
while(true)
break;
k=6;
System.out.println(k);
}
}
Will result in
a)Compile time error,cause k has not been initialized.
b)Compile time error,cause break cannot be used in while.
c)Compile time error,cause statement k=6 is not reached.
d)Prints out the value of k,i.e 6.
kumar
Greenhorn

Joined: Jul 31, 2000
Posts: 2
Ofcourse, the answer is d.
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
Surya,
I compiled it! But that was definately not the answer I had expected.
Vivek Namala
Greenhorn

Joined: Jul 07, 2000
Posts: 2
Compiled I got out put as 6.
Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
Me too !! got 6 as the output...
Aman
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
I haven't compiled it but if everybody is getting output as 6, is that because if {} are not given after while(expression), the the immedite next statement is taken to be within the while block.
For me none of the choice seemed plausible, though I'd have opted for C.
Surya, I know you r putting some real brain teasers. Can u pls explain about this one.
Thanks.
[This message has been edited by Harry Chawla (edited August 01, 2000).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The answer is d. You got the reason correct when you mentioned {}. This will clarify what is going on

a is false because with k=6 outside the while block, it's always garantied to be initalized before it is used.
b false exiting a while or for block early is a use for break.
c. false since k=6 is outside the while block, it will always be reached.

[This message has been edited by Carl Trusiak (edited August 01, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
From JLS:
The statement V is definitely assigned after X (where V is a local variable and X is a statement or expression) means V is definitely assigned after X if X completes normally. If X completes abruptly, the assignment may not have occurred.A peculiar consequence of this definition is that V is definitely assigned after break; is always true!Because a break statement never completes normally, it is vacuously true that V has been assigned a value if the break statement completes normally.
So the answer as most of the guys have given as 6 is correct.Answer c would have been correct if you put the braces around the while loop and then it gives the compile time error,statement not reached.
public class isAssigned
{
public static void main(String[] args)
{
int k,m=6;
while(true){
break;
k=6;
System.out.println(k);}
}
}
This results in compile time error statement not reached.

[This message has been edited by Surya B (edited August 01, 2000).]
 
Don't get me started about those stupid light bulbs.
 
subject: Simple Assignment question!!!!
 
Similar Threads
loops
switch question
why the below highlighted line is not showing unreachable compile error.
Help with a question from JCHQ
loops