Study Guide 2: Core Operations

  1. What does the following code print?
    int x = 10;
    int a = x++;
    cout << a << " " << x << endl;
    1. 11 11
    2. 11 10
    3. 10 10
    4. 10 11
  2. What does the following code print?
    int x = 10;
    int a = --x;
    cout << a << " " << x << endl;
    1. 9 9
    2. 9 10
    3. 10 10
    4. 10 9
  3. What does the following code print?
    int a = 2;
    int b = 3;
    cout << a / b << endl;
    1. 0.666666
    2. 0.666667
    3. 0
    4. 0.000000
    5. 1
  4. What does the following code print?
    int sum = 29;
    int count = 10;
    cout << sum / count << endl;
    1. 2.9
    2. 3
    3. 2
  5. What does the following code print?
    int sum = 29;
    int count = 10;
    cout << (double)sum / count << endl;
    1. 2.9
    2. 3
    3. 2
  6. What does the following code print?
    int sum = 29;
    int count = 10;
    cout << double(sum / count) << endl;
    1. 2.9
    2. 3
    3. 2
  7. What does the following code print?
    double a = 2;
    double b = 3;
    cout << a / b << endl;
    1. 0.666666
    2. 0.666667
    3. 0
    4. 0.000000
    5. 1
    6. The variables a and b are type double and it is an error to assign integer values to them.
  8. What does the following code print?
    cout << 3 % 4 << endl;
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
  9. What does the following code print?
    cout << 5 % 4 << endl;
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
  10. What does the following code print?
    cout << 5 % 5 << endl;
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
  11. What does the following code print?
    cout << 5 % 6 << endl;
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
  12. m and n are integers that are greater than 0. What is the range of possible values produced by the C++ expression m % n (hint: look for a pattern in the last three questions)?
    1. 0 to m
    2. 0 to m - 1
    3. 0 to n
    4. 0 to n - 1
    5. 0 to m - n
    6. 0 to n - m
  13. What is the purpose of the following statement from the money.cpp example program?
    pennies = pennies % 25;
    1. It is the number of quarters that can be converted from the remaining pennies.
    2. It is the number of pennies that can be converted to quarters.
    3. It removes from the variable pennies the number of pennies that was converted to quarters.
  14. A program defines a variable named x. What does the following code print?
    cout << sizeof(x) << endl;
    1. The number of bytes of memory needed to store variable x.
    2. The number of bits of memory needed to store variable x.
    3. This is a syntax error and must be written as:
      cout << sizeof x << endl;
  15. x and y are two double variables. Write an expression to calculate xy.
  16. The payment.cpp example program contains the following statement:
    double payment = principal * R / (1 - pow(1 + R, -N));

    Choose the best statement to describe the two sets of parentheses and the two minus signs.

    1. The outer set of parentheses is for grouping, the inner set of parentheses calls a function, and both minus signs are the arithmetic subtraction operator.
    2. The outer set of parentheses is for grouping, the inner set of parentheses calls a function, the first minus sign is the arithmetic subtraction operator, and the last minus sign is the unary negation operator.
    3. Both sets of parentheses are for grouping, and both minus signs represent the arithmetic subtraction operation.
    4. Both sets of parentheses are for grouping, the first minus sign is the arithmetic subtraction operator, and the last minus sign is the unary negation operator.
  17. The ftoc.cpp example program illustrates several statements that convert a temperature from Fahrenheit to Celsius. Explain what takes place when the following statement executes.
    c = (f - 32) * 5 / 9;
    1. The statement converts the value 32 from type int to type double because f is type double, and it can only calculate the difference if the values are the same data type. It performs the subtraction operation following the conversion. Next, it converts 5 to a double and does the multiplication operation. Finally, it converts 9 to a double and does the division operation. The statement stores the result in c.
    2. The expression on the right-hand side of the assignment operator is evaluated first. The program evaluates the expression right-to-left, so the division operation occurs first. It converts the result, 5/9, to a double because 5/9 cannot be represented as an int. It multiplies the quotient by the difference of f and 32. The statement stores the result c.
    3. The subtraction operation takes place first, and the result is truncated to an int value because 32 is an int. Then the difference is multiplied by 5 and then divided by 9. The result, which may be inaccurate because of truncation, is stored in c.
  18. The hypot.cpp example program defines three variables: a, b, and c. Without changing the meaning of the program or the problem that it solves (so the program is still going to calculate the hypotenuse), which variable could be eliminated by rewriting the program?
    1. a
    2. b
    3. c
    4. None of the variables can be eliminated.
  19. The statements cout << '\n'; and cout << endl; seem to have the same affect. What does the second statement do that the first does not?
    1. It causes a linefeed to be inserted into the output stream.
    2. It causes a carriage return to be inserted into the output stream.
    3. It makes the next line of text printed display one line down and start on the left.
    4. It flushes the output buffer.
  20. A header file is incorporated into a .cpp file using the _____ directive.

     

  21. A C++ program compiles and runs but produces incorrect output. This is an example of what kind of programming error?
    1. A syntax error
    2. A logical error
    3. A runtime error
  22. A C++ program fails to compile. This is an example of what kind of programming error?
    1. A syntax error
    2. A logical error
    3. A runtime error
  23. A C++ program compiles but does not run to completion. This is an example of what kind of programming error?
    1. A syntax error
    2. A logical error
    3. A runtime error
  24. A large C++ programs fails to compile and the compiler indicates a syntax error on line 456. Which of the following statements is most accurate?
    1. The syntax error must be on line 456 of the program.
    2. The syntax error must be above line 456 of the program.
    3. The syntax error must be below line 456 of the program.
    4. The syntax error is on or above line 456 of the program.
    5. The syntax error is on or below line 456 of the program.
  25. The compiler will always correctly identify the cause of a syntax error, making a detailed knowledge of a computer programming language and its syntax unnecessary.
    1. True
    2. False
  26. C++ documentation provides the following description for the square root library function:
    double sqrt(double x);
    where x ≥ 0. Choose the best C++ implementation of the following formula (in the responses below, l0 is a lower case L-zero): \[ l = l_0 \sqrt{1 - {v^2 \over {c^2}}} \]
    1. l = l0 * sqrt(1 - v*v / c*c);
    2. l = l0 * sqrt(1 - v*v / (c*c));
    3. l = l0(sqrt(1 - (v*v)/(c*c)));
    4. l = l0 * sqrt(1 - pow(v,2) / pow(c,2));
  27. C++ documentation provides the following description for the square root library function:
    double sqrt(double x);
    where x ≥ 0. Choose the best C++ implementation of the following formula: \[ t = {t_2 - t_1 \over {\sqrt{1 - {v^2 \over {c^2}}}}} \]
    1. t = (t2 - t1) / sqrt(1 - pow(v,2) / pow(c,2));
    2. t = (t2 - t1) / sqrt(1 - v*v / (c*c));
    3. t = (t2 - t1) / sqrt(1 - v*v/c/c);
    4. t = t2 - t1 / sqrt(1 - pow(v,2) / pow(c,2));
  28. A program contains two variables named x and y. Each variable is an instance of a class named Foo. Variables of type Foo may be defined, and one Foo variable may be assigned to another, but the Foo class does not support other operations. Write three C++ statements that swap the values stored in variables x and y.

    C++ statement 1: __________________________

    C++ statement 2: __________________________

    C++ statement 3: __________________________

  29. To calculate xy, C++ provides the the power library function:
    double pow(double x, double y);
    Note that exponentiation or raising a number to a power has a higher precedence than multiplication. Assuming that all variables are defined and initialized, choose the best statement that translates the following formula into C++ code: \[ B = R \left[ {1 - (x + 1)(x + 1)^{-(n - x)}} \over {2i - 1} \right] \]
    1. B = (R * ((1 - (x + 1) * (pow((1 + i), ((-1) * (n-x)))) / ((2*i) - 1)));
    2. B = (R * ((1 - (x + 1) * (pow((1 + i), ((-1) * (n-x))))) / ((2*i) - 1)));
    3. B = (R) * ((1 - (x + 1) * (pow((1 + i), ((-1) * (n-x))))) / ((2*i) - 1)));
    4. B = R * ((1 - (x + 1) * pow(1 + i, -(n-x))) / (2*i - 1));
    5. B = R * ((1 - (x + 1) * pow(1 + i, -(n-x))) / 2*i - 1);
    6. B = (R * [(1 - (x + 1) * (pow((1 + i), ((-1) * (n-x))))) / ((2*i) - 1)]);
    7. B = R((1 - (x + 1) * pow(1 + i, -(n-x))) / (2*i - 1));
    8. B = R[(1 - (x + 1) * pow(1 + i, -(n-x))) / (2*i - 1)];
  30. To calculate xy, C++ provides the the power library function:
    double pow(double x, double y);
    Using the variables
    double i1;
    double i2;
    double n1;
    double n2;
    write the C++ statement (one line) to represent the following formula: \[ i_2 = \left[ \left(1 + {i_1 \over n_1} \right)^{n_1 \over n_2} - 1 \right] n_2 \]
  31.  

  32. Choose the best elaboration to fill the blank comment in the following program:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    double f;
    cout << "Enter a temperature in Fahrenheit: ";
    cin >> f;
    
    double c = 5 * (f - 32) / 9;		// ____________________
    
    cout << "The temperature in Celsius = " << c << endl;
    
    return 0;
    }
    1. The statement does not work because dividing by an integer causes a truncation error.
    2. The statement works without producing a truncation error.
    3. f-32 is evaluated first producing a double-value; multiplying 5 by a double produces a double, and dividing a double by 9 produces a double, so there is no truncation error.
    4. The assignment operator is right-associative, meaning that the statement evaluates the arithmetic expression on the right-hand side first. All arithmetic operators are left-associative, so the arithmetic expression is evaluated from left to right. The parentheses cause the statement to evaluate f-32 first. f is a double, so the 32 must be converted from an integer to a double before calculating the difference. The 5 must be converted from an integer to a double before calculating the product. Then the 9 is converted from an integer to a double before the division operation, which avoids a truncation error. Finally, the calculated value is assigned or stored in variable c.
    You should carefully study the solution for this question in the answers.