Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Exercises 4.11 Multiple Choice Exercises

Answer the following Multiple Choice questions to assess what you have learned in this chapter.

1.

Say you run the following code. What is the value of mod?
int x = 4;
int y = 7;
int mod = y % x;
  • There is a remainder.
  • Incorrect!
  • Incorrect!
  • The remainder of 7 / 4 is 3.
  • We can’t have a remainder of 4, since 4 is the divisor.

2.

What is printed when the following code executes?
int x = 8;

if (x % 3 == 2) {
    cout << "hey!" << endl;
}
else if (x != 7) {
    cout << "hi!" << endl;
}
else if (x % 2 == 0) {
    cout << "hello!" << endl;
}
else {
    cout << "bye!" << endl;
}
  • hey!
    
  • Since the first conditon is met, the rest of the chained conditional does not execute.
  • hi!
    
  • It’s true that 8 != 7, but “hi!” is not printed here.
  • hi!
    
  • It’s true that 8 % 2 == 0, but “hello!” is not printed!
  • hey!
    hi!
    hello!
    
  • All of these conditons are met, but only one expression is printed!
  • bye!
    
  • At least one of the conditons is met, so the else will not execute!

3.

What is printed when the following code executes?
int x = 34;

if (32 < x) {
    cout << "It's Freezing!";
}
if (x < 40) {
    cout << "It's Cold!";
}
if (x > 65) {
    cout << "It's Warm!"'
}
else {
    cout << "It's Hot!";
}
  • It's Freezing!
    
  • Take a closer look at the conditions and the way they are written in the program.
  • It's Cold!
    
  • Take a closer look at the conditions and the way they are written in the program.
  • It's Freezing!
    It's Cold!
    
  • You’ve identified some of the conditons that are met! Take another look at the chain of conditionals at the end!
  • It's Freezing!
    It's Cold!
    It's Hot!
    
  • These statements are quite contradicting, but that’s exactly what the output would be if we ran this code.
  • It's Hot!
    
  • Take a closer look at the conditions and the way they are written in the program.

4.

Suppose you have defined the following function:
void practicingReturns(int a, int b) {
    if (a < b) {
        a += 2;
    }
    if (a > b) {
        return;
    }
    cout << a + b;
}
What is printed when we run the following code?
int x = practicingReturns(2, 3);
  • This is what a + b would be before the first conditonal.
  • This is the value of a + b after the first conditional, but it doesn’t print.
  • 23
  • This is not the value of a + b.
  • Nothing.
  • The function exits with a return before anything is printed.

5.

