4.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;
}

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.

  1. cout << r;

  2. cout << *t;

  3. cout << *q;

  4. cout << *p;

  5. cout << **t;

  6. cout << *r;

  7. cout << *s;

  8. 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}
  1. What is the output produced by cout on line 14 of the code

  2. The program may have a problem with it. What is the problem, if any? Circle only one answer.

    1. The program has no problem with it

    2. The program has a memory leak.

    3. The delete on line 17 should not dereference p_ptr, but use it directly.

    4. The program deletes the same region of memory more than once.

    5. 2 and 3.

    6. 2 and 4.

    7. 2, 3 and 4.

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.