Exercises 4.11 Multiple Choice Exercises
Answer the following Multiple Choice questions to assess what you have learned in this chapter.
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);
- 5
- This is what
a + b
would be before the first conditonal. - 7
- 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 theif
block.The fortune you seek is in another cookie.
'm'
is NOT less than'm'
, so you don’t even enter theif
block.He who laughs at himself never runs out of things to laugh at.
if (b)
really meansif (b == true)
.You will be hungry again in one hour.
'm' < 'r'
is true andb == false
, so this is the fortune that will print.Fortune not found? Abort, retry, ignore.
'm'
is less than'r'
so you would enter theelse if
block, not theelse
.
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'
and22 % 2 == 0
, so this is the fortune that will print.The fortune you seek is in another cookie.
22 % 2 == 0
, so you enter theif
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 theif
block, not theelse if
.You will be hungry again in one hour.
'b'
is less than'm'
, so you would enter theif
block, not theelse if
.Fortune not found? Abort, retry, ignore.
'b'
is less than'm'
, so you would enter theif
block, not theelse
.
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);
- 5
- 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 theelse if
block executes.- 10
- The condition for
else if
is met, so the function never enters theelse
. - -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);
- 6
5 % 4 != 0
in theif
block, so the function doesn’t print 6.- 10
m + n !> 10
in theif
block, so the function doesn’t print 10.- 2
- The condition for
if
is met, so the function never enters theelse if
. - 4
- The condition for
if
is met, so the function never enters theelse
. - -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);
- 0
- When we call the function
4 != 8
, so “Moo!” is printed at least once. - 1
- The function calls itself inside of the
if
loop, so “Moo!” is printed more than once. - 2
m
is incremented by two each with each function call, so after twom == n
and the recursion stops.- 3
- 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);
- 0
- When we call the function
5 != 10
, so “Moo!” is printed at least once. - 1
- The function calls itself inside of the
if
loop, so “Moo!” is printed more than once. - 2
- After two function calls,
m == 9
andn == 10
. The function is not done printing. - 3
- After three function calls,
m == 11
andn == 10
. The function is not done printing - infinite recursion
- The function stops printing “Moo!” when
m == n
, but sincem
is odd andn
is even, they will never be equal as long as we increment by two.
You have attempted 1 of 2 activities on this page.