Suppose you have defined the following function:
void fortuneCookie(int a, bool b, char c) {
    if (c < 'm') {
        if (a % 2 == 0) {
            cout << "An alien of some sort will be appearing to you shortly.";
        }
        else {
            cout << "The fortune you seek is in another cookie.";
        }
    }
    else if (c < 'r') {
        if (b) {
            cout << "He who laughs at himself never runs out of things to laugh at.";
        }
        else {
            cout << "You will be hungry again in one hour.";
        }
    }
    else {
        cout << "Fortune not found? Abort, retry, ignore.";
    }
}
What will be your fortune if you run the following code?
fortuneCookie(14, false, 'm'');
  • An alien of some sort will be appearing to you shortly.
  • 'm' is NOT less than 'm', so you don’t even enter the if block.
  • The fortune you seek is in another cookie.
  • 'm' is NOT less than 'm', so you don’t even enter the if block.
  • He who laughs at himself never runs out of things to laugh at.
  • if (b) really means if (b == true).
  • You will be hungry again in one hour.
  • 'm' < 'r' is true and b == false, so this is the fortune that will print.
  • Fortune not found? Abort, retry, ignore.
  • 'm' is less than 'r' so you would enter the else if block, not the else.

6.

Suppose you have defined the following function:
void fortuneCookie(int a, bool b, char c) {
    if (c < 'm') {
        if (a % 2 == 0) {
            cout << "An alien of some sort will be appearing to you shortly.";
        }
        else {
            cout << "The fortune you seek is in another cookie.";
        }
    }
    else if (c < 'r') {
        if (b) {
            cout << "He who laughs at himself never runs out of things to laugh at.";
        }
        else {
            cout << "You will be hungry again in one hour.";
        }
    }
    else {
        cout << "Fortune not found? Abort, retry, ignore.";
    }
}
What will be your fortune if you run the following code?
fortuneCookie(22, true, 'b');
  • An alien of some sort will be appearing to you shortly.
  • 'b' < 'm' and 22 % 2 == 0, so this is the fortune that will print.
  • The fortune you seek is in another cookie.
  • 22 % 2 == 0, so you enter the if block, not the else.
  • He who laughs at himself never runs out of things to laugh at.
  • 'b' is less than 'm', so you would enter the if block, not the else if.
  • You will be hungry again in one hour.
  • 'b' is less than 'm', so you would enter the if block, not the else if.
  • Fortune not found? Abort, retry, ignore.
  • 'b' is less than 'm', so you would enter the if block, not the else.

7.

Suppose you have defined the following function:
void theThing(int m, int n, bool b) {
    if (b) {
        if (m % 4 == 0) {
            cout << m;
            return;
        }
        if ((m + n) > 10) {
            cout << m + n;
            return;
        }
    }
    else if ((m > n) == b) {
        cout << m - n;
        return;
    }
    else {
        if (n % 3 == 0) {
            cout << n;
            return;
        }
    }
    cout << -1;
}
What is printed when we run the following code?
theThing(5, 10, false);
  • The outer if condition is not met, the block does not execute.
  • 15
  • The outer if condition is not met, the block does not execute.
  • -5
  • m > n evaluates to false, so the else if block executes.
  • 10
  • The condition for else if is met, so the function never enters the else.
  • -1
  • The function has returned.

8.

Suppose you have defined the following function:
void theThing(int m, int n, bool b) {
    if (b) {
        if (m % 4 == 0) {
            cout << m;
            return;
        }
        if ((m + n) > 10) {
            cout << m + n;
            return;
        }
    }
    else if ((m > n) == b) {
        cout << m - n;
        return;
    }
    else {
        if (n % 3 == 0) {
            cout << n;
            return;
        }
    }
    cout << -1;
}
What is printed when we run the following code?
theThing(6, 4, true);
  • 5 % 4 != 0 in the if block, so the function doesn’t print 6.
  • 10
  • m + n !> 10 in the if block, so the function doesn’t print 10.
  • The condition for if is met, so the function never enters the else if.
  • The condition for if is met, so the function never enters the else.
  • -1
  • None of the conditions were met, so we reach the default cout -1.

9.

Suppose you have defined the following function:
void moo(int m, int n) {
    if (m != n) {
        m += 2;
        cout << "Moo!";
        moo(m, n);
    }
    else {
        cout << "Got Milk?";
    }
}
How many times does “Moo!” print when we run the following?
moo(4, 8);
  • When we call the function 4 != 8, so “Moo!” is printed at least once.
  • The function calls itself inside of the if loop, so “Moo!” is printed more than once.
  • m is incremented by two each with each function call, so after two m == n and the recursion stops.
  • Take a look at how m is incremented with each function call.
  • infinite recursion
  • The function stops printing “Moo!” when m == n.

10.

Suppose you have defined the following function:
void moo(int m, int n) {
    if (m != n) {
        m += 2;
        cout << "Moo!";
        moo(m, n);
    }
    else {
        cout << "Got Milk?";
    }
}
How many times does “Moo!” print when we run the following?
moo(5, 10);
  • When we call the function 5 != 10, so “Moo!” is printed at least once.
  • The function calls itself inside of the if loop, so “Moo!” is printed more than once.
  • After two function calls, m == 9 and n == 10. The function is not done printing.
  • After three function calls, m == 11 and n == 10. The function is not done printing
  • infinite recursion
  • The function stops printing “Moo!” when m == n, but since m is odd and n is even, they will never be equal as long as we increment by two.
You have attempted 1 of 2 activities on this page.