C++ Quiz 4/20/99
Part A: Mark your answers on the computer grade book program.
- What is the value of the integer expression: 1 - 3 * 5 + 6 / 4 %
2
| (a) |
0 |
| (b) |
-8 |
| (c) |
-13 |
| (d) |
-15 |
- If addition had higher precedence than multiplication, then the value of
the expression
1 + 2 * 3 + 4 * 5
would be which of the following?
| (a) |
65 |
| (b) |
69 |
| (c) |
105 |
| (d) |
135 |
- True or false: The expressions a + b % c and (a + b) %
c are equivalent.
- True or false: The expressions a - b + c and (a - b) +
c are equivalent.
- The value of the expression 19 / 8.0
| (a) |
has type double and value 2.375 |
| (b) |
has type double and value 2 |
| (c) |
has type int and value 2 |
| (d) |
has type int and value 2.375 |
6.. The expression !((x > y) && (y <= 3)) is equivalent
to which of the following?
| (a) |
(x > y) && (y <= 3) |
| (b) |
(x > y) || (y <= 3) |
| (c) |
(x < y) || (y >= 3) |
| (d) |
(x <= y) || (y > 3) |
| (e) |
(x <= y) && (y > 3) |
7. By the rules of short-circuit evaluation, in the following expression,
p || (n < 3)
| (a) |
if p is true, then n < 3 will not be
evaluated |
| (b) |
if p is false, then n < 3 will not be
evaluated |
| (c) |
if p is true, then n < 3 will be
evaluated |
| (d) |
none of the above |
8. What numbers are printed out when the following for loop is
executed?
for (i = 0; i < 12; i++)
cout << i + 1 << endl;
| (a) |
the numbers 0 through 12 (including 0 and 12) |
| (b) |
the numbers 1 through 12 (including 1 and 12) |
| (c) |
the numbers 0 through 11 (including 0 and 11) |
| (d) |
the numbers 1 through 11 (including 1 and 11) |
9. Assume that the following definitions have been made.
int num = 10;
int sum;
Which of the following code segments correctly computes the sum of the first 10 multiples of 5 (i.e., 5, 10, ..., 50)?
| (a) |
sum = 5;
while (num > 1)
{
sum += sum;
num--;
}
|
(d) |
sum = 0;
while (num >= 0)
{
num--;
sum += 5 * num;
}
|
| (b) |
sum = 5;
while (num > 0)
{
sum += sum;
num--;
}
|
(e) |
sum = 0;
while (num > 0)
{
sum += 5 * num;
num--;
}
|
| (c) |
sum = 0;
while (num > 0)
{
num--;
sum += 5 * num;
}
|
10. True or False: When a function is called, the number of arguments should
be the same as the number of parameters in the function's prototype.
11. True or False: A function prototype must include names and types for all
parameters.
12. True or False: The definition of a function must appear in your program
file before any calls to that function.
13. A function f which takes two integer parameters and does not
return any value could have the function prototype:
| (a) |
f(int, int); |
| (b) |
void f(int); |
| (c) |
void f(int, int); |
| (d) |
int f(void, void); |
14. True or False: It would be legal to have these two function prototypes in
the same program:
double f(int);
int f(int);
15. True or False: It would be legal to have these two function prototypes in
the same program:
double f(int, int);
int f(int, double);
16. True or False: Using a call-by-reference parameter, a function can obtain
the initial value of an argument variable as well as change the value of that
variable.
17. Consider the following functions:
void Myst(int a, int& b)
{
a *= b;
b = 2 + a;
}
void Test()
{
int u = 2;
int v = 3;
Myst(u, v);
cout << u << " " << v << endl;
}
What is printed as a result of the call Test()?
| (a) |
2 3 |
| (b) |
2 8 |
| (c) |
6 3 |
| (d) |
6 8 |
| (e) |
8 3 |
Part B:
- Write down (line by line) the output of the following program:
#include <iostream.h>
int main()
{
int a, b;
a = 6;
b = a + 7;
cout << "a = " << a << ", b = " << b << endl;
a += b / 3;
cout << "a = " << a << ", b = " << b << endl;
a = b = 2 * a;
cout << "a = " << a << ", b = " << b << endl;
}
2. Write a program that inputs a list of integers, terminated by entering 0,
then displays the number of even numbers included in the list.
3. Write a for loop to print out the squares of the first 8 odd
numbers (that is, 1, 9, 25, 49, 81, 121, 169, 225). Your solution should use
only one variable.
4. Write down the output of this program.
int deep(int, int);
int space(int);
int main()
{
int a = 1, b = 2, c = 3;
b = space(c);
cout << "main: a = " << a << ", b = " << b << ", c = " << c << endl;
}
int deep(int x, int y)
{
int z;
z = x - y;
cout << "deep: x = " << x << ", y = " << y << ", z = " << z << endl;
return(z);
}
int space(int c)
{
int a = 1, b;
b = c * 2 + a;
a = b + 5;
c = deep(a, b);
cout << "space: a = " << a << ", b = " << b << ", c = " << c << endl;
return(b);
}
5. Write a function hundreds which, given an integer argument,
returns the digit in the hundreds place. If the number is less than 100, it
should return 0. So hundreds(1234) should return 2, and
hundreds(1001) and hundreds(95) should both return 0.
6. Assume that you are designing a program in which you need a void
function called readAndAverage that reads a given number of
double values and returns their average. The number of values to
process is read as user input into a variable in the main program. Write the
function prototype (only) for readAndAverage, using call-by-value or
call-by-reference parameters as appropriate.