CSCI 240 – Sample C++ MidTerm
Note: this test is somewhat longer than an actual midterm
Name: _______________________ Section: ____ ID: _________________
Part I – Multiple Choice
1. Which of the following is a program control instruction?
a. while
b. = (the assignment statement)
c. cout
d. #define
2. If x contains the value 3 before the following instruction is executed, what is the value of x after the instruction: x *= 5;
a. 5
b. 15
c. the value is unknown
d. the statement is illegal
3. Which of the following is not a legal variable name?
a. _something
b. aVariable
c. float2string
d. 2manyLetters
e. x
4. Which numeric data type has the largest range?
a. int
b. long int
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
6. Consider the following code fragment carefully, then answer the question: how many times will the printf() 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
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
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. A function calling statement must supply arguments that
a. match the function header in number
b. match the function header in number and order
c. match the function header in number, order, and data type
d. match the function header in number, order, data type, and names
e. match the function header in number, names, and data type
11. 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 bot and right. 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
12. Which of the following appear in a function header?
a. the function name, return type, and argument types
b. the function name, return type, argument types and argument names
c. the function name, return type, and argument names
d. the function name and the supplied arguments
13. How does a function report its answer back to its calling (or boss) function?
a. by altering or storing the result into one of the variables passed as an argument
b. by altering or storing the result directly into the caller’s variable
c. by cout – ing the result to the screen
d. by asking the user via an input function
e. by executing a return statement with the answer
14. 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
1. (4) a. Write one statement to display the first 3 letters of the letters of your first name, one per line.
b. Write a statement to display a double in a field 10 characters wide using two decimal places.
2. (4) 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
___ addition and subtraction, left to right
___ addition and subtraction, right to left
___ sub-expressions in parenthesis
___ multiplication and division, left to right
___ multiplication and division, right to left
3. (4) A function may return ____ or _____ result(s). It may take _____ to _____ arguments.
4. (4) Consider the following:
int x;
double n1 = 5, n2 = 2;
x = n1/n2;
What value is in x? ________
5. (4) a. What symbol in C++ is used to express a logical "or"? ________
b. What symbol in C++ is used to express a logical "and"? ________
1. (10) Write a function that takes a real number of ounces (representing the weight of a letter) and returns an integer number of cents which represents the cost of mailing the letter. The rules for calculating the cost are as follows:
· If the weight is less than or equal to 1 ounce, the cost is 33 cents
· If the weight is greater than 1 ounce but less than or equal to 1.5 ounces, the cost is 40 cents
· If the weight is greater than 1.5 ounces but less than 2 ounces, the cost is 50 cents
· If the weight is greater than 2 ounces, the cost is 30 times the number of ounces
2. (10) 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.
3. (20) 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