CSCI 159: Fall 2026 Midterm Sample Solutions
Sections F26N01-F26N02 (Dr. Wessels)
- You have 80 minutes to complete the exam
- There are 5 questions on the exam, worth 10 marks each.
- Answer directly on the exam paper.
- The exam is closed book, closed notes, no calculators, phones, or other electronic aids
of any form may be used during the exam, but you are permitted one double-sided 8.5x11" sheet
of reference notes ("cheatsheet").
- The use of ear plugs/protection is permitted, but NOT the use of
headphones or headsets (with or without attached devices).
- Be sure to check your exam to make sure all 5 questions are included.
| Question | Mark | Possible |
| 1. Short answer | | /10 |
| 2. Scopes and functions | | /10 |
| 3. Write a program | | /10 |
| 4. Write a recursive function | | /10 |
| 5. Loops | | /10 |
| TOTAL | | /50 |
Question 1: Short answer [10]
Provide valid single-line C++ definitions or statements for each of the following
(assume it is within an otherwise-correct main routine, and that all the needed #includes
and using statements were already provided):
- A definition for an integer variable named year that is initialized to the value 2026.
Sample solution
int year = 2026;
|
- A definition for a real number constant named Pi that is initialized to the value 3.14.
Sample solution
const double Pi = 3.14;
|
- A definition for a character variable named letter, uninitialized.
Sample solution
char letter;
|
- A statement using cout to display the remainder after dividing integer variable i by 3.
(Assume i has been previously declared and initialized.)
Sample solution
cout << (i % 3);
|
- A statement using cin to read a user input into a variable named userData.
(Assume userData has been previously declared and initialized.)
Sample solution
cin >> userData;
|
- A statement that assigns a new value to numeric variable X, where the new
value is twice the current value of X.
(Assume X has been previously declared and initialized.)
Sample solution
x = 2 * x;
|
- A statement using cout, setw, fixed, and setprecision to display a real-number variable named
info in a column of width 20 and with exactly 3 digits of precision after the decimal point.
(Assume info has been previously declared and initialized.)
Sample solution
cout << fixed << setprecision(3) << setw(20) << info;
|
Question 2: Scopes and functions [10]
(i) In the doSomething function in the C++ program below, for the variables, constants, and parameters
used in the function, identify:
- which of them are globals and which are locals,
- which of them are variables, which are constants, and which are parameters,
- which parameters are pass-by-reference and which are pass-by-value.
#include <iostream>
float f = 5.0f;
const int DataValue = 100;
float doSomething(float f, int &x, float val);
int main()
{
int orig = 0;
float num;
std::cout << "Enter a number: ";
std::cin >> num;
num = doSomething(num, orig, f);
std::cout << "The final values are ";
std::cout << num << "," << orig << "," << f << std::endl;
}
float doSomething(float f, int &x, float val)
{
x = DataValue;
float g;
g = (x + val) * f;
return g;
}
Sample solution
Within doSomething, the following are used:
- pass by value parameter f
(the global variable f could only be accessed as ::f inside doSomething)
- pass by reference parameter x
- pass by value parameter val
- local variable g
- the global constant DataValue is accessed inside the function when altering parameter x
while not requires as part of the answer to question (i), main's local variables are orig and num
|
(ii) For the program above, show as precisely as possible the output
that would be produced if the compiled program was run and the user entered the value 20.
Sample solution
Enter a number: (20 gets entered by the user)
The final values are 2100, 100, 5
Explanation:
Main passes num (the user's entered 20) to doSomething in parameter f
orig (0 initially) by reference in parameter x
f (the global one, 5) in parameter val
doSomething
changes x to DataValue, 100, which also changes main's orig because of the pass by ref
sets g to (x + val) * f
i.e. (100 + 5) * 20 since it's the local parameter f not the global variable f
so g is now 2100
returns the 2100
back in main the 2100 is assigned to num,
so orig has now been changed to 100 and num has now been changed to 2100
|
Question 3: Write a program [10]
Write a complete and correct C++ program that does the following:
- Prompts the user to enter two real-number values.
- Reads the two values the user enters, storing them in appropriate variables.
- Uses the sqrt routine (from cmath) to compute the square root of the sum of those
two values and stores the computed value in another variable. (E.g. if they entered 50 and 1.5 then it would compute and store the square root of 51.5.)
- Displays the original two entered values and the computed result, formatted so that each
appears on a separate line and with exactly 3 digits shown after the decimal point.
- Prints a message telling the user which of the two entered values is the smaller and which of the two entered values is the larger.
Sample solution
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
double userVal1, userVal2, result;
std::cout << "Enter two real numbers: ";
std::cin >> userVal1 >> userVal2;
result = sqrt(userVal1 + userVal2);
std::cout << std::fixed << std::setprecision(3);
std::cout << userVal1 << std::endl;
std::cout << userVal2 << std::endl;
std::cout << result << std::endl;
if (userVal1 < userVal2) {
std::cout << "smaller is " << userVal1 << ", larger is " << userVal2 << std::endl;
} else if (userVal1 > userVal2) {
std::cout << "smaller is " << userVal2 << ", larger is " << userVal1 << std::endl;
} else {
// did not ask you for this case, optional
std::cout << "both values are equal: " << userVal1 << std::endl;
}
}
|
Question 4: Write a recursive function [10]
Provide a correct recursive (no loops) implementation of the C++ function described by the comments and
profile given below.
(Assume the rest of the C++ program using this function is already written and correct,
including all the appropriate #includes and usings: do NOT write any code except the function itself.)
// getVal
// prompts the user to enter an integer value less than that contained in the
// MAX parameter (displaying the MAX value as part of the prompt to the user),
// reads their response and checks it for validity (an integer that is less than MAX),
// repeating (recursively) until a valid response is provided
// returns the valid response obtained
long getVal(long MAX)
Sample solution
long getVal(long MAX)
{
long userVal;
cout << "Enter an integer less than " << MAX << endl;
cin >> userVal;
if (cin.fail()) {
cin.clear();
cin.ignore(80, '\n');
cout << "That was not an integer, please try again" << endl;
userVal = getVal(MAX);
} else if (userVal > MAX) {
cout << "That was too large, please try again" << endl;
userVal = getVal(MAX);
}
return userVal;
}
|
Question 5: Loops [10]
The function below has been implemented using a for loop.
Rewrite it to use a while loop instead, keeping the overall logic and behaviour
as close to the original as possible.
// ORIGINAL
void displaySteps(int first, int last, int step)
{
cout << "Displaying steps of size " << step;
cout << " from " << first << " to " << last << endl;
for (int current = first; current < last; current += step)
{
cout << " " << current << endl;
}
}
// YOUR RE-WRITE
void displaySteps(int first, int last, int step)
{
Sample solution
void displaySteps(int first, int last, int step)
{
cout << "Displaying steps of size " << step;
cout << " from " << first << " to " << last << endl;
int current = first;
while (current < last)
{
cout << " " << current << endl;
current += step;
}
}
|