csci 20 :: book learning challenge 5 (due tues, feb 23)
Submitting your work:

Complete this exercise on your own.

(1) Fill in all of the "Answer" boxes, (2) press "Prepare for printing", then (3) print the answer grid.

Staple all pages together and submit to the instructor on the date listed above.

Questions:

Read Operators, Operators, Statements and flow control, Conditionals and answer the following:

Your name:
  1. Briefly define Boolean expression.
    Answer:
  2. Write the C++ operators that are used to express the following:
    • equal to/equivalent
    • not equal to
    • greater than
    • less than
    • greater than or equal to
    • less than or equal to

    Answer:
  3. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 1;
    int numTwo = 0;
    if (numOne > numTwo)
    {
         cout << "TRUE" << endl;
    }
    else
    {
         cout << "FALSE" << endl;
    }
    Answer:
  4. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 10;
    int numTwo = 10;
    if (numOne < numTwo)
    {
         cout << "TRUE" << endl;
    }
    else
    {
         cout << "FALSE" << endl;
    }
    Answer:
  5. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 25;
    int numTwo = 25;
    if (numOne >= numTwo)
    {
         cout << "TRUE" << endl;
    }
    Answer:
  6. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 10;
    int numTwo = 100;
    if (numOne <= numTwo)
    {
         cout << "TRUE" << endl;
    }
    else
    {
         cout << "FALSE" << endl;
    }
    Answer:
  7. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = -1;
    int numTwo = 1;
    if (numOne != numTwo)
    {
         cout << "TRUE" << endl;
    }
    else
    {
         cout << "FALSE" << endl;
    }
    Answer:
  8. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 10;
    int numTwo = 20;
    if (numOne == numTwo)
    {
         cout << "TRUE 1" << endl;
    }
    else if (numOne > numTwo)
    {
         cout << "TRUE 2" << endl;
    }
    Answer:
  9. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 10;
    int numTwo = 20;
    int numThree = 30;
    if (numOne < numTwo && numOne <= numThree)
    {
         cout << "TRUE" << endl;
    }
    else
    {
         cout << "FALSE" << endl;
    }
    Answer:
  10. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = -1;
    int numTwo = 0;
    if (numOne >= 0 || numTwo >= 0)
    {
         cout << "TRUE" << endl;
    }
    Answer:
  11. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    bool isHappy = true;
    bool knowIt = true;
    if (isHappy == true && knowIt == true)
    {
         cout << "CLAP YOUR HANDS" << endl;
    }
    else if (isHappy == true || knowIt != true)
    {
         cout << "SMILE A SMILE" << endl;
    }
    else
    {
         cout << "CHEER UP" << endl;
    }
    Answer:
  12. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    char answer = 'y';
    switch (answer)
    {
         case 'n':
              cout << "NO" << endl;
              break;
         case 'y':
              cout << "YES" << endl;
              break;
         default:
              cout << "UNKNOWN" << endl;
              break;
    }

    Answer:
  13. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer. (HINT: tricky!)
    int choice = 3;
    switch (choice)
    {
         case 1:
              cout << "ONE" << endl;
              break;
         case 2:
              cout << "TWO" << endl;
              break;
         case 3:
              cout << "THREE" << endl;
         default:
              cout << "UNKNOWN" << endl;
              break;
    }

    Answer:
  14. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int numOne = 100;
    if (numOne = 1)
    {
         cout << "ONE" << endl;
    }
    else
    {
         cout << "NOT ONE" << endl;
    }
    Answer:
  15. Use an IF--ELSE statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm.

    • if hasTicket is true OR moneyInPocket is greater than or equal to costOfTicket, print out "WELCOME"
    • otherwise print out "SEE YOU LATER"

    Answer:
  16. Use an IF-ELSE IF-ELSE statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm.

    • if temperature is greater than 95, print out "HOT!"
    • else if temperature is greater than 80, print out "WARM"
    • else if temperature is greater than 60, print out "PLEASANT"
    • else if temperature is greater than 32, print out "COLD"
    • otherwise print out "FREEZING!"

    Answer:
  17. Use an IF-ELSE IF-ELSE statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm.

    • if letter is an 'A' print out "A IS FOR APPLE"
    • else if letter is a 'B' print out "B IS FOR BANANA"
    • else if letter is a 'C' print out "C IS FOR CHERRIES"
    • otherwise print out "NO FRUIT FOR THAT LETTER"

    Answer:
  18. Use a SWITCH statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm.

    • if letter is an 'A' print out "A IS FOR APPLE"
    • else if letter is a 'B' print out "B IS FOR BANANA"
    • else if letter is a 'C' print out "C IS FOR CHERRIES"
    • otherwise print out "NO FRUIT FOR THAT LETTER"

    Answer:
  19. Use a SWITCH statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm. (HINT: stack the cases!)

    • if selection is 'A' or 'a', print out "SELECTION A"
    • if selection is 'B' or 'b', print out "SELECTION B"
    • if selection is 'C' or 'c', print out "SELECTION C"

    Answer:
  20. Use an IF-ELSE IF-ELSE statement to translate the following pseudocode into correct C++ code. You do not need to write a complete program or declare any variables -- assume the variables being referenced have already been declared, and only write the C++ code needed to express the algorithm.

    • if itemOrdered is 1 OR itemOrdered is 11, increment totalPrice by 10
    • else if itemOrdered is 2 OR itemOrdered is 12, increment totalPrice by 20
    • else if itemOrdered is greater than 12, increment totalPrice by 1

    Answer:

Total points for this book learning challenge:

150 points