Hunter Liu's Website

9. Week 5 Tuesday: Midterm Practise

≪ 8. Week 4 Thursday: Break, Continue, and Some Common Errors | Table of Contents | 10. Week 5 Thursday: Functions and Function Scope ≫

There are three broad types of problems on coding exams: predict the output, catch the error, and writing code. In the first category, you’ll be given a snippet of code and asked to predict what happens. In the second, you’ll be given a snippet of code that may fail to compile or fail to run properly, and you’ll be asked to identify the problem and propose a fix. The last category is hopefully self-explanatory.

Here are a few problems of each type to practise with, arranged in no particular order.

Predict the Output

Problem 1.

Suppose the user enters the following input, with newline characters displayed explicitly:

147.33\n
PIC 10A ROOLS\n

Determine the output of the following program:

 1#include <iostream> 
 2#include <string> 
 3
 4using namespace std; 
 5
 6int main() {
 7    char c1, c2, c3; 
 8    string s1, s2; 
 9    int i; 
10    double d; 
11
12    c1 = cin.peek(); 
13    cin >> i; 
14    c2 = cin.peek(); 
15    getline(cin, s1); 
16    cin.ignore(); 
17    cin >> s2 >> d >> c3; 
18
19    // prints out the variables in the order
20    // they were declared. 
21    cout << c1 << endl; 
22    cout << c2 << endl; 
23    cout << c3 << endl; 
24    cout << s1 << endl; 
25    cout << s2 << endl; 
26    cout << i << endl; 
27    cout << d << endl; 
28    return 0; 
29} 

Problem 2.

Predict the output of the following code:

 1#include <iostream> 
 2#include <string> 
 3
 4using namespace std; 
 5
 6int main() {
 7    for(int i = 0; ++i <= 10; i++) {
 8    for(string s; s.length() <= i; s.push_back('*')) {
 9        if(s.length() == i) { 
10            cout << s << endl; 
11        } 
12    }}  // don't worry, all curly braces are here. 
13
14    return 0; 
15} 
Hint
Rewrite the for loops as while loops. In words, what is the inner loop doing?

Problem 3.

Suppose the user enters the input Spitting Jonny on a single line. Predict the output of the following program.

 1#include <iostream> 
 2#include <string> 
 3
 4using namespace std; 
 5
 6string mystery(string s) {
 7    if(s.length() == 0) {
 8        return ""; 
 9    } 
10
11    string output; 
12    output = s.substr(s.length() / 2); 
13    return output + s.substr(0, s.length() / 2); 
14} 
15
16int main() {
17    cout << "Please enter your first and last name." 
18         << endl; 
19
20    string first, last; 
21    cin >> first >> last; 
22
23    cout << mystery(first) << " " 
24         << mystery(last) << endl; 
25
26    return 0; 
27} 

Catch the Error

Problem 4.

The following code asks the user for a line of input, then counts the number of uppercase letters. Find and classify each error, then propose a fix for each.

 1#include <iostream> 
 2#include <string> 
 3
 4using namespace std; 
 5
 6int main() {
 7    string input; 
 8    getline(cin, input); 
 9
10    int counter = 0; 
11    for(int i = 0; i <= input.length(); i++) {
12        if('A' <= input.at(i) <= 'Z') {
13            counter++; 
14        } 
15    } 
16
17    cout << counter << endl; 
18    return 0; 
19} 

Problem 5.

The following code asks the user for an integer and determines if it’s prime or not. Find, classify, and fix the errors in the code, assuming the user provides valid input.

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5int main() {
 6    cout << "Please enter an integer, " 
 7         << "and I'll determine if it's prime." 
 8         << endl; 
 9    int n; 
10    cin >> n; 
11
12    // for every number between 2 and n - 1, 
13    // check if it's a divisor of n. 
14    for(int d = 2; d < n; d++) {
15        if(n % d == 0) { 
16            bool is_prime = false; 
17            break; 
18        } else {
19            bool is_prime = true; 
20            continue; 
21        } 
22    } 
23
24    if(is_prime) {
25        cout << "You entered a prime number!" 
26             << endl; 
27    } else {
28        cout << "You entered a composite number!" 
29             << endl; 
30    } 
31
32    return 0; 
33} 

Problem 6.

The following code asks the user for their grade in a class as a number between 0 and 100, then converts it to a letter grade. Find, classify, and fix the errors, assuming the user provides valid input.

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5int main() {
 6    cout << "Please enter your current numerical grade, "
 7         << "and I will determine your letter grade." 
 8         << endl; 
 9    
10    double grade; 
11    cin >> grade; 
12
13    char letter_grade; 
14    if(grade >= 90) {
15        letter_grade = 'A'; 
16    } 
17
18    if(grade >= 80) { 
19        letter_grade = 'B'; 
20    } 
21
22    if(grade >= 70) {
23        letter_grade = 'C';
24    } 
25
26    if(grade >= 60) {
27        letter_grade = 'D'; 
28    } 
29
30    if(grade <= 59) {
31        letter_grade = 'F'; 
32    } 
33
34    cout << "You are in the " << letter_grade 
35         << " range." << endl; 
36    
37    return 0; 
38} 

Do It Yourself

Problem 7.

Write a C++ program that accepts a positive integer n as input from the user, then prints out the outline of an (n\times n) squares of *’s.

SAMPLE RUN
INPUT   5
OUTPUT  *****
OUTPUT  *   *
OUTPUT  *   *
OUTPUT  *   *
OUTPUT  *****

Problem 8.

Fill in the contents of the following C++ function, which accepts an integer n as a parameter and returns true if n is a perfect square (and returns false otherwise).

1bool is_square(int n) {
2    /* Fill this in... */ 
3} 

Problem 9.

Write a C++ function named letter_interval that accepts two lowercase letters c1 and c2 and returns a string containing every lowercase letter between c1 and c2, inclusive.

 1#include <iostream> 
 2#include <string> 
 3
 4using namespace std; 
 5
 6/* YOUR FUNCTION GOES HERE */ 
 7
 8int main() {
 9    // output should be: 
10    // abcde
11    // mnopqrs
12    cout << letter_interval('a', 'e') << endl; 
13    cout << letter_interval('s', 'm') << endl; 
14    return 0; 
15}