Midterm Practice Questions

The following are practice questions for the midterm.

Please note that these are generic questions, and may not include all the topics discussed in your particular offering of the course. It is also possible that some questions are about topics not covered in your offering; please ignore any such questions.

  1. Suppose lst is a vector<string>. Re-write the following code using a C-style for-loop with an index variable:

    for(string& s : lst) {
        cout << s << "\n";
    }
    

    For a sample solution, see look here.

  2. Consider the following code fragment:

    void example() {
        vector<float*> nums = {new float(1),
                               new float(2),
                               new float(3)
                              };
    
        // ... code that uses nums ...
    }
    

    Add a fragment of code to example() that ensures there are no memory leaks due to the values nums points to.

    For a sample solution, see look here.

  3. Add a function called shrink() to doublevec that re-sizes the underlying array to be the same length as sz so that no memory is wasted.

    For a sample solution, see this implementation of doublevec.

  4. Lists in the Python programming language have a useful feature called negative indexing. If L is a Python list with n elements, then L[-1] is the last element, L[-2] is the second to last element, L[-3] is the third to last element, and so on down to L[-n], which is the first element of L.

    Add negative indexing to doublevec. Regular non-negative indexing should work as before, and index values less than -n and greater than n-1 should throw out-of-range errors.

    For a sample solution, see this implementation of doublevec.

  5. Write a class called Circle that can be used like this:

    const Circle c{5.3};  // 5.3 is the radius of the circle
                          // if a radius of 0, or less, is passed,
                          // then Circle throws an error
    
    cout << "        Radius = " << c.get_radius() << "\n"
         << "          Area = " << c.get_area() << "\n"
         << " Circumference = " << c.get_circumference()
         << "\n";
    
    Circle d{c};
    
    cout << "        Radius = " << d.get_radius() << "\n"
         << "          Area = " << d.get_area() << "\n"
         << " Circumference = " << d.get_circumference()
         << "\n";
    

    Here is a sample solution.

  6. Write a class called Rectangle that can be used like this:

    const Rectangle r{6, 2.4};  // 6 is the width, 2.4 is the height
                                // if either width or height is 0, or less,
                                // then Rectangle throws an error
    
    cout << "         Width = " << r.get_width() << "\n"
         << "        Height = " << r.get_height() << "\n"
         << "          Area = " << r.get_area() << "\n"
         << "     Perimiter = " << r.get_perimeter()
         << "\n";
    
    Rectangle b{r};
    
    cout << "         Width = " << b.get_width() << "\n"
         << "        Height = " << b.get_height() << "\n"
         << "          Area = " << b.get_area() << "\n"
         << "     Perimiter = " << b.get_perimeter()
         << "\n";
    

    Here is a sample solution.

  7. SFU designates semesters using a 4-digit numeric code of the form CYYS, where:

    • C is the century: 0 for the 1900s, 1 for the 2000s.
    • YY is the last two digits of the year.
    • S is the semester: spring is 1, summer is 4, and fall is 7.

    For example:

    • “0951” is “Spring 1995”
    • “1004” is “Summer 2000”
    • “1077” is “Fall 2007”

    Write a function called english_cyys(const string& cyys) that takes a CYYS code (as a string) as input an returns an English description (as in the examples) of the semester it represents

    Importantly, you cannot assume the CYYS code passed to english_cyys is properly formatted. If it’s not, then throw an error with the error function.

    Here is a sample solution.

  8. Suppose 4 friends are playing 7-card poker. What is the probability that one of them is dealt a hand with all 4 aces?

    While you could solve this problem purely mathematically, don’t do it that way here. Instead, simulate shuffling a deck of cards and dealing 4 7-card hands, and then check each hand to see if one contains all 4 aces.

    Repeat this, say, a million times, and keep a count of how many times someone gets 4 aces. Divide the final count by a million to get an estimate of the probability of getting four aces.

    The random_shuffle function is an easy to correctly shuffle an array of elements.

    For a sample solution, look here.

  9. The following questions are all related:

    1. Write an abstract base class called Monster that specifies these three methods:

      • name() returns the name of the monster
      • health() returns, as a double, the current health of the monster
      • do_damage(double d) that does damage amount d to the monster

      Importantly, no monster can ever have health less than 0.

    2. Write a class called Dragon that inherits from Monster, and could be used like this:

      Dragon d{"Smaug"};
      cout << d.name() << " currently has " << d.health() << " health\n";
      

      All dragons start with 100 health.

    3. Write a class called Penguin that inherits from Monster and works like this:

      Penguin p{"Cici", 10};
      cout << d.name() << " currently has " << d.health() << " health\n";
      
    4. Write a function (not a method!) called report that prints the name and health of any monster. It should work like this:

      Dragon d{"Smaug"};
      Penguin p{"Cici", 10};
      
      report(&d);  // prints "Smaug currently has health 100"
      report(&p);  // prints "Cici currently has health 10"
      
    5. Add a method (not a function!) to Monster called debug_info() that prints the name and health of the monster. It should work like this:

      Dragon d{"Smaug"};
      Penguin p{"Cici", 10};
      
      d.debug_info();  // prints "Smaug currently has health 100"
      p.debug_info();  // prints "Cici currently has health 10"
      

      Don’t modify the Penguin class or the Dragon class!

    For a sample solution, look here.