aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equality test for boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equality test for boolean " Watch "equality test for boolean " New topic
Author

equality test for boolean

lakshmisreenivas karri
Greenhorn

Joined: Oct 31, 2003
Posts: 5
What is the result of executing the following fragment of code:
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
E. Compile error
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Karri
The if(flag=true) always is true, there is not a compile error because flag is a boolean. So the answer is A.
lakshmisreenivas karri
Greenhorn

Joined: Oct 31, 2003
Posts: 5
Thnx for the reply.
but why "if(flag=true)" is always true?
Could u gimme clear picture?
Thnx
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Because flag=true is an assignment, not a comparison. Therefore it assigns true to flag and then returns the value of flag.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
lakshmisreenivas karri
Greenhorn

Joined: Oct 31, 2003
Posts: 5
Yes I'm seeing it as an assignment but still i was stucked.
Ofcourse i got it now.
Thnx anyway.
 
 
subject: equality test for boolean
 
Similar Threads
boolean question from John Junt Mock Exam
simple question
if_else
boolean
Q.52 from John Hunt