Quiz 2 Solutions

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. 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

  1. The following program uses a variety of arithmetic operators and mathematical functions. What does it output? You can write each line of output to the right of the corresponding endl.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    cout << 9 / 2            << endl; // _______
    cout << 7 / 2.0          << endl; // _______
    cout << 17 % 2           << endl; // _______
    cout << 31 % 7           << endl; // _______
    cout << pow(3,3)         << endl; // _______
    cout << sqrt(25)         << endl; // _______
    cout << floor(9.8)       << endl; // _______
    cout << sqrt(pow(2,4))+1 << endl; // _______
    return 0;
}

4
3.5
1
3
27
5
9
5