<

Third If Statement Worksheet Answers

  1. x > 3
  2. y > 2 && y < 5
  3. r < 0 && z > 0
  4. a > 0 && b > 0
  5. a < 6 || a > 10
  6. x < 3 && x >= 0
  7. 	System.out.println("Enter the area code: ";
        areacode = keyboard.nextInt();
        if (areacode == 732 || areacode == 973 || areacode == 908)
           System.out.println("Local call");
        else
           System.out.println("Long distance");
    	
  8. 	System.out.println("Enter the area code: ";
        areacode = keyboard.nextInt();
        if (areacode == 732 || areacode == 848)
           System.out.println("Local");
        else
           if (areacode == 201 || areacode == 908 || areacode == 973)
              System.out.println( "North Jersey");
           else
              if (areacode == 609)
                 System.out.println("South Jersey");
              else
                 System.out.println("Not in Jersey");
    	
  9.      System.out.println("Enter a character: ");
         str = keyboard.next();
         ch = str.charAt(0);
         if (ch >= 'A' && ch <= 'Z')
            System.out.println(ch + " is an uppercase letter");
         else if (ch >= 'a' && ch <= 'z')
            System.out.println(ch + " is a lowercase letter");
         else if (ch >= '0' && ch <= '9')
            System.out.println(ch + " is a digit");
         else
            System.out.println(ch + " is a special character");
    	
  10.      System.out.print("Enter the speed limit: ");
         speedLimit = keyboard.nextInt();;
         System.out.print("Enter the actual speed: ");
         actualSpeed = keyboard.nextInt();
         overlimit = actualSpeed - speedLimit;
         if (overlimit >= 1 && overlimit <=14)
            fine = 75;
         else if (overlimit <=24)
            fine = 125;
         else
            fine = 215;
         System.out.println("The fine for travelling " + overlimit +
               "miles over the speed limit is " + fine);
    	
  11. 20 (the first if is true, everything in else is skipped)
  12. True, since the condition in the else is the opposite of i < 0, it is the same as the condition i >= 0.
  13. In the statements on the left, the if condition is true so the final value of x is 1. Since the if is true the statements in the else are never executed. In the statements on the right, the first if condition is true so x is set to 1. Since the two if statements are not nested the second if is executed whether or not the first if condition is true. The condition in the second if is true, so the final value of x is 3.
    1.      1
           3
           	
    2.      2
           3
           	
    3.      1
           3
           	
    1.      10
           10
           	
    2.      3
           7
           6
           z
           	
    3.      The value of x is 3
           	
    1. x < y && x <= z
    2. x > 0 && y > 0 && z > 0
    3. x != y && x != z
    4. x == y && x == z
  14. The semicolon after the close brace of the if means that the close brace is the end of the if statement, and that there is no else statement for this if.Thus the else can't be matched with the if so it has no if to match with.
  15.      if (age < 18)
            System.out.println("Under age");
         else
            if (age < 65)
               System.out.println("Regular voter");
            else
               System.out.println("Senior voter");
         
  16.       if (a > 0)
             if (a < 20) {
                System.out.println("A is in range");
                b = 5;
             }
             else {
                System.out.println("A is too large");
                b = 3;
             }
          else
             System.out.println("A is too small");
          System.out.println("All done");
          
    
    	  
  17. 15
  18.       System.out.print("Enter a number: ");
          num = keyboard.nextInt();
          switch (num % 2) {
             case 0:  System.out.println("even");
                      break;
             case 1:  System.out.println("odd");
                      break;
          }
          

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013