[go: up one dir, main page]

0% found this document useful (0 votes)
65 views6 pages

Sample C++ MidTerm Answers

The document contains sample answers to a C++ midterm exam, with the correct answers highlighted in red. Part I contains 10 multiple choice questions testing concepts like program control flow, variable assignment, data types, loops, and logical operators. Part II contains 5 short answer questions asking the student to write code snippets related to output formatting, arithmetic evaluation order, and logical operators. Part III contains 2 coding questions - one to print a ball's falling height over time using a formula, and another to calculate the average of a series of user-input numbers while excluding values outside a given range.

Uploaded by

Momo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Sample C++ MidTerm Answers

The document contains sample answers to a C++ midterm exam, with the correct answers highlighted in red. Part I contains 10 multiple choice questions testing concepts like program control flow, variable assignment, data types, loops, and logical operators. Part II contains 5 short answer questions asking the student to write code snippets related to output formatting, arithmetic evaluation order, and logical operators. Part III contains 2 coding questions - one to print a ball's falling height over time using a formula, and another to calculate the average of a series of user-input numbers while excluding values outside a given range.

Uploaded by

Momo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Sample C++ MidTerm Answers

The correct answers are highlighted in red.

Part I – Multiple Choice


1. Which of the following is a program control instruction?

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.

2. If x contains the value 3 before the following instruction is


executed, what is the value of
x after the instruction: x *= 5;
is executed?

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.

3. Which of the following is not a legal variable name?

a. _something
b. aVariable
c. float2string
d. 2manyLetters
e. x

Variable names can ONLY start with _ or an upper or lower case letter.

4. Which numeric data type has the largest range?

a. int
b. char
c. float
d. double

double has a range of + 10308

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

3/2 is integer division, which means that the result will be 1.

6. Consider the following code fragment carefully, then answer the


question: how many
times will the cout statement execute:
for (i = 0; i < 5; i++);
cout << i;

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.

7. What are the three basic control structures used in


programming?

a. int, double, string


b. while, do..while, for
c. sequence, decision, repetition
d. input, output, and calculation

Sequence, decision (if or switch) and repetition (for, while, or do...while) are all
ways to alter the flow of the program.

8. A loop exit condition must

a. be the last instruction in the body of the loop


b. evaluate to true or false
c. be the first instruction in the body of the loop
d. not use compound conditions

9. If the logic of your program at some point requires you to do one


thing or another,
which instruction would you use to implement
this decision?

a. while
b. for
c. if..else
d. sequence
e. cin

10. Suppose you have a point on a plane represented by the variables


ptX and ptY (its x and
y coordinates). Suppose you also have a
rectangle, whose upper left corner is
represented by the variables left
and top, and whose bottom right corner is represented
by the variables
right and bot. Using the standard graphics coordinate
system, in
which y increases from top to bottom and x increases from left
to right, which of the
following conditions will test if the point is
outside the rectangle?

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

Hint: Draw the rectangle.

11. The force of gravitational attraction, F, of two bodies is


given by a formula in which a
constant, G, is multiplied by the
product of the two masses (m1 and m2). This is then
divided by
the square of the distance, d, between the two bodies. Assuming these
variables are declared, and have proper initial values where necessary, which
of the
following C++ statements correctly expresses this formula?

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

Part II - Short Answer


1. Write one statement to display the first 3 letters of
your first name, one per line.

cout << "J" << endl <<


"o" << endl << "e"; === or === cout <<
"J\no\ne";

2. Write a statement to display a double in a field 10 characters wide


using two decimal
places.

cout << setiosflags(ios::fixed) << setprecision(2) << setw(10) << doubleNum;

3. Rank the order in which arithmetic statements are evaluated (place


numbers 1, 2, ... in
the space provided. If a rule is given that is not a
correct rule, do not place a number next
to it.

___ exponentiation

3 addition and subtraction, left to right

___ addition and subtraction, right to left

1 sub-expressions in parenthesis

2 multiplication and division, left to right

___ multiplication and division, right to left

4. Consider the following:


int x;
double n1 = 5, n2 = 2;

x = n1/n2;

What value is in x? 2

5. a. What symbol in C++ is used to express a logical "or"?


||

b. What symbol in C++ is used to express a logical "and"?&&

Part III – Coding


1. A ball is thrown up in the air. Its height above the ground is given
by the formula h = 3 +
10t – t2 where t is the time in seconds
after it is thrown. For example, at

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 );
}

2. In many cases when we want to find the average of a set of numbers, we


throw away the
extreme values, either because they

are not representative (i.e. a 0 on a quiz because the person was


absent)
are presumed to be errors in measurement (in a bunch of 80o
temperatures there is
a 180o reading – someone used a magnifier
to focus the sun’s rays...)

Write a complete program to read a series of numbers from the


keyboard. Use an end-
of-input signal value of –99. Calculate and print the
average of the numbers between 32
and 212. Any number below 32 or above 212
should not be included in the average
calculations, but you should
keep a count of them and print that at the end of the
program. A run of the
program should resemble the following:

Enter a number (-99 to quit): 22


Enter a number (-99 to quit): 44
...
Enter a number (-99 to quit): -99

Average is: 45.45

There were 3 extreme values

#include <iostream>
#include <iomanip>

using namespace std;

#define LOW 32
#define HIGH 212
#define EXIT -99

int main()
{
int userVal, betweenCnt = 0, extremeCnt = 0, sum = 0;
double avg;

//get the first number from the user

cout << "Enter a number (" << EXIT << " to quit):";
cin >> userVal;

//while the user has not entered the exit value

while( userVal != EXIT )


{
//if the user's value is an extreme, then increment the count of
//extreme values. Otherwise, add the user's value to the running
//total and increment the count of the valid values

if( userVal < LOW || userVal > HIGH )


extremeCnt++;

else
{
sum += userVal;
betweenCnt++;
}

//get the next number from the user

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;

cout << endl << endl << "Average is: "


<< setiosflags(ios::fixed) << setprecision(2) << avg
<< endl << "There were " << extremeCnt << " extreme values";
}
return 0;
}

You might also like