Sample C++ MidTerm Answers
Sample C++ MidTerm Answers
a. while
b. = (the assignment statement)
c. cout
d. #define
Of the above answers, while is the only one that can alter the flow of program
execution.
a. 5
b. 15
c. the value is unknown
d. the statement is illegal
The instruction takes the contents of x, multiplies it by 5 and then assigns it back
to x. 3 * 5 = 15.
a. _something
b. aVariable
c. float2string
d. 2manyLetters
e. x
Variable names can ONLY start with _ or an upper or lower case letter.
a. int
b. char
c. float
d. double
5. Suppose we want to store the value 1.5 into a double variable d. Which
of the following
would not work?
a. d = 3.0/2;
b. d = 1.5;
c. d = 3/2;
d. d = 1 + 0.5;
e. all of the above will work
a. 5 times
b. 4 times
c. 6 times
d. 0 times
e. 1 time
The semi-colon at the end of the for loop causes the loop to simply increment the
variable i from 0 to 5. Once i has the value 5, the condition is no longer true and
program control moves to the next line of executable code, which is the cout
statement.
Sequence, decision (if or switch) and repetition (for, while, or do...while) are all
ways to alter the flow of the program.
a. while
b. for
c. if..else
d. sequence
e. cin
a. (ptX < left && ptX > right && ptY > bot && ptY < top)
b. (ptX < left || ptX > right || ptY > bot || ptY < top)
c. (ptX > left && ptX < right && ptY < bot && ptY > top)
d. (ptX < left || ptX > right || ptY < bot || ptY > top)
e. none of the above
a. F = = G*m1*m2/d^2;
b. F = G*m1*m2/d*d;
c. F = G*m1*m2/(d*d);
d. a or c is correct
e. b or c is correct
___ exponentiation
1 sub-expressions in parenthesis
x = n1/n2;
What value is in x? 2
Time t = 0, h = 3 + 0 - 0 = 3
Time t = 2, h = 3 + 20 – 4 = 19
When the ball hits the ground, h becomes negative. Write a fragment of
C++ code to
print out the height, h, once every second, starting a t = 0,
and continuing until h becomes
negative (meaning the ball has hit the ground).
(Do not print the negative value.) Include
all necessary data declarations
and initializations.
int h, t;
t = 0; //start at time 0
h = 3; //the initial value for h when t is 0
while ( h >= 0 )
{
cout << "Height: " << h << endl;
t = t + 1;
h = 3 + ( 10 * t ) - ( t * t );
}
#include <iostream>
#include <iomanip>
#define LOW 32
#define HIGH 212
#define EXIT -99
int main()
{
int userVal, betweenCnt = 0, extremeCnt = 0, sum = 0;
double avg;
cout << "Enter a number (" << EXIT << " to quit):";
cin >> userVal;
else
{
sum += userVal;
betweenCnt++;
}
cout << "Enter a number (" << EXIT << " to quit):";
cin >> userVal;
}
//If the user entered at least 1 valid number, calculate the average
//and then display it and the number of extreme values that were
//entered.
if ( betweenCnt > 0 )
{
avg = (double) sum / betweenCnt;