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
elsewill 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 + bwould be before the first conditonal. - 7
- This is the value of
a + bafter 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 theifblock.The fortune you seek is in another cookie.'m'is NOT less than'm', so you donβt even enter theifblock.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 ifblock, 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 theifblock, 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 theifblock, not theelse if.You will be hungry again in one hour.'b'is less than'm', so you would enter theifblock, not theelse if.Fortune not found? Abort, retry, ignore.'b'is less than'm', so you would enter theifblock, 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
ifcondition is not met, the block does not execute. - 15
- The outer
ifcondition is not met, the block does not execute. - -5
m > nevaluates to false, so theelse ifblock executes.- 10
- The condition for
else ifis 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 != 0in theifblock, so the function doesnβt print 6.- 10
m + n !> 10in theifblock, so the function doesnβt print 10.- 2
- The condition for
ifis met, so the function never enters theelse if. - 4
- The condition for
ifis 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
ifloop, so βMoo!β is printed more than once. - 2
mis incremented by two each with each function call, so after twom == nand the recursion stops.- 3
- Take a look at how
mis 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
ifloop, so βMoo!β is printed more than once. - 2
- After two function calls,
m == 9andn == 10. The function is not done printing. - 3
- After three function calls,
m == 11andn == 10. The function is not done printing - infinite recursion
- The function stops printing βMoo!β when
m == n, but sincemis odd andnis even, they will never be equal as long as we increment by two.
You have attempted of activities on this page.
