Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-21-2009, 07:05 AM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
gpveena20 is on a distinguished road
Default java
public class Test {
12. public static void main(String [] args) {
13. int x =5;
14. boolean b1 = true;
15. boolean b2 = false;
16.
17.if((x==4) && !b2)
18. System.out.print(”l “);
19. System.out.print(”2 “);
20. if ((b2 = true) && b1)
21. System.out.print(”3 “);
22. }
23. }
What is the result?
A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. Au exceptional is thrown at runtime.


output for if stm is true
so that output will print =123

is thisright or wrong
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-21-2009, 07:22 AM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 589
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
Hi,
Please work out and check the output.If there is any doubt ask.

-Regards
Ramya
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-21-2009, 07:52 AM
Senior Member
 
Join Date: Feb 2009
Posts: 624
Rep Power: 2
pbrockway2 is on a distinguished road
Default
RamyaSivakanth is right.

But my suggestion is to hand this in:

H. Deliberate non-use of braces and abominations like "if((b2==true)&&cetera)" deserve contempt.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-21-2009, 09:05 AM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
gpveena20 is on a distinguished road
Default
but the answer was given like 23 in book

thats y little confused....
thank u
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-21-2009, 11:15 AM
Senior Member
 
Join Date: Feb 2009
Posts: 624
Rep Power: 2
pbrockway2 is on a distinguished road
Default
Code:
if((x==4) && !b2)
The first if condition is not true because x is 5 and 5 is not 4.

Hence the inital "1" will not be printed.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-21-2009, 03:52 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Code:
if ((b2 = true) && b1)
Also, you have an error in this "if" statement... it should be "==" to compare ...

edit: and on second thought, you don't even need a comparation...
Code:
if (b2 && b1)
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Last edited by CJSLMAN; 05-21-2009 at 03:58 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-22-2009, 05:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by CJSLMAN View Post
Code:
if ((b2 = true) && b1)
Also, you have an error in this "if" statement... it should be "==" to compare ...

Chris, I'm mess-up with this. For me it's a valid statement. You can have assignments in if condition like that.

So,

Code:
if ((b2 = true) && b1)
here b2 assign to true and since the b1 is true, the condition evaluates as true. So the final result is 2 3
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-22-2009, 07:51 AM
Member
 
Join Date: Mar 2009
Posts: 31
Rep Power: 0
satti is on a distinguished road
Default
yeh the result is 2 3 it correct
__________________
never never give up until successed
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-22-2009, 05:17 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default oh...ok..
Eranga: Oh..ok... yes, I understand that you can have assignments in an "if" statement. I've rarely seen it used that way, so I assumed that it was supposed to be a an assignment. Sorry for any misunderstandings.

CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-23-2009, 05:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
It's okay Chris, I just added what I've seen in the code. I don't think that our thread starter knows about that, and I try to pointed it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +2. The time now is 12:47 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org