Vous êtes sur la page 1sur 3

Beginning Material -- material through the midterm exam

1. What is wrong with the following definition for a C-style symbolic constant
called PI?
#define PI = 3.24159

a. The actual value of the mathematical constant pi is not 3.24159


b. There should be a semi-colon at the end of the line
c. There should not be an equal sign
d. The name PI is a reserved word and therefore cannot be used
e. There is nothing wrong with the definition
The C++ style of symbolic constant uses an equal sign as well as a semicolon. For the symbolic constant above, the definition would be:
const

double

PI = 3.24159;

In either case, the compiler does not do any error checking to make sure
that the appropriate (or correct) value is associated with the constant. That
is up to the programmer.
2. Which of the following is a condition that will test if the value in the integer
variable num is even?
a. (num % 2) == 0
b. (num / 2) == 0
c. (num % 2) == 1
d. (num / 2) == 1
e. none of the above
To test if an integer contains an even value, check if the remainder is equal
to 0 when the integer is divided by 2. If it is equal, then the integer is even.
If it is not equal (i.e.. it is equal to 1 instead), then the integer is odd.

3. Which of the following (a-c) is a control structure?


a. sequence structure
b. repetition structure
c. decision structure
d. all of the above are control structures
e. none of the above are control structures
Four control structures were discussed in lecture: sequence, decision (if
and switch), repetition (for, while, and do...while), and branch and return
(calling functions).
4. How many times will the following while loop print "hello"?
int i = 1;
while( i <= 10 )
cout << "hello"

a. 10
b. 8
c. an infinite number of times
d. 0
This is an infinite loop because the value of i is 1 and that is less than or
equal to 10 and the value of i will remain as 1 because there is nothing in
the loop body to change the value of i.
5. A function prototype does not have to __________.
a. include argument names
b. end with a semi-colon
c. agree with the function header
d. match with all calls of the function

A function prototype supplies the return data type, function name, number
of arguments the function requires, and the data type of any arguments
that the function requires. The argument names, if specified, are ignored.
6. What will be displayed by the following code?
int main()
{
int num = 3;
fn(num);
cout << num;
return 0;
}

void fn( int n )


{
n++;
}

a. The code will not display anything


b. 3
c. 4
d. 5
e. There is not enough information provided to determine what will be
displayed.
The contents of main's integer variable num does not get changed by the fn
function because a copy of the contents of num is being passed to the
function.

Vous aimerez peut-être aussi