Quiz 1 Solutions

17 September 2014

You have up to 20 minutes. Please write your answers on this page, and your name at the top. You may not use the computer, notes, or books.

  1. Which of the following statements prints exactly this text, as shown:

    We the
    PEOPLE

    Select all that apply.

    1.   cout << "We the PEOPLE" << endl;
    2.   cout << "We the" << "endl" << "PEOPLE" << "endl";
    3.   cout << "We" << " the" << endl << "PEOPLE" << endl;
    4.   cout << "We the" << endl << "PEOPLE" << endl;
    5.   cout << "WE the" << endl << "people";

    Both c and d produce the required output.

  2. Given a variable maxDays = 14, which statement prints 14?

    1.   cout >> maxDays;
    2.   cout << maxDays;
    3.   cout << "maxDays";
    4.   print maxDays;

    Only b.

  3. Which of these are valid C++ comments, that will be ignored by the compiler? Select all that apply.

    1.   /* Hello world */
    2.   "Hello world"
    3.   # Hello world
    4.   /* Hello world
    5.   -- Hello world
    6.   (( Hello world ))
    7.   // Hello world

    The valid comment forms are a, g.

  4. Circle and describe any syntax errors in this program:

#include <iostream>
using namespace;
INT main( )
{
    int score = 100;
    cout << 'Hello world.' << endl
    cout < score < '%';
    return 0;
}

  • Missing namespace std
  • Should lowercase INT
  • Need double quotes around “Hello world”
  • Need semi-colon after endl
  • Need double less-than << on second cout

  1. Below is a simple C++ program containing a calculation and some output. What would it output when you run it?
#include <iostream>
using namespace std;
int main()
{
    int lisa = 8;
    int bart = lisa + 2;
    int maggie = bart - 7;
    cout << maggie << ", " << bart << lisa << endl;
    return 0;
}

3, 108