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.
Suppose
lst
is avector<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.
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 valuesnums
points to.For a sample solution, see look here.
Add a function called
shrink()
todoublevec
that re-sizes the underlying array to be the same length assz
so that no memory is wasted.For a sample solution, see this implementation of doublevec.
Lists in the Python programming language have a useful feature called negative indexing. If
L
is a Python list withn
elements, thenL[-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 toL[-n]
, which is the first element ofL
.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.
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";
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";
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 astring
) as input an returns an English description (as in the examples) of the semester it representsImportantly, you cannot assume the CYYS code passed to
english_cyys
is properly formatted. If it’s not, then throw an error with theerror
function.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.
The following questions are all related:
Write an abstract base class called
Monster
that specifies these three methods:name()
returns the name of the monsterhealth()
returns, as adouble
, the current health of the monsterdo_damage(double d)
that does damage amountd
to the monster
Importantly, no monster can ever have health less than 0.
Write a class called
Dragon
that inherits fromMonster
, and could be used like this:Dragon d{"Smaug"}; cout << d.name() << " currently has " << d.health() << " health\n";
All dragons start with 100 health.
Write a class called
Penguin
that inherits fromMonster
and works like this:Penguin p{"Cici", 10}; cout << d.name() << " currently has " << d.health() << " health\n";
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"
Add a method (not a function!) to
Monster
calleddebug_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 theDragon
class!
For a sample solution, look here.