19. Week 10 Thursday: Final Review!
≪ 18. Week 10 Tuesday: Pointers and Arrays | Table of ContentsBefore 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
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:
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}
01- 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
fandghave runtime errors. - Both
fandghave undefined behaviour. fhas a runtime error whileghas undefined behaviour.ghas a runtime error whilefhas undefined behaviour.
Problem 3.
Consider the following snippet of code:
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, andint int int.double double int,double double int, andint 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.
Problem 6.
Problem 7.
Predict the Output
Determine the output of the following programs.
Problem 8.