More If Statements Worksheet

Write boolean expressions for each of the following conditions:

  1. x is greater than 3
  2. y is between 2 and 5, but not equal to 2 or 5
  3. r is negative and z is positive
  4. a and b are both positive
  5. a is less than 6 or is greater than 10
  6. x is less than 3 and is greater than or equal to zero
  7. Read an area code. If the area code is 732, 973, or 908, print the message "Local call". Otherwise print the message "Long distance".
  8. Read an area code. If the area code is 732 or 848, print the Message "Local". If the area code is 201, 908, or 973, print the message "North Jersey". If the area code is 609, print the message "South Jersey". Otherwise print the message "Not in Jersey".
  9. Write the statements to read in a character and print a message stating whether the character is an uppercase letter, a lowercase letter, a digit, or a special character. An uppercase letter is any character that is greater than or equal to 'A' and less than or equal to 'Z'. A lowercase letter is any character that is greater than or equal to 'a' and less than or equal to 'z'. A digit is any character that is greater than or equal to '0' and less than or equal to '9'. All other characters are special characters.
  10. Write the statements to calculate and print the fine for a speeding ticket. Read in the speed limit and the actual speed, and calculate how much the driver was travelling over the speed limit. If the miles over the speed limit is between 1 and 15, the fine is $75. If the miles over the speed limit is between 16 and 24, the fine is $125. If the miles over the speed limit is 25 or more, the fine is $215. You can assume that the actual speed is greater than the speed limit.
  11. What value is assigned to fee by the if statement below when speed is 75?
         if (speed > 35)
            fee = 20.00;
         else
            if (speed > 50)
               fee = 40.00;
            else
               if  (speed > 75)
                  fee = 60.00;
    
  12. True or false? The following two if statements perform the same tasks as the subsequent if-else statement:
         if (i < 0) {
            i = 10 + i;
            j = i * i;
            System.out.println("i = " + i);
            System.out.println("j = " + j);
         }
         if (i >= 0) {
            k = 4 * i + 1;
            System.out.println("k = " + k);   }
    
    
         if (i < 0) {
            i = 10 + i;
            j = i * i;
            System.out.println("i = " + i);
            System.out.println("j = " + j);
         }
         else {
            k = 4 * i + 1;
            System.out.println("k = " + k);
         }
    
  13. Explain the difference between the statements on the left and the statements on the right below. For each of them, what is the final value of x if the initial value of x is 0?
         if ( x >= 0)                     if (x >= 0)
            x = x + 1;                       x = x + 1;
         else                             if (x > = 1)
            if (x >= 1)                      x = x + 2;
               x = x + 2;
    
  14. Consider the following code segment:
         if (i <= j)
            System.out.println("1");
         else
            System.out.println("2");
         System.out.println("3");
    
    1. if i is 1 and j is 2, what is the output?
    2. if i is 2 and j is 1, what is the output?
    3. if i is 2 and j is 2, what is the output?
  15. Given the int variables x, y, and z, where x is 3, y is 7, and z is 6, what is the output from each of the following code fragments?
    1.      if (x<=3)
           System.out.println(x+y);
           System.out.println(x+y);
      
    2.      if (x != -1) {
           System.out.println(x);
           System.out.println(y);
           System.out.println(z);
           }
           else
           System.out.println("x");
           System.out.println("z");
      
    3.      if (x != -1)
           System.out.println("The value of x is " + x);
           else
           System.out.println("The value of y is " + y);
      
  16. The following expressions make sense but are invalid according to Java’s rules of syntax. Rewrite them so that they are valid logical expressions. (All the variables are of type int).
    1. x < y <= z
    2. x, y, and z are greater than 0
    3. x is equal to neither y nor z
    4. x is equal to y and z
  17. What causes the error message "unexpected else" when this code fragment is compiled?
         if (mileage < 24.0) {
            System.out.println("Gas");
            System.out.println("guzzler");
         };
         else
            System.out.println("Fuel efficient");
    
  18. Simplify the following program segment, taking out unnecessary comparisons. Assume that age is an int variable.
         if (age > 64)
            System.out.println("Senior voter");
         if (age < 18)
            System.out.println("Under age");
         if (age >=18 && age < 65)
            System.out.println("Regular voter");
    
  19. The following Java program segment is almost unreadable because of the inconsistent indentation and random placement of left and right braces. Fix the indentation and align the braces properly.
         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");
    
  20. Suppose the input is 5. What is the value of alpha after the following Java code executes? (Assume that alpha is an int variable and console is a Scanner object initialized to System.in.
         alpha = console.nextInt();
    
         switch (alpha) {
            case 1:
            case 2:  alpha = alpha + 2;
                     break;
            case 4:  alpha += 1;
            case 5:  alpha *= 2;
            case 6:  alpha += 5;
                     break;
            default:  alpha -= 1;
         }
    
  21. Write a segment of Java code, using a switch statement, that prints "Even" if the integer value read from the keyboard is even, and prints "Odd" otherwise. Hint: if the result of taking the modulus of an integer value and the number 2 is 0 the number is even. If the result is 1, the number is odd.

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

© Copyright Emmi Schatz 2013