EXAM 1 – SPRING 2008

 

 

PLACE ALL ANSWERS, CLEARLY NUMBERED, IN YOUR BLUE BOOK.  NO CALCULATORS MAY BE USED.

 

 

A)    Show what is printed by the following program: (14 points)

 

 

 

#include<string>

using namespace std;

int main()

{

     double countA;

     double countB;

     string item = "Widgets";

 

 

     countA=20.3;

    

     countB=6.1;

 

     cout<<"Total is = "<<countA + countB<<endl;

 

     if (countA > 10)

     {

          cout<<"Large Order!\n";

          countB++;

     }

     else

     {

          cout<<"Small Order!\n";

          countB--;

     }

 

     cout << item << ": " << countB << endl;

     cout<<"\nEND OF " << item << "PROGRAM";

     return 0;

}

 

 

 

 

 

 

B)    Multiple Choice (4 points each)

Identify the letter of the choice that best completes the statement or answers the question.

 

 

 

____          1.   The value of the expression 33%10 is _____.

a.

0.3

c.

3.0

b.

3

d.

3.3

 

 

 

____          2.   The value of the expression 26 + 14 / 3 + 1 is _____.

a.

10

c.

29

b.

14

d.

31

 

 

 

____          3.   Suppose that x = 5 and y = 6. Choose the output of the following C++ statement: cout<<"Sum of "<<x<<" and "<<y<<" = "<<x+y<<endl;.

 

a.

Sum of 5 and 6 = 11

c.

Sum of x and y = x+y

b.

Sum of x and y = 11

d.

Sum of 5 and 6 = x+y

 

 

 

 

____          4.   Suppose that alpha and beta are int variables and alpha = 5 and beta = 10. After the statement alpha *= beta; executes _____.

a.

alpha = 5

c.

alpha = 50

b.

alpha = 10

d.

alpha = 50.0

 

 

 

 

____          5.   Suppose that x is an int variable, ch is a char variable, and the input is: 276. Choose the values after the following statement executes: cin>>ch>>x;.

a.

ch = ‘2’, x = 276

c.

ch = ' ', x = 276

b.

ch = ‘2’, x = 76

d.

ch = ‘b’, x = 76

 

 

 

____          6.   Which of the following expressions correctly determines that x is greater than 10 and less than 20?

a.

10 < x < 20

c.

10 < x && x < 20

b.

(10 < x < 20)

d.

10 < x || x < 20

 

 

 

____          7.   What is printed by the following piece of code?

 

                        int x = 15;

 

               if (x == 15)

          {

              x = 0;

              cout << “bye”;

          }

          if (x == 0)

              cout << “hi”;

 

a.

bye

c.

byehi

b.

hi

d.

hibye

 

 

 

____          8.   What happens if an input file is not on disk, and the C++ program tries to open it?

 

a.

Causes a file input stream Error

c.

It creates  the file in the current project directory

b.

It creates the file in the C:

directory

d.

Prompts the user to enter values instead

 

 

 

____          9.   Which one of the following four statements is correct given the three lines of code:

 

            ofstream questionFile;

         questionFile.open(“ondisk.txt”);

         int  value = 0;

 

a.

ofstream << value;

c.

cin >> questionFile;

b.

questionFile << value;

d.

None of the above

 

 


 

C)    Short Answers (6 points each)

 

 

  1. Write the C++ code that declared an int variable and sets it equal to zero.

 

 

 

  1. Write the C++ code that prints “hi” three times, each on its own line.

 

 

 

  1. Assume the char variable initial holds a person’s middle initial.  Write the C++ statement that will check the initial and print vowel, if the initial is A, E, I, O, or U.

 

 

 

  1. Write C++ code that checks two integer variables x and y, and print out true if the sum of the two numbers greater than the product of the two numbers.

 

 

 

 

  1. What is the truth value of the following (true or false)?

 

 

4 + 4 != 8 && 17  % 3 > 2 || 6 + 1 >= 7

 

 

 

 


 

 

D)    Write the following complete program:  (20 points)

 

Your program updates bank records. 

 

Prompt the user to enter the following:

 

 

  1. average monthly balance for a customer
  2. type of account it is: checking or savings. (You should use a character variable for this)

 

If the balance is less than the minimum of $100, there is a service charge of $25 for checking accounts and $10 for savings account; otherwise there is no charge.

 

Your program prints out a message stating whether or not there is a charge, and what the charge is.

 

Checking customers receive 3% interest; savings customers receive 5% interest.

 

Your program prints a message with the amount of interest the customer received this month.