Quiz 2 Solutions

30 September 2013

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.

The program on this page uses nested conditional statements. Given the values of x and y as inputs, what does the program output?

  • x = 6; y = 3; Output: BCF
  • x = 7; y = 7; Output: DF
  • x = 5; y = 5; Output: ACF
  • x = 7; y = 10; Output: DEF
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "Enter two integers: ";
    cin >> x >> y;
    if(x < 7)
    {
        if(y > 3)
        {
            cout << "A";
        }
        else
        {
            cout << "B";
        }
        cout << "C";
    }
    else
    {
        cout << "D";
        if(y > 9)
        {
            cout << "E";
        }
    }
    cout << "F";
}

This is a similar problem, but the program on this page uses a switch statement. Given the value of k as input, what does the program output?

  • k = 2; Output: E
  • k = 3; Output: AB
  • k = 4; Output: B
  • k = 5; Output: CDE
  • k = 6; Output: DE
#include <iostream>
using namespace std;
int main()
{
    int k;
    cout << "Enter an integer: ";
    cin >> k;
    switch(k) {
    case 3:
        cout << "A";
    case 4:
        cout << "B";
        break;
    case 5:
        cout << "C";
    default:
        if(k >= 4) {
            cout << "D";
        }
        cout << "E";
    }
    return 0;
}