YOUR NAME:
CSCI 159 Quiz 2 (F24N01/02) Monday lab
The quiz is closed book, closed notes, no electronics permitted, and to be completed as a
strictly individual exercise. You are permitted one double-sided 8.5" x 11" sheet of notes.
The quiz is worth a total of 10 marks. Attempt all questions, answering directly on the exam paper.
Note that when a code segment is requested you are only expected to put the requested lines of code,
not the rest of a larger program it would be part of. If a complete program is requested, on
the other hand, you should provide the entire program (#includes, int main, etc).
Question 1 [4 marks]
Consider the function prototype below then answer parts 1-3.
// computes and returns pi to the power of p
float powerPi(int p);
- What is the return type of the function?
- What is the return type of the parameter?
- Assuming it was part of a correct main routine (and larger program), write a single
line of code that makes a call to the function, passing 7 as the parameter and storing the
returned value into a variable named 'answer'.
Question 2 [3 marks]
For the code segment below (assuming it's part of a larger correct program) answer questions 1 and 2.
// beginning of code segment
if (a != b) {
cout << "a is " << a << endl;
if (b < 0) {
cout << "b is negative" << endl;
} else {
cout << "b is not negative" << endl;
}
}
// end of code segment
- As precisely as possible, what will be its output if a is 7 and b is 3?
- As precisely as possible, what will be its output if a and b are both -1?
Question 3 [3 marks]
As precisely as possible, show the output from the following program assuming the user types in 4
when they run it.
#include <iostream>
void q3(int i, int n);
int main()
{
int x;
std::cout << "Enter an integer: ";
std::cin >> x;
q3(1, x);
}
void q3(int i, int n)
{
if (i > n) {
std::cout << "done!" << std::endl;
} else {
int v = i * 5;
std::cout << v << std::endl;
q3(i+1, n);
}
}