Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Assignment question!!!!

 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse, the answer is d.
 
Sandra Marti
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surya,
I compiled it! But that was definately not the answer I had expected.
 
Vivek Namala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiled I got out put as 6.
 
Amandeep Waraich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too !! got 6 as the output...
Aman
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic