Wednesday, October 26, 2011

Exam #1 short answers

I am still in the process of grading the exams from Monday night. In the meanwhile, you might like to review the following solutions to the short answer section of the exam. I am planning to also post some source code solutions to the programming problems presented on the test.

  1. Apply the pre-increment operator to ctr.

       ++ctr


  2. Fill in code to output 3.4 cubed.

       cout << pow(3.4, 3.0) << endl; or
       cout << 3.4 * 3.4 * 3.4 << endl;


  3. Complete the following table regarding the three loop types used in c++.
    loop typeminimum iterationspre-test or post-test
    do-while1post
    for0pre
    while0pre


  4. Name the operation and fill in the value.
    23 / 4integer5
    23 / 4.0floating point5.75
    23 % 4modulus3


  5. Complete the truth tables for
    AND
    p1p2p1 && p2
    TTT
    TFF
    FTF
    FFF

    OR
    p1p2p1 || p2
    TTT
    TFT
    FTT
    FFF

    XOR
    p1p2p1 ^ p2
    TTF
    TFT
    FTT
    FFF


  6. Convert the following while loop to a for loop. Show the output.
       const int STOP = 3;
       int ctr = 0;
       while ( ctr < STOP )
          cout << ctr++ << endl;


    Output:
       0
       1
       2


    Code:
       const int STOP = 3;
       for ( int ctr = 0; ctr < STOP; ctr++ )
          cout << ctr << endl;


  7. Rewrite the following if/else if code using a switch statement and display the grade level associated with the grade entered by the user. Assume grade is an integer. Declare the constants used in the code.
       string grade_level;
       if ( grade == FRESHMAN )
          grade_level = "Freshman")
       else if ( grade == SOPHOMORE )
          grade_level = "Sophomore;
       else if ( grade == JUNIOR )
          grade_level = "Junior";
       else if ( grade == SENIOR)
          grade_level = "Senior";

       cout << grade_level << endl;


    Code:
       string grade_level;
       const int FRESHMAN = 9;
       const int SOPHOMORE = 10;
       const int JUNIOR = 11;
       const int SENIOR = 12;

       switch ( grade )
       {
          case ( FRESHMAN ):
             grade_level = "Freshman";
             break;
          case ( SOPHOMORE ):
             grade_level = "Sophomore";
             break;
          case ( JUNIOR ):
             grade_level = "Junior";
             break;
          case ( SENIOR ):
             grade_level = "Senior";
             break;
       }
       cout << grade_level << endl;