Quiz 6 Solutions

Mon Dec 7

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

The following program uses an array. What does it output?

int main()
{
    const int SZ = 5;
    int g[SZ];
    int x = 5;
    for(int i = 0; i < SZ; i++)
    {
        x += i;
        g[i] = x+1;
    }
    for(int i = SZ-1; i > 0; i--)
    {
        cout << g[i] << "\n";
    }
    return 0;
}

Output:

16
12
9
7

The following program uses nested function calls with return values. What is its output?

int trot(int x)
{
    if(x > 4) {
        return x - 3;
    }
    else {
        return x - 1;
    }
}

int thaw(int y)
{
    return y+5;
}

int main()
{
    cout << trot(3) << "\n";
    cout << thaw(4) << "\n";
    cout << trot(thaw(5)) << "\n";
    cout << thaw(trot(thaw(6))) << "\n";
    return 0;
}

Output:

2
9
7
13