6.7. Multiple Choice ExercisesΒΆ
Answer the following Multiple Choice questions to assess what you have learned in this chapter.
Q2
What is printed when the following code executes?
int x = 8;
if (x % 3 == 2) {
cout << "hey!" << '\n';
} else if (x != 7) {
cout << "hi!" << '\n';
} else if (x % 2 == 0) {
cout << "hello!" << '\n';
} else {
cout << "bye!" << '\n';
}
Q3
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!";
}
Q4
Suppose you have defined the following function:
void practicing_returns (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 = practicing_returns(2, 3);
Q5
Suppose you have defined the following function:
void fortune_cookie (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?
fortune_cookie(14, false, 'm');
Q6
Suppose you have defined the following function:
void fortune_cookie (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?
fortune_cookie(22, true, 'b');
Q7
Suppose you have defined the following function:
void the_thing (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?
the_thing (5, 10, false);
Q8
Suppose you have defined the following function:
void the_thing (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?
the_thing (6, 4, true);
Q9
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);
Q10
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);