This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
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).]
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).]
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).]