CS110 Java Practice Quiz #4

if and if..else




  Choose the best alternative:
  1. The if statement is a control structure used for selection (Decision Making). .

    NOTES:

    Logical expressions, which produce either the value true or false have come to be known as boolean expressions - also called conditions.
    Java has six relational operators: == , != , > , < , >= , <=

  2. The value True is represented in the computer as the integer .

  3. The value False is represented in the computer as the integer .

  4. The logical operator for AND is .

  5. The logical operator for OR is .

  6. 1 && 0 results in

  7. 1 || 0 results in

    JavaLogical Expressions
    Evaluate the following Logical expressions. Enter 1 or 0. (1 is TRUE; 0 is FALSE)

  8. (!0 )
  9. (5 + 4 < 3 && 7 + 3 <= 20 )
  10. ( 'a' != 'b' - 1)
  11. ( ! (7 == 7 ))
  12. ( 3 % 2 )
  13. ( !1 || !0 )
  14. ( 'a' == 97 )


    Java if Statements

  15. When using an if .. else statement, indentation is demanded by the compiler.

  16. What output is produced by the segment of code shown below:
    int x = 25;
    if (x == 25)
      System.out.print("BOBBA");
    else
      System.out.print("FAB");

  17. What output is produced by the segment of code shown below:
    int x = 12;
    if (x != 12)
      System.out.print("YES");
    else
       System.out.print("NO");

  18. What output is produced by the segment of code shown below:
    double Max = 12.7;
    if (Max >= 12)
      System.out.print("FRENCH");
    else
      System.out.print("FRIES");

    For the following questions, watch out for poor formatting.

  19. What output is produced by the segment of code shown below:

    int x = 12;
    if (x >= 12)
      System.out.print("HEY");
    System.out.print("YOU");

  20. What output is produced by the segment of code shown below:
    int x = 25;
    if (x / 2 == 12){
      System.out.print("BUFFY");
      System.out.print("LOVES");
    }else{
      System.out.print("ZACK)";
      System.out.print("ISA");
    }
    System.out.print("DIPPY";

  21. What output is produced by the segment of code shown below:
    Watch out for poor indentation!
    int x = 12;
    if (x > 12){
    System.out.print("BUFFY";
    System.out.print("SUE");
    }System.out.print("ELLEN");

  22. What output is produced by the segment of code shown below:
    int x = 20;
    if (x > 15)
       if ( x < 17)
         System.out.print("BLUE");
      else
        System.out.print("GREEN");
    System.out.print("JEANS");

  23. What output is produced by the segment of code shown below:
    Watch out for poor indentation!
    int x = 12;
    if (x > 12)
    if ( x < 15)
    System.out.print("BLUE");
    else
    System.out.print("GREEN");
    System.out.print("JEANS");

  24. What output is produced by the segment of code shown below:
    int x = 12;
    if (x > 12){
      if ( x < 15)
        System.out.print("BLUE");
    }else
      System.out.print("GREEN");
    System.out.print("JEANS");



Answers

  1. True. The if statement is a control structure used for decision making.
  2. 1. TRUE is the same as 1.
  3. 0. FALSE is the same as 0.
  4. The logical operator for AND is &&
  5. The logical operator for OR is ||.
  6. 0. 1(TRUE) and 0 (FALSE) is 0 (FALSE).
  7. 1. 1(TRUE) or 0 (FALSE) is 1(TRUE)
  8. 1. not FALSE is TRUE
  9. 0 ...because 9 < 3 && 10 <= 20 is ....FALSE && TRUE is.... FALSE
  10. 0 ...because 'a' != 'b' - 1 is the same as 'a' != 'a'. This is clearly FALSE.
  11. 0 ...because !(7==7) is the same as !TRUE which is FALSE.
  12. 1 ...because 3 % 2 is equal to 1.
  13. 1... because !1 || !0 is the same as !TRUE || ! FALSE which is TRUE.
  14. 1 ....because 'a' can be represented by the ascii character code 97. 'a' == 97 is TRUE!
  15. False. Indentation is NOT demanded by the compiler.
  16. BOBBA. Since (x==25) is TRUE, the if clause gets executed.
  17. NO. Since (x !=12) is FALSE, the else clause gets executed.
  18. FRENCH Since (Max >=12) is TRUE, the if clause gets executed.
  19. HEYYOU. (x >=12) is TRUE! Also the last cout statement is unconditional.
  20. BUFFYLOVESDIPPY. (x/2 == 12) is TRUE! Also the last cout statement is unconditional.
  21. ELLEN. Since (x > 12) is FALSE, the if clause is NOT executed. The last cout statement is unconditional.
  22. GREENJEANS. Since x>15 is TRUE and x <17 is FALSE, the nested else is executed. JEANS is unconditional.
  23. JEANS. Since (x > 15) is FALSE, only the last cout statement isexecuted because it is unconditional.
  24. GREENJEANS. Since (x>15) is FALSE, the ELSE claus statement is executed and the cout is unconditional.