Exercises
3.1. Exercises#
Many of these exercises are taken from past exams of ECE 244 Programming Fundamentals courses at University of Toronto. The solutions are provided in the answer boxes.
Headings in this page classify the exercises into different categories: [Easy], [Intermediate], and [Challenging]. I suggest you start by easy exercises and work your way up to the challenging ones.
Question 9 in Fall 2022 Midterm Exam [Intermediate]
Write down the standard output of the following program. Remember to write two “Check Point”, since partial marks are given based on these “stop points”. You might find it helpful to write down the memory layout.
#include <iostream>
using namespace std;
int i[5] = {0, 2, 4, 6, 8};
int* p;
void foo() {
cout << *p << endl;
++(*p);
++p;
}
void bar() {
for (int i = 0; i < 3; ++i) {
foo();
}
}
int main() {
p = i;
bar();
cout << "Check Point 1" << endl;
p = i;
foo();
cout << "Check Point 2" << endl;
return 0;
}
Answer
0 2 4 Check Point 1 1 Check Point 2
Question 3 in Fall 2021 Final Exam [Intermediate]
Consider the following code snippet that manipulates pointers in a main function of a C++ program.
int* p = nullptr;
int* q = nullptr;
int* r = nullptr;
int** t = &p;
int** s = &q;
r = p;
p = new int;
q = new int;
*p = 5;
*q = 2;
**s = *p + **t;
Which of the following statements (that come after the above snippets executes) prints 5 to the standard output? You may assume iostream
is included and the std
namespace is used. Choose all correct answers.
cout << r;
cout << *t;
cout << *q;
cout << *p;
cout << **t;
cout << *r;
cout << *s;
cout << (**s) / 2;
Answer
cout << *p;
cout << **t;
cout << (**s) / 2;
Question 4 in Fall 2018 Midterm Exam [Intermediate]
Consider the following main
function. The line numbers to the left are for reference and are not part of the code.
1#include <iostream>
2using namespace std;
3
4int main() {
5 int* first_ptr;
6 int* second_ptr;
7 int** p_ptr;
8 first_ptr = new int;
9 second_ptr = new int;
10 p_ptr = &first_ptr;
11 *first_ptr = 4;
12 *second_ptr = 8;
13 second_ptr = *p_ptr;
14 cout << *first_ptr << " " << *second_ptr << endl;
15 delete first_ptr;
16 delete second_ptr;
17 delete *p_ptr;
18 return (0);
19}
What is the output produced by
cout
on line 14 of the codeAnswer
The output is
4 4
The program may have a problem with it. What is the problem, if any? Circle only one answer.
The program has no problem with it
The program has a memory leak.
The delete on line 17 should not dereference p_ptr, but use it directly.
The program deletes the same region of memory more than once.
2 and 3.
2 and 4.
2, 3 and 4.
Answer
The program has a memory leak because in line 13, it changes what
second_ptr
is pointing to without freeing up the memory space it was pointing to.second_ptr
,first_ptr
, and*p_ptr
are all pointing to anint
with \(4\), while there are three deletes for the same memory space.
Question 2 in Fall 2017 Midterm Exam [Intermediate]
Consider the following program.
class Point {
int x;
int y;
public:
Point(int i, int j);
Point increment_x();
Point increment_y();
void print() const;
};
Point::Point(int i, int j) {
x = i;
y = j;
}
Point Point::increment_x() {
++x;
return *this;
}
Point Point::increment_y() {
++y;
return *this;
}
void Point::print() const {
cout << "(" << x << "," << y << ")" << endl;
}
int main() {
Point a(2, 3);
// Evaluation is done left to right
a.increment_x().increment_y().print();
a.print();
return 0;
}
Assuming the C++ compiler does not optimize away copying of objects. Write the output produced by the program.
Answer
(3,4)
Recall, this
is a pointer to the object itself.
a.increment_x()
is evaluated first, and would increment the x
of object a
making x
of a
equal to 3. This would return a new object with x = 3
and y = 3
. On this new object, you would call increment_y()
, which would increment y
to 4 and return a new object with x = 3
and y = 4
. Printing this new object would produce (3,4)
.
(3,3)
Printing object a
would output (3,3)
due to the previous a.increment_x()
call.