Mockup Midterm Exam 1
This exam covers Weeks 1 to 4. Try to solve it on paper, without a compiler.
Question 1
What is the output of the following program? Assume that the compiler never makes an extra copy of a temporary object: the elements of the array are created directly. The only copies are the ones made by the program itself.
#include <iostream>
using namespace std;
class Ticket {
private:
int number;
public:
Ticket(int n = 1) : number(n) {
cout << "open " << number << endl;
}
Ticket(const Ticket& other) : number(other.number + 10) {
cout << "dup " << number << endl;
}
~Ticket() {
cout << "close " << number << endl;
}
int getNumber() const {
return number;
}
};
void check(Ticket t) {
cout << "check " << t.getNumber() << endl;
}
int main() {
Ticket a(5);
Ticket row[3] = { Ticket(7), Ticket(8) };
{
Ticket b = a;
check(row[1]);
}
Ticket c;
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 Wallet {
private:
const int id;
double cash;
static int count;
public:
Wallet(int i, double c) : id(i), cash(c) {
count++;
}
~Wallet() {
count--;
}
int getId() const {
return id;
}
double getCash() const {
return cash;
}
void add(double x) {
cash += x;
}
void empty() const {
cash = 0; // (1)
}
static double bonus() {
return cash * 0.1; // (2)
}
static int getCount() {
return count;
}
};
int Wallet::count = 0;
void show(const Wallet& w) {
cout << w.getCash() << endl; // (3)
w.add(5); // (4)
}
int main() {
const Wallet w1(1, 50);
Wallet w2(2, 10);
w2.add(10); // (5)
w1.add(10); // (6)
cout << w1.getId() << endl; // (7)
cout << Wallet::getCount() << endl; // (8)
cout << w1.getCount() << endl; // (9)
cout << Wallet::getId() << endl; // (10)
const Wallet w3; // (11)
w2.id = 7; // (12)
return 0;
}Question 3
The class Date is given:
class Date {
private:
int day, month, year;
public:
Date(int d, int m, int y) : day(d), month(m), year(y) {
}
int getDay() const {
return day;
}
int getMonth() const {
return month;
}
int getYear() const {
return year;
}
};Write the class Book, and a main that uses it, in one file after the class Date.
Book has:
- Private data members: a const
int id, aDate publishedand adouble price. - A private static data member
count, the number of books that exist right now. - A constructor that takes the id, the day, the month and the year of publication, and the price. It sets the id and the date in the member initializer list. The price must be positive; if it is not, it stays 0.0.
- A destructor.
isNewerThan(other), which returnstrueif this book was published afterother. Compare the year first, then the month, then the day.print(), which prints the id, the date and the price.- A static function
getCount().
Mark every function that only reads the object as const, and make sure count is correct when books are created and destroyed.
In main, create two books, print them, and print the result of isNewerThan for the two books. Then create a third book inside a block, and print getCount() inside the block and after it.
Question 4
The class Rectangle is given. Assume that <iostream> is included and using namespace std; is written.
class Rectangle {
private:
int width, height;
public:
Rectangle(int w = 1, int h = 1) {
width = w;
height = h;
}
int area() const {
return width * height;
}
void print() const {
cout << width << " x " << height << endl;
}
};Write these two functions. Each one receives an array of rectangles and its size:
largestIndexreturns the index of the rectangle with the largest area.totalAreareturns the sum of the areas of all the rectangles.
Then write a main that creates an array of 4 rectangles, the last one with no arguments, prints the four rectangles, and prints the index of the largest rectangle and the total area.
Question 5
Multiple choice. For each part, answer the question below the code, then press Check answers.
(a)
void show(Ticket t) { }
Ticket a(5);
Ticket b = a;
show(a);
Ticket c;
c = a;This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
c = a;line 7 of the programTicket has a copy constructor. How many times is it called?
(b)
Counter c1, c2;
{
Counter c3;
}
cout << Counter::getCount();This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
cout << Counter::getCount();line 5 of the programCounter has a static data member n that starts at 0. Its constructor does n++, and it has no destructor. What does the last line print?
(c)
class Car {
private:
Engine engine;
Radio radio;
public:
~Car() { }
};This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
Radio radio;line 4 of the programCar has the members Engine engine; and then Radio radio;. In which order do the destructors run when a Car object is destroyed?
(d)
const Rectangle r(2, 3);
r.area();
r.setWidth(4);This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
r.setWidth(4);line 3 of the programRectangle has double area() const and void setWidth(double w). Which calls compile?