Exam prep

Here are some complete mid-term tests from previous years, as well as some selected questions from previous final exams. The finals have the same format as the mid-terms, but with some additional question types as shown below (specifically, questions involving UML that we hadn’t seen before the mid-term test).

Previous mid-term tests

Previous final exam questions

2015 Q5(b)

Given the following class hierarchy:

#include <string>

class Lorem
{
	public:
	virtual std::string hello() const = 0;
	std::string world() const { return "world"; }
};

class Ipsum : public Lorem
{
	public:
	std::string hello() const { return "Hello"; }
};

class Dolor : public Ipsum
{
	public:
	std::string hello() const { return "Hi"; }
	std::string world() const { return "planet"; }
};

class Sit : public Lorem
{
	public:
	std::string hello() const { return "Greetings"; }
	std::string world() const { return "everyone"; }
};

class Amet : public Sit
{
	public:
	std::string hello() const { return "Salutations"; }
	std::string world() const { return "third rock from the sun"; }
};

i) Sketch a UML diagram to represent the hierarchy (you may ignore method details). [2 marks]

ii) What will be output when the following code is run? [4 marks]

#include "vtables.h"
#include <iostream>

int main(int argc, char *argv[])
{
	Ipsum ipsum;
	Dolor dolor;
	Sit sit;
	Amet amet;

	Lorem& i = ipsum;
	Ipsum& d = dolor;
	Sit& s = sit;
	Sit& a = amet;

	std::cout << i.hello() << ", " << i.world() << "!" << std::endl;
	std::cout << d.hello() << ", " << d.world() << "!" << std::endl;
	std::cout << s.hello() << ", " << s.world() << "!" << std::endl;
	std::cout << a.hello() << ", " << a.world() << "!" << std::endl;

	return 0;
}

2015 Q6

Given the following class declaration (and assuming Light and MasterController are declared elsewhere):

class Intersection
{
	public:
	/* ... */
	~Intersection();

	Intersection(const Intersection&);
	Intersection(Intersection&&);

	Intersection& operator= (const Intersection&);
	Intersection& operator= (Intersection&&);

	private:
	//! The master traffic controller that "owns" this intersection.
	const MasterController *controller_;

	Light *lights_;        //!< Lights that this intersection owns.
	unsigned int size_;    //!< Number of owned lights.
};

implement:

  • the destructor [2 marks]
  • the move constructor [3 marks]
  • the copy assignment operator [4 marks]