CS 509, Advanced Programming II

Fall 2002 - Test 2


  1. Is it possible to throw an exception that can't be caught? Explain.


    No, for at least two reasons. First, it is always possible to catch any exception thrown with the ellipsis version of a catch clause

    try { f() }
    catch (...) {
      // any exception from f() ends up here.
      }
    

    Second, the C++ run-time system is the catcher of last resort. Even the program

    int main() {
      throw 1;
      }
    

    will have its exception caught, albeit not by the program itself.

    A number of people got this wrong by pointing out that a throw statement without any try-catch statements wouldn't be caught. Even ignoring the run-time catch, the question asked about an exception that couldn't be caught, not an exception that wouldn't be caught.


  2. Explain the difference between the float and double data types.


    See page 37 of Koenig and Moo.


  3. Suppose the following program

    int main() {
      T x;
      while (std::cin >> x) { }
    
      char c;
      std::cin.clear();
      if (std::cin >> c)
        std::cout << "c = '" << c << "'.\n";
      }
    

    produces some output for a given input file. What is the basic type T, what was the input, and what was the output? Explain.


    The while loop terminated when the cin extraction failed, which it can do on bad input, on end of file, or on input failure. The if extraction didn't fail; if it did, no output would have been produced. If the while extraction failed due to eof or input failure, then the if extraction would have failed in the same way (the call to clear() just clears cin's failure bits, it doesn't fix the problem with the input stream). The while extraction must have failed on incorrect input.

    The principle reason for failing on bad 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 T is int, then the input "a" (without the double quotes) produces the output

    c = 'a'.
    

    Almost everybody got this completely wrong; many people confused T with a template parameter, which isn't even syntactically close. Unfortunately, this kind of reasoning is necessary if you want to write solid input procedures using stream I-O.


  4. Describe a test case that determines whether or not an implementation of the second assignment moves two boxes at what appears to be the same time.

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


    The animation script is

    box b1 2 2
    box b2 2 2
    
    move b1  1  1  1 30 2000
    move b2 10  1 10 30 4000
    

    This moves two boxes from left to right across the screen, the top one moving twice as fast as the bottom one. If the animation program is moving boxes simultaneously, then by the time the top box reaches the right end of its movement, the bottom box should be somewhere around the middle of its movement.

    Pretty much everybody got this right.



This page last modified on 8 October 2002.