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

Instructor: Dan Shiovitz

Name: _____________________________________________________________

Mean was similar to last time, around 25, though with a greater variation.

  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 values of an object's variables define the state of an object.
        True.
      2. T F An object is an instance of a class.
        True.
      3. T F Aliasing does not occur with primitives.
        True. Aliasing is when you have two names for a single thing; this can't occur with primitives because their value is always copied.
      4. T F A program will not successfully compile if it has at least one syntax error.
        True. (This is not true for semantic errors, of course)
      5. 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.
      6. T F The new operator is used to instantiate an object.
        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]

    A rocket ship is sitting on a launch pad, waiting to blast off. Each second it sits on the launch pad, it uses up 5% of its remaining fuel. When its second count reaches zero, it blasts off. Assume the variables int secondsToGo and double fuelRemaining have each been declared and initialized to some value. Implement a code fragment using a while loop to count down to zero seconds, printing out the number of seconds each time, and then print "blast off!" and the remaining fuel amount when the counter gets to zero. An example output might be, for some value of secondsToGo and fuelRemaining:
    3..2..1..BLAST OFF! (85.0 units of fuel remaining)

    Here's a simple solution:

    while (secondsToGo > 0)
    {
      System.out.print(secondsToGo + "..");
      fuelRemaining = fuelRemaining * 0.95;
    }
    System.out.println("BLAST OFF! (" + fuelRemaining + 
                       " units of fuel remaining)");
        

  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("Score");
    int len = str.length();
    int a = 1, b, c;
    
    while (a <= len) {
        c = len - a;
    
        while (c < len) {
            System.out.print(str.charAt(c));
            c = c + 1;
        }
    
        b = len;
    
        while (b > a) {
            System.out.print('-');
            b = b - 1;
        } 
    
        System.out.println();
        a = a + 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 (a <= len). a 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 "Score" 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 (c < len). len is always 5, and c increases by one each time, so the question is what does c start at? c starts at something that depends on the value of len and a: c = (len - a); len is always 5, again, and a ranges from 1-5, so c'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 c. c, 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 "e" (because charAt indexes strings from 0). The second time it runs, it will run twice and print "r" and then "e". The last time it runs it will run five times and print out "S", then "c", then "o", then "r", then "e". The second loop is almost identical to this one, except it counts down rather than up. Also, it's printing out -s every time, not particular characters. So the final expected output is:
    e - - - -
    r e - - -
    o r e - -
    c o r e -
    S c o r e