Course Materials

Mockup Final Exam 1

This exam covers the whole course, with most of the questions from Weeks 5 to 8. Try to solve it on paper, without a compiler.

Question 1

What is the output of the following program?

#include <iostream>
using namespace std;

class Media {
protected:
    int id;

public:
    Media(int i) : id(i) {
        cout << "Media " << id << endl;
    }
    virtual ~Media() {
        cout << "~Media " << id << endl;
    }
    void info() const {
        cout << "info " << id << endl;
    }
    virtual void play() const {
        cout << "play media " << id << endl;
    }
};

class Song : public Media {
public:
    Song(int i) : Media(i) {
        cout << "Song" << endl;
    }
    ~Song() {
        cout << "~Song" << endl;
    }
    void info() const {
        cout << "song info " << id << endl;
    }
    void play() const override {
        cout << "play song " << id << endl;
    }
};

class Video : public Media {
public:
    Video(int i) : Media(i) {
        cout << "Video" << endl;
    }
    ~Video() {
        cout << "~Video" << endl;
    }
    void play() const override {
        cout << "play video " << id << endl;
    }
};

int main() {
    Media* items[3];
    items[0] = new Song(1);
    items[1] = new Video(2);
    items[2] = new Media(3);

    for (int k = 0; k < 3; k++) {
        items[k]->info();
        items[k]->play();
    }

    delete items[1];
    delete items[0];
    delete items[2];

    Song s(4);
    Media& r = s;
    r.info();
    r.play();
    cout << "end" << endl;
    return 0;
}

Question 2

The program below does not compile. Each statement marked with a number from (1) to (12) is a candidate. Treat each one as if it were the only marked statement in the program. For every statement, say whether it compiles, and for every statement that does not, give the reason.

#include <iostream>
using namespace std;

class Shape {
private:
    int id;

protected:
    int size;

public:
    Shape(int i, int s) : id(i), size(s) { }
    virtual ~Shape() { }
    virtual double area() const {
        return size * size;
    }
};

class Circle : public Shape {
public:
    Circle(int i, int r) : Shape(i, r) { }
    double area() const override {
        return 3 * size * size;
    }
    void radiusUp() {
        size++;
    }
    void test() {
        size = 5;                                  // (1)
        id = 5;                                    // (2)
    }
};

class Point {
private:
    int x, y;

public:
    Point(int px, int py) : x(px), y(py) { }
    Point operator*(int times) const {
        return Point(x * times, y * times);
    }
    Point& operator++() {
        x++;
        y++;
        return *this;
    }
};

int main() {
    Circle c(1, 2);
    Shape s(2, 3);
    Shape* p = &c;
    Point a(1, 2);

    Shape& r = c;                                  // (3)
    Circle* q = &s;                                // (4)
    p->radiusUp();                                 // (5)
    cout << p->area();                             // (6)
    Shape* arr = new Shape[3];                     // (7)
    cout << c.size;                                // (8)
    Point d = 3 * a;                               // (9)
    Point e = a * 2;                               // (10)
    a++;                                           // (11)
    cin >> a;                                      // (12)
    return 0;
}

Question 3

Write the class Team and a main that uses it, in one file. Team has:

  • Private data members: a const int id, a pointer int* points to an array with the points of the team, and an int size, the size of the array.
  • A constructor with two default parameters: the id (0) and the size (1). It sets id in the member initializer list, creates the array with new (a size smaller than 1 becomes 1), and sets every element to 0.
  • A copy constructor that makes a deep copy.
  • A destructor that releases the array.
  • setPoints(k, p), which sets the element k to p, only if k is a valid index and p is not negative. It must work in a chain: a.setPoints(0, 10).setPoints(1, 5).
  • total(), which returns the sum of the points.
  • A member operator < that returns true if the total of this team is smaller than the total of another team.
  • A friend operator << that prints Team, the id, a colon and the total, as in Team 1: 22.

Mark every function that only reads the object as const.

In main, create two teams with 3 points each and fill them with chained setPoints calls, including calls with an invalid index and with a negative number. Print both teams with <<, and print a message if the second team has fewer points than the first, using <. Then make a copy of the first team, change one point of the copy, and print the first team and the copy to show that the first team did not change.

Question 4

The class Shape is given:

#include <iostream>
using namespace std;

class Shape {
protected:
    int id;

public:
    Shape(int i) : id(i) { }
    virtual ~Shape() { }
    virtual double area() const {
        return 0;
    }
    void print() const {
        cout << "shape " << id << ": " << area() << endl;
    }
};

Write these classes after Shape, with override where it is needed:

  • Rectangle, derived from Shape, with a width and a height. Its constructor takes the id, the width and the height.
  • Square, derived from Rectangle. Its constructor takes the id and the side.
  • Triangle, derived from Shape, with a base and a height. Its area is half of the base times the height.

Then write a function totalArea that receives an array of Shape* and its size, and returns the sum of the areas. In main, create an array of 3 pointers with new: a rectangle, a square and a triangle. Print the three shapes with print(), print the total area, and release the objects.

Question 5

Multiple choice. For each part, answer the question below the code, then press Check answers.

(a)

class Buffer {
private:
    int* data;

public:
    Buffer(int n) { data = new int[n]; }
    ~Buffer() { delete[] data; }
};

Buffer a(10);
Buffer b = a;
What happens when the objects are destroyed? 0 / 1 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.Buffer b = a;line 11 of the program

Buffer has no copy constructor. At the end of the program, a and b are destroyed. What happens?

Answer for Buffer b = a;

(b)

class Vehicle {
public:
    Vehicle() { cout << "V"; }
};

class Engine {
public:
    Engine() { cout << "E"; }
};

class Car : public Vehicle {
private:
    Engine engine;

public:
    Car() { cout << "C"; }
};

Car c;
What does the program print? 0 / 1 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.Car c;line 19 of the program

Car is derived from Vehicle and has an Engine as a data member. What does the program print?

Answer for Car c;

(c)

class Counter {
private:
    int n;

public:
    Counter() : n(0) { }
    Counter add(int x) {
        n += x;
        return *this;
    }
    int get() const { return n; }
};

Counter c;
c.add(1).add(2).add(3);
cout << c.get();
What does the last line print? 0 / 1 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.cout << c.get();line 16 of the program

add returns a copy of the object (the return type is Counter, not Counter&). What does the last line print?

Answer for cout << c.get();

(d)

class Point {
private:
    int x, y;

public:
    Point(int px, int py) : x(px), y(py) { }
    Point& operator++() {
        x++;
        y++;
        return *this;
    }
    Point operator++(int) {
        Point old = *this;
        ++(*this);
        return old;
    }
};

Point p(1, 1);
Point q = ++p;
Point r = p++;
What are r and p? 0 / 1 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.Point r = p++;line 21 of the program

After these three lines, what are r and p?

Answer for Point r = p++;