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?

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

    1. 5
    2. 15
    3. the value is unknown
    4. 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?

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

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

  4. Which numeric data type has the largest range?

    1. int
    2. char
    3. float
    4. 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?

    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

    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:

  7. for (i = 0; i < 5; i++);
      cout <<  i;
    1. 5 times
    2. 4 times
    3. 6 times
    4. 0 times
    5. 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.

  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

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

  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

    Hint: Draw the rectangle.

  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. cout << "J" << endl << "o" << endl << "e"; === or === cout << "J\no\ne";

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

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

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

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

  7. Consider the following:

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

    What value is in x? 2

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

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

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

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