Hunter Liu's Website

19. Week 10 Thursday: Final Review!

≪ 18. Week 10 Tuesday: Pointers and Arrays | Table of Contents

Before providing a collection of exam-style practise questions to discuss, I’d like to quickly comment on the const keyword and how it interacts with references and pointers.

One should imagine that the const keyword describes to C++ which variable names are allowed to be modified. Saying const int i = 7; says to create an int-sized box called i, and the variable i cannot be modified. The code

1const int i = 7; 
2int& r = i; 

is a build error, and C++ holds dearly in its heart that it will never downgrade a const variable to a non-const variable. However, it is okay with upgrading to a const:

1int i = 7; 
2const int& r = i; 

In this setup, the variable name i is modifiable, but the variable name r is not.

Things are a little trickier with pointers. Consider the following code:

 1int i = 7; 
 2const int* p1 = &i; 
 3int* const p2 = &i; 
 4const int* const p3 = &i; 
 5
 6*p1 = 1; // build error 
 7*p2 = 1; // okay 
 8*p3 = 1; // build error 
 9
10int j = 8; 
11p1 = &j; // okay 
12p2 = &j; // build error 
13p3 = &j; // build error 

To understand the difference between the three pointers, we should read the types from right to left. const int* p1 says that p1 is a pointer to an integer that’s constant. Thus, trying to say *p1 = 1 is a build error — the address stored in the variable p1 is that of a constant integer. However, saying p1 = &j is okay, since the variable p1 itself is allowed to change.

int* const p2 says that p2 is a constant pointer to a (non-constant) integer. Thus *p2 = 1 is okay, but p2 = &j is not. The address stored in p2 is not allowed to change, but the variable referenced by this address is allowed to change.

Finally, const int* const p3 says that p3 is a constant pointer to an integer that’s constant. Both *p3 = 1 and p2 = &j are build errors: the address stored in p3 is that of a constant integer, and the address itself cannot change!

The questions provided below fall into three broad categories, based heavily upon the midterm format. Since the professor has graciously uploaded a practise final, I’ll aim to cover topics and concepts his practise final omits.

Multiple Choice Questions

Select the correct answer. There will only be one correct answer in each of the following.

Problem 1.

What is the output of the following program?

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5int main() {
 6    int* ptr; 
 7    for(int i = 0; i < 1; i++) {
 8        ptr = &i; 
 9    } 
10
11    cout << *ptr << endl; 
12    return 0; 
13} 
  • 0
  • 1
  • Undefined behaviour
  • Build error

Problem 2.

Consider the following two functions:

1void f(vector<int> v) {
2    cout << v.at(v.size() + 1) << endl; 
3} 
4
5void g(vector<int> v) {
6    cout << v[v.size() + 1] << endl; 
7} 

Do either or both of the functions encounter runtime errors and/or undefined behaviour?

  • Both f and g have runtime errors.
  • Both f and g have undefined behaviour.
  • f has a runtime error while g has undefined behaviour.
  • g has a runtime error while f has undefined behaviour.

Problem 3.

Consider the following snippet of code:

1int i; 
2char c; 
3double d; 
4string s; 
5cin >> i >> c >> d; 
6getline(cin, s); 
7cout << s << endl; 

Suppose the user enters the input 11 14.5 18, hits the enter key, then writes “ i love getline” and hits the enter key again. What is the output of the above code?

  • 18 (with a leading space).
  • 18 (without a leading space).
  • i love getline (with a leading space).
  • i love getline (without a leading space).

Problem 4.

What is the output of the following program?

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5void f(int i, int j, int k = 7) {
 6    cout << "int int int" << endl; 
 7} 
 8
 9void f(int i, double d, int j = 3) {
10    cout << "int double int" << endl; 
11} 
12
13void f(double d, double e, int i = 1) {
14    cout << "double double int" << endl; 
15} 
16
17int main() {
18    f(1.5, 2); 
19    f(3, 1.5, true); 
20    f('c', 1); 
21    return 0; 
22} 
  • double double int, int double int, and int int int.
  • double double int, double double int, and int int int.
  • Build error.

Catch the Error

Determine if there are build errors, runtime errors, and/or undefined behaviour in the following programs. If there are, identify which lines they occur on and explain briefly why they occur. Otherwise, predict the output of the program.

Problem 5.

 1#include <iostream> 
 2using namespace std; 
 3
 4class A {
 5private: 
 6    int i; 
 7
 8public: 
 9    A(int _i) : i(_i) {} 
10    void print() {
11        cout << "An A object with i = " << i << endl; 
12    } 
13}; 
14
15void main() {
16    const A a(15); 
17    a.print(); 
18
19    return 0; 
20} 

Problem 6.

 1#include <iostream>
 2using namespace std; 
 3
 4struct B {
 5    int* ptr; 
 6    B(int i) : ptr(&i) {} 
 7
 8    void increment() {
 9        *ptr += 1; 
10    } 
11};  
12
13int main() {
14    int a = 15; 
15    B b(a); 
16    b.increment();  
17    cout << a << endl; 
18
19    return 0; 
20} 

Problem 7.

 1#include <iostream> 
 2#include <vector> 
 3using namespace std; 
 4
 5int main() {
 6    vector<int> v = {1, 1, 2, 3, 5, 8}; 
 7    vector<int> * const p = &v; 
 8
 9    p.push_back(13); 
10    cout << v.at(6) << endl; 
11    return 0; 
12} 

Predict the Output

Determine the output of the following programs.

Problem 8.

 1#include <iostream> 
 2using namespace std; 
 3
 4struct A {
 5    A() { 
 6        cout << "A "; 
 7    } 
 8
 9    A(int i) {
10        cout << "A" << i << " "; 
11    } 
12} 
13
14struct B {
15    A a; 
16
17    B(A _a) { 
18        a = _a; 
19        cout << "B2 "; 
20    } 
21} 
22
23int main() {
24    A a1; 
25    A a2(15); 
26    B b1(a2); 
27        
28    return 0; 
29} 

Problem 9.

 1#include <iostream>
 2using namespace std; 
 3
 4void f(int* p, int** pp) {
 5    *p = **pp; 
 6    *pp = p; 
 7    pp = &p; 
 8    
 9    **pp = 7; 
10} 
11
12int main() {
13    int n = 5; 
14    int m = 18; 
15    int* p = &m; 
16
17    f(&n, &p); 
18
19    cout << n << ' ' << m << ' ' 
20         << *p << endl; 
21    return 0; 
22}