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

Instructor: Dan Shiovitz

Name: _____________________________________________________________

  1. Terms and Concepts (10 points)

    1. [6 points total - 1 each] Is the statement below true or false? Circle one:
      1. T F The new operator is used to instantiate an object.
        True.
      2. T F Aliasing occurs with primitives.
        False. Aliasing is when you have two names for a single thing; this can't occur with primitives because their value is always copied.
      3. T F A program will not successfully compile if it has at least one syntax error.
        True.
      4. T F str1 == str2 compares if the contents of Strings str1 and str2 are the same.
        False. It compares addresses. To compare contents, you have to use the equals() function.
      5. T F The values of an object's variables define the state of an object.
        True.
      6. T F An object is an instance of a class.
        True.

    2. [4 points total - 1 each] What are the four basic activities involved in software development?

        There are different names for these, but basically:

      1. determining the REQUIREMENTS for the software
      2. creating a DESIGN for the software that fits the requirements
      3. IMPLEMENTING the design previously created
      4. TESTING/DEBUGGING the implementation produced


  2. Coding While Statements [8 points]

    An airplane is about to touch down on a landing pad. Each second it is still in the air, it slows down by 10 mph (assume it never gets into negative speed). Implement a code fragment using a while loop to count down to zero seconds (when the plane touches down), printing the number of seconds each time and then print "Final speed at touchdown: mph." Assume the variables int secondsToTouchdown and int currentSpeed have each been declared and initialized to some value.. An example output might be, for some value of secondsToTouchdown and currentSpeed:
    5..4..3..2..1..Final speed at touchdown: 20 mph.

    while (secondsToTouchdown > 0)
    {
      System.out.print(secondsToTouchdown + "..");
      currentSpeed = currentSpeed - 10;
    }
    System.out.println("Final speed at touchdown: " + currentSpeed + " mph.");
        

  3. Objects and Methods [12 points]

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

    For your reference, here are two methods from the String class:

    char charAt(int pos)

    Returns the character at position pos. Remember that the first character is at position 0.
    int length()
    Returns the number of characters in the string.

    String str = new String("Panda");
    int len = str.length();
    int x, y, z = 1;
    
    while (z <= len) {
    
        x = len - z;
    
        while (x < len) {
            System.out.print(str.charAt(x));
            x = x + 1;
        }
    
        y = len;  
    
        while (y > z) {
            System.out.print(y);
            y = y - 1;
        } 
    
        System.out.println();
        z = z + 1;
    }
    
    Ok, here we go. These are all while loops, so the first thing to do is to determine how many times they all go. They're all counting loops, which have three parts. What are they? The initialization, the test, and the each time. For the outer loop, you can see the test is (z <= len). z increases by one at the bottom of the loop each time, and it starts at 1, so the outer loop will execute len times. The length of "Panda" is five. Then there's two inner loops. So the big picture is going to be some number of times (five), do something, and then do something else. Each of those inner somethings is a loop too. A quick glance shows they're both printing something, so we expect to see five lines, with each line made up of two parts. The first inner loop goes while (x < len). len is always 5, and x increases by one each time, so the question is what does x start at? x starts at something that depends on the value of len and z: c = (len - z); len is always 5, again, and z ranges from 1-5, so x's initial value will range from 4 to 0. That, in turn, means the loop will first execute (5 - 4) times, then (5 - 3) times, ..., down to (5 - 0) times. Or, in other words, the five times it executes, it will run first once, then twice, then three times, then four, then five. Each time it runs, the body prints out the character at position x. x, you remember, ranges from its initial value, (somewhere between 4 and 0), and one less than len. Putting those two facts together, we see that the first time the loop runs, it will print str.charAt(4), which is "a" (because charAt indexes strings from 0). The second time it runs, it will run twice and print "d" and then "a". The last time it runs it will run five times and print out "P", then "a", then "n", then "d", then "a". The second loop is almost identical to this one, except it counts down rather than up. Also, it's printing out the value of y every time (not the character y -- that'd be 'y' (with the single quotes)). So the final expected output is:
    a 5 4 3 2
    d a 5 4 3
    n d a 5 4
    a n d a 5
    P a n d a