• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

easy question

 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Base{

private void test() {

boolean flag = false;
if(flag==true){
System.out.println("001 Flag is true");
}
else {
if (flag=true)
System.out.println("002 Flag is true");
else
System.out.println("003 Flag is false");
}

}

static public void main(String[] a) {
new Base().test();
}

}
Select most appropriate answer.
a) 001 Flag is true
b) 002 Flag is true
c) 003 Flag is false
d) None
the ans is b, can anyone explain this to me? thanks~~
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kid of question tests your recognition of the difference between the assignment operator (flag=true) and the equality test (flag==true).
The test: if (flag=true) succeeds if the assignment statement succeeds. flag is a boolean variable, so the assignment is ok because true is a boolean value. This branch therefore will execute its code.
-----------------
Michael Ernest, co-author of:
The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited December 27, 2000).]
 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it!
thanks~~
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic