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

Instructor: Dan Shiovitz

Name: _____________________________________________________________

Mean was 25.20, min 20, 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 An else clause always pairs with the nearest unmatched if clause.
        True. Most people got this.

      2. T F Java is not a case sensitive programming language.
        False. Java is indeed case-sensitive. "foo" and "FOO" are different variables.

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

      4. T F The parseInt method of the Integer class turns an integer into a string.
        False. Other way around -- it turns a string into an integer.

    2. [3 points total - 1 each] Fill in the blanks (the first letter of each word is given)

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

      2. S_(SEMANTICS)_ dictate(s) the meaning of programming languages constructs, and

        s_(SYNTAX)_ dictate(s) the form by which they are used.

    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 = 13, minute = 29;
    double time = 10.0;

    1. [1 point] minute / 3
      This is integer division. That means the remainder is dropped. Therefore, 29/3 = 9
    2. [2 points] minute + -hour * time
      Operator precedence means that the first thing that is evaluated is the unary minus: -hour = -13. Then we multiple -13 by time, converting -13 into a floating-point number, and ending up with -130. Then we add minute to that, converting minute into a floating-point number and ending up with -101.0
    3. [2 points] minute > 30 || (hour >= 4 && minute < 30) 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 || (true && true), which reduces to false || true, which is true.
  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 no more than 8.0
    2. The weight must be at least 50.0
    3. The slice density (defined as width times depth divided by weight), must be more than 0.30

    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 <= 8.0 && weight >= 50.0 && (width * depth / weight) > 0.30)
          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 = 0, b = 6, c = 0;
    
        while (a < b) {
           if (a % 2 == 0)
           {
             System.out.print(b * c);
             c = c + 1;
           }
           else
             System.out.println("Tweedle");
    
           a = a + 1;
        }
        System.out.println("Twaddle" + a);
      

    Ok, the first thing to notice is the 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 0, once when it's 1, 2, 3, 4, 5, and then a will be 6 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 "Tweedle" followed by a newline. If it's even, we print the product of b and c, with no newline, and then we increment c by one. When the loop completes, we print "Twaddle" followed by the value of a (which, you remember, was 6 when the loop stopped). Therefore, the output is:

       0Tweedle
       6Tweedle
       12Tweedle
       Twaddle6