12. Week 6 Tuesday: Midterm Retrospective
≪ 11. Week 5 Thursday: Blackjack | Table of Contents | 13. Week 6 Thursday: Vectors ≫I had originally planned to cover vectors, but in an unpredictable turn of events we ended up going over some problems f rom the midterm exam. I’ll recall what we discussed here so there’s a written record of it.
Problem 1. (Midterm 2.2)
Predict the output of the following snippet of code:
The answer is 1 0. Although the variable tmp only exists within the three scoped lines of code, the variables a and b exist throughout the entire program. So while tmp gets demolished after the curly braces are closed, the changes that occur to the variables a and b persist. (It looks like about a third of students answered 0 1.)
The moral here is that scope only affects the lifetime of a variable, e.g. a box of data in memory, and not its contents.
Problem 2. (Midterm 1.5)
What is the output of the following code?
f- Runtime Error
- Undefined Behaviour
The answer is choice b. Since 8 is not a valid index in the string s, the .at function will intentially crash the program (also known as “throwing an exception”): this is described in the C++ reference page for string::at. The .at function is paranoid and will ensure that whatever index is being accessed is a legitimate index.
In contrast, though, the alternative s[8] will produce undefined behaviour; this is also described in the C++ reference page for string::operator[]. The brackets behave just lite .at does when a valid index is supplied; however, the brackets are not always as cautious as .at and won’t necessarily check if the index really exists. How this is handled depends on your implementation of the string library!
(The majority, 80%, of students got this correct.)
Problem 3. (Midterm 2.5)
Predict the output of the following code:
Let us trace through the program step-by-step.
sis initialised to"abcdef", andindis the index of the first occurrence of the character'c'. Thusindis initialised to2.- At the beginning of the for loop,
iis initialised tos.size(), which is6. - The for loop checks
i > ind. Since6 > 2, it executes the body.sis changed to"abcde"afterpop_backis called;iis changed to5after the decrement--i. - The for loop checks
i > ind. Since5 > 2, it executes the body.sis changed to"abcd"afterpop_backis called;iis changed to4after the decrement--i. - The for loop checks
i > ind. Since4 > 2, it executes the body.sis changed to"abc"afterpop_backis called;iis changed to3after the decrement--i. - The for loop checks
i > ind. Since3 > 2, it executes the body.sis changed to"ab"afterpop_backis called;iis changed to2after the decrement--i. - The for loop checks
i > ind. Since2 > 2is false, the loop ends. - The contents of
sare printed, yielding an output ofab.
It looks like most answers were split between ab and abc, and for this the order in which the for loop checks the condition and performs the updates becomes paramount.
Problem 4. (Midterm 3.1)
Identify, explain, and classify the errors in the following code:
I will confide that this is a somewhat ambiguously stated problem. When grading, I looked for at least two build errors, one on line 3 and one on line 4.
- You cannot declare a reference without defining it. This is a build error on line
3. - On line
4, the extra semicolon produces a syntax error (the=1;doesn’t make sense). Alternatively, if one believes the;is a typo, you cannot define a non-constreference as a literal.
The space between the int and the & does not produce a build error; C++ always interprets them as two separate “words” (e.g. “integer reference”), regardless of if there’s a space between them.
It is tempting to say that there is undefined behaviour on line 6. This is accurate if line 3 said int xr; instead. However, since the code does not even build, no undefined behaviour can occur.
Problem 5. (Midterm 2.2)
Predict the output of the following code:
Let’s walk through each line of code:
s1is set to"123,456,789".- A
'1'is appended tos1, making it"123,456,7891". positionis set to the index of the last occurrence of a comma, which is7.- The character at index
position(i.e. index7) ins1is replaced by a0, makings1 = "123,45607891. s1.substr(3, 6)— the first six characters starting at index3— are printed out. This gives an output of,45607.
In taking a random sample of submissions, I observed the following common mistakes:
- Mstaking
rfindforfindand believingposition = 3instead ofposition = 7. - Forgetting that indexing begins at
0and believing the output is3,4560or30456,. - Misunderstanding the meaning of
s1.substr(3, 6)as “the substring from indices 3 through 6”, thus believing the output is,456(or some variant of this).
It looks like around half of students got this correct.
Problem 6. (Midterm 1.3)
Predict the output of the following code:
There were some answer choices, but let’s treat this as a predict the output problem. We should first understand what C++ sees in the if statement. Knowing that find returns -1 if a substring isn’t seen anywhere, we get the code
string s = "AAARGH!!!";
if( -1 + 1 ) { cout << 1 << " "; }
if( 3 + 1 ) { cout << 2 << " "; }
if( 0 + 1 ) { cout << 3 << " "; }
Now C++ must convert these integers in the if statements into booleans. 0’s are false and everything is true, so we should see an output of 2 3.
I should remark that find returns std::npos when a substring isn’t found. This is an alias for the constant -1! std::npos is supposed to be an astronomically large number that will never be reached in a string. Since indices are size_t’s, however, which have to be nonnegative, the constant -1 is interpreted as this astronomically large number as a size_t. If you’re interested, read about the two’s complement system for negative numbers in binary.
(Less than a third of students got this correct. )