Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

easy question

 
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~~
 
High Plains Drifter
Posts: 7289
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~~
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic