CS 509, Advanced Programming II

Fall 2002 - Test 2


  1. Describe a test case that determines whether or not an implementation of the second assignment provides correct behavior for overlapping boxes.

    Describe the input script and expected output, and explain how the expected output satisfies the required test.


    The animation script is

    box b1 10  5 0 
    box b2 10  5 1
    box b3 10  5 2
    box b4  4 23 1
    
    move b1 1  3 1  3 1
    move b2 1 10 1 10 1
    move b3 1 17 1 17 1
    move b4 4  1 4  1 1
    

    The script creates four boxes, three vertical (b1, b2, and b3) and one horizontal (b4). Boxes b2 and b4 have the same level; b1 has a lower level than does b2 and b3 has a higher level.

    The move statements don't move at all, but place b1, b2, and b3 so they overlap with b4. Given their respective levels, b1 should appear below b4, b2 should be merged with b4, and b3 should appear above b4:

      +---+  +---+  +---+
      |   |  |   |  |   |
      |   |  |   |  |   |
    +---------   ---|   |-+
    |               |   | |
    |               |   | |
    +---------   ---|   |-+
      |   |  |   |  |   |
      |   |  |   |  |   |
      +---+  +---+  +---+
    

    A lot of people got this almost right by testing for either overlapping or merging, but not both.


  2. Why is std::endl a special stream manipulator?


    See Koenig and Moo, page 36.

    An absurdly large number of people tried to answer this question by using input streams in their answer.


  3. Suppose the following program

    int main() {
      T x;
      while (std::cin >> x) { }
      assert(!std::cin.bad() && !std::cin.eof());
      }
    

    runs without error for a given input file. What is basic type T and what was the input? Explain.


    The while loop terminated when the cin extraction failed, which it can do on incorrect input, on end of file, or on input failure. If the while extraction failed due to eof or input failure, then the program would have produced an error when the assertion failed. The while extraction must have failed on incorrect input.

    The principle reason for failing on incorrect input is formatting errors; that is, the type T of the extraction variable doesn't match the next available characters in the input stream. The char and char * types accept almost all characters on input, so T must be a type with more restrictive formatting requirements, such as bool or int.

    For example if the input was "a" (without the double quotes) then T can be of type int but not of type char or char *.

    This question got almost everybody; many people ignored the constraint that no error occurred during execution. Unfortunately, this kind of reasoning is necessary if you want to write solid input procedures using stream I-O.


  4. Explain how a function can throw an exception to itself.


    The same way it can throw an exception in general: by using a try-catch statement. For example,

    int f(void) {
      try {
        throw 1;
        }
      catch (int e) {
        std::cout << "caught exception << i << "\n";
        }
      }
    

    Some people tried to answer this question by referring to a recursive function, which was clever, but then forget to mention using a try-catch statement, which was not so clever.



This page last modified on 9 October 2002.