CS 302 sect. 301
Quiz 1 (3% - 30 points)

Instructor: Dan Shiovitz

Name: _____________________________________________________________

Mean was 25.52, min 17, max 30.

  1. Terms and Concepts (9 points)

    1. [4 points total - 1 each] Is the statement below true or false? Circle one
      1. T F The parseInt method of the Integer class turns a string into an integer.
        True. Most people got this.

      2. T F The body of a while loop must execute at least once.
        False. The test expression is always evaluated first, and then the body is executed if the test was true.

      3. T F An else clause always pairs with the most-distant unmatched if clause.
        False. It pairs with the nearest one not inside braces.

      4. T F Java is a case sensitive programming language.
        True. True. "foo" and "FOO" are not the same variable, which is why it's confusing and a bad idea to use them in the same program.

    2. [3 points total - 1 each] Fill in the blanks (the first letter of each word is given)
      1. S_(SYNTAX)_ dictate(s) the form by which programming languages constructs are to be used, and

        s_(SEMANTICS)_ dictate(s) their meaning.

      2. A l_(LITERAL)_ value is an actual value in a program.

    3. [2 points] Briefly describe two reasons why constants should be used.

        A number of answers here, of course. The best two:

      1. If you use a constant and wish to change its value, you only have to change it in one place. If you were using the number, you'd have to remember all the places you used it and change all of them.
      2. If you use a constant, then you're using words instead of numbers, and it's obvious what the word represents. Using mysterious numbers in your program with no explanation of their meaning is known as a "magic number" problem.

  2. Expressions [5 points]

    Given the variables below, evaluate the following expressions. Distinguish floating-point numbers from integers.
    int hour = 3, minute = 35;
    double time = 11.0;

    1. [1 point] minute % 3
      This is the modulus operator. Remember? It gives the remainder when the first number is divided by the second. The remainder when 35 is divided by 3 is 2.
    2. [2 points] minute / hour + -time
      Due to operator precedence, the first thing we evaluate is the unary minus in front of time. -time = -11.0. Then we evaluate minute/hour. These are both integers, so we do integer division, which means dropping the fraction. 35/3, therefore, is 11. Then we evaluate 11 + -11.0. The 11 gets automatically converted into a floating-point number, so we get 11.0 + -11.0 = 0.0 Note that this is a floating-point result, and is different from 0.
    3. [2 points] (hour >= 4 || minute == 30) && minute < 40
      Some people had trouble with this one. "boolean" is a valid type just like "int" and "double". A && or >= type operator is going to result in a boolean value. This one fairly simply reduces to (false || false) && true, which reduces to false && true, which is false.
  3. If/Else Statements [8 points]

    Implement a code fragment (not a whole program) that prints whether or not a potato is suitable for being made into potato chips. Assume you are given four variables, weight, length, width, and depth, for the potato, and that they have been assigned positive floating-point values. Print "yes" if the potato with those values meets the standards given below, and "no" if it doesn't. Assume a rectangular potato.

    1. The length must be at least 10.0
    2. The weight must be more than 66.0
    3. The slice density (defined as width times depth divided by weight), must be at least 0.35

    Note that this is a code fragment, not a whole program, and you are given four variables which have already been assigned values. Also note "at least", "more than", and so on, and make sure you know when this means >= and when this means >. The cleanest way to write it would be something like:
        if (length >= 10.0 && weight > 66.0 && (width * depth / weight) >= 0.35)
          System.out.println("Yes.");
        else
          System.out.println("No.");   
        
  4. While Statements [8 points]

    Show the output for the code fragment below in the box provded. Show a trace of your execution for partial credit (the more information given the more likely I will be able to follow your logic).

        int a = 18, b = 23, c = 0;
    
        while (a < b) {
           if (a % 2 == 1)
             System.out.print(c);
           else
           {
             System.out.println("Hop");
             c = c + 1;
           }
           a = a + 1;
        }
        System.out.println("Skip Jump" + b);
      

    Ok, the first thing to notice is that this loop loops as long as a is less than b. b never changes. a gets incremented by one each time the loop body occurs. Therefore the loop will happen once when a is 18, once when it's 19, 20, 21, 22, and then a will be 23 and the test will fail and the loop will stop. Each time the loop body happens, also, before a is incremented, we test if it's odd or even (that's what the % 2 does, remember?). If it's odd, we print (with no newline!) the value of c (which is a variable, not a letter, so it won't print "c", it'll print whatever c stores). If it's even, we print "Hop", followed by a newline, and then increase c. When the loop completes, we print "Skip Jump" followed by the value of b, which doesn't change for the whole program. Therefore, the output is:

        Hop
        1Hop
        2Hop
        Skip Jump23