Sample C++ MidTerm


Name: _______________________Section: ____ ID: _________________

Part I – Multiple Choice

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

    1. while
    2. = (the assignment statement)
    3. cout
    4. #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; is executed?

    1. 5
    2. 15
    3. the value is unknown
    4. the statement is illegal

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

    1. _something
    2. aVariable
    3. float2string
    4. 2manyLetters
    5. x

  4. Which numeric data type has the largest range?

    1. int
    2. char
    3. float
    4. double

  5. Suppose we want to store the value 1.5 into a double variable d. Which of the following would not work?

    1. d = 3.0/2;
    2. d = 1.5;
    3. d = 3/2;
    4. d = 1 + 0.5;
    5. all of the above will work

  6. Consider the following code fragment carefully, then answer the question: how many times will the cout statement execute:

  7. for (i = 0; i < 5; i++);
      cout <<  i;
    1. 5 times
    2. 4 times
    3. 6 times
    4. 0 times
    5. 1 time

  8. What are the three basic control structures used in programming?

    1. int, double, string
    2. while, do..while, for
    3. sequence, decision, repetition
    4. input, output, and calculation

  9. A loop exit condition must

    1. be the last instruction in the body of the loop
    2. evaluate to true or false
    3. be the first instruction in the body of the loop
    4. not use compound conditions

  10. 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?

    1. while
    2. for
    3. if..else
    4. sequence
    5. cin

  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 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?

    1. (ptX < left && ptX > right && ptY > bot && ptY < top)
    2. (ptX < left || ptX > right || ptY > bot || ptY < top)
    3. (ptX > left && ptX < right && ptY < bot && ptY > top)
    4. (ptX < left || ptX > right || ptY < bot || ptY > top)
    5. none of the above

  12. 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?

    1. F = = G*m1*m2/d^2;
    2. F = G*m1*m2/d*d;
    3. F = G*m1*m2/(d*d);
    4. a or c is correct
    5. 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.

  2. Write a statement to display a double in a field 10 characters wide using two decimal places.

  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.

  4. ___ 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

  5. Consider the following:

  6. int x;
    double n1 = 5, n2 = 2;
    
    x = n1/n2;

    What value is in x? __________

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

  8. 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

  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.

  3. In many cases when we want to find the average of a set of numbers, we throw away the extreme values, either because they

  4. 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