Name: ______Key_________

 

Quiz 3 (Week 4)

CS-150, Fall ‘02, Dr. C. S. Tritt

 

Each question is worth the same amount.

 

  1. What output would be produced by the following C++ code (assume any necessary header files have been included)?

 

int myValue = 12345;

cout << "The value is " << setw(8) << myValue << " eggs.\n";

 

Answr: The value is    12345 eggs.

 

  1. What output would be produced by the following C++ code (assume any necessary header files have been included)?

 

double aValue = 12.345;

cout << "The value is ";

cout << setw(6) << setprecision(2) << aValue;

cout << "ft/s.\n";

 

Answr: The value is     12ft/s.

                       

Extra space after 12 okay. Answering  … 12.34 … okay. Commenting on missing semicolons would have been + 5 bonus (but nobody did).

 

  1. Write the C++ code that would correctly display the cost of an order in dollars. Assume the cost stored in a variable call orderCost and is between $1.00 and $9999.99.

 

Answr: cout << "$" << setw(7) << fixed << setprecision(2) << orderCost << endl;

 

“$” and endl optional. Missing “fixed” okay – may have already been set. Missing “setw” okay – but does help align decimal points.

 

  1. What value will be stored in factor after the execution of the following C++ code?

 

int count = 1;

double factor = 2.5;

 

count = count + 1; // count will be 2 here.

if (count <= 1)

      factor = factor + 1.0; // not executed.

else

      factor = factor – 1.0; // factor becomes 1.5 here.

count = count + 1; // count becomes 3 here.

factor = factor * 2.0; // factor doubled to 3.0 here.

 
 

 

 

 

 

 

 

 

 

 

 


Answr: 3.0 (wrong values generally -20)