• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam -4test.com

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic