• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

easy question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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~~
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it!
thanks~~
reply
    Bookmark Topic Watch Topic
  • New Topic