Hello, boolean flag = false; if (flag = true) { System.out.println("true"); } else { System.out.println("false"); } } A.true is printed to standard out B.false is printed to standard out C.An exception is raised D.Nothing happens The answer to above question is A....and I have checked by typing the code as well....that also gives answer as A. But, I am unable to understand the reason for this...Please can you explain..... Thanks.
c stet
Greenhorn
Joined: Jan 04, 2001
Posts: 1
posted
0
The line if(flag = true) sets the value of flag to true and then the if evalutes flag to see if it is true. To have the else statement execute you would need yo have the code use ==
Originally posted by Mukti Bajaj: Hello, boolean flag = false; if (flag = true) { System.out.println("true"); } else { System.out.println("false"); } } A.true is printed to standard out B.false is printed to standard out C.An exception is raised D.Nothing happens The answer to above question is A....and I have checked by typing the code as well....that also gives answer as A. But, I am unable to understand the reason for this...Please can you explain..... Thanks.
Vineet Sharma
Ranch Hand
Joined: Dec 30, 2000
Posts: 51
posted
0
Hi! The trick, here is with the operator used! Note that it is =operator(Assingment Operator) and not the ==operator(Equality Operator). From the mock exams I have taken I feel one needs to be extremely Attentive with the questions as well as the answer choices.