Results 1 to 10 of 10

Thread: java

  1. #1
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Hi,
    Please work out and check the output.If there is any doubt ask.

    -Regards
    Ramya
    Ramya:cool:

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    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.

  4. #4
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    but the answer was given like 23 in book

    thats y little confused....
    thank u

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java 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.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Java Code:
    if ((b2 [B][COLOR="Red"]=[/COLOR][/B] 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...
    Java Code:
    if ([B][COLOR="Blue"]b2[/COLOR][/B] && b1)
    Luck,
    CJSL
    Last edited by CJSLMAN; 05-21-2009 at 02:58 PM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post
    Java Code:
    if ((b2 [B][COLOR="Red"]=[/COLOR][/B] 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,

    Java 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

  8. #8
    satti is offline Member
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    yeh the result is 2 3 it correct
    never never give up until successed

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •