6.12. Multiple Choice Exercises¶
0
-
xis initialized to 0, but it’s value is reassigned in the while loop. Can you figure out what the final value assigned toxis? 1
-
When
iis 1,xis assigned the value ofi, soxis 1. However, the while loop continuously increments i, so the final value ofxis not 1. 9
-
xis assigned the value of 9 during the last iteration of the while loop, and thus 9 is the output of the program. 10
-
iis incremented to a value of 10, but sincei < 10is false, the contents of the while loop is not executed, soxis never assigned the value of 10.
Q-1: What is the output of the code below?
int main() {
int x = 0;
int i = 1;
while (i < 10) {
x = i;
i++;
}
cout << x;
}
0
-
iis initialized with a value of 1 and is incremented, so it will never have a value of 0. 1
-
iis initialized with a value of 1 but it is incremented during the while loop. 9
-
This is the final value of
xwhen the code is finished running. 10
-
In order for the while loop to terminate, the condition
i < 10must be false, and this is achieved wheniis incremented to 10.
Q-2: What is the final value of i when the code finished running?
int main() {
int x = 0;
int i = 1;
while (i < 10) {
x = i;
i++;
}
cout << x;
}
1
-
Take a closer look at the while loop and conditional.
3
-
Take a closer look at the while loop and conditional.
5
-
Take a closer look at the while loop and conditional.
The loop will run infinitely.
-
The value of
iwill always be greater than 2, resulting in an infinite loop.
Q-3: How many times does the following while loop run?
int main() {
int i = 6;
while (i > 2) {
i = i + 4;
if (i > 8) {
i = i - 5;
}
}
People really like Joe’s Pizza.
-
Take a closer look at the while loop condition.
People really really really like Joe’s Pizza.
-
The code will print out three “really”s, two from evaluating the while loop condition and one from evaluating the if condition before printing out “like Joe’s Pizza”
People really really really really really like Joe’s Pizza.
-
Take a closer look at the while loop condition.
The loop will run infinetly.
-
Take a closer look at the while loop condition and reassignment of
i
Q-4: What is the output of the code below?
int main() {
int j = 6;
int i = j + 4;
cout << "People ";
i = i % 2;
i = i - 1;
while (i < 3) {
cout << "really ";
if (i > 0) {
cout << "really ";
}
i += 2;
}
cout << "like Joe's Pizza." << endl;
}
na na na na na na na na Batman!
-
The code prints out eight “na”s before printing out “Batman!”
na na na na na na na Batman!
-
Look over the code carefully. There are output statements before the while loop.
Da na na na na na na na na Batman!
-
Will “Da” ever be printed?
It will result in an infinite loop.
-
Since we repeatedly decrement
ninside the while loop, it will eventually be equal to 3 and the while loop will terminate.
Q-5: What is the output of the code below?
int main() {
int n = 10;
// cout << "Da ";
cout << "na ";
while (n != 3) {
cout << "na ";
n--;
}
cout << "Batman!";
}
Batman!
-
Take a closer look at the while loop.
Da Batman!
-
Take a closer look at the while loop.
Da na na na na na na na na Batman!
-
Take a closer look at the while loop.
It will result in an infinite loop.
-
Since we never change the value of
n, 10 will never equal 3 so the code will run forever.
Q-6: What is the output of the code below?
int main() {
int n = 10;
cout << "Da ";
cout << "na ";
while (n != 3) {
cout << "na ";
}
cout << "Batman!";
}
The first six perfect fifths.
-
Take a closer look at the while loop and what
xwas initialized to. The first six perfect squares.
-
Take a closer look at the while loop and what
xwas initialized to. The first five perfect squares.
-
Dividing
xto the power of 5 byxto the power of 3 effectively results in perfect squares. The first five perfect cubes.
-
Take a closer look at the mathematical expression inside the while loop.
Q-7: What is the output of the code below?
int main() {
int x = 1;
while (x < 6) {
cout << x << "\t" << pow (x, 5) / pow (x, 3) << endl;
x++;
}
}
We’re using the same variable, but just reassigning the value from 0 to 1.
-
We are actually using two different variables that happen to have the same name.
Although the name of both variables is
x, they represent different locations in memory, and thus are different variables.-
One
xis a local variable ofsuperSecretFunctionwhile the other is a local variable ofmain. We can assign them different values but not the same value. Thus, if both were initialized to 0, then we’d get an error.
-
Since they are not in the same storage location, they can store any value, including the same value.
We’re not allowed to do this. The code will result in an error.
-
The code does not produce an error.
Q-8: Why are we allowed to use the variable x in both main and in the function definition of superSecretFunction?
int superSecretFunction (int n) {
int x = 0;
return (2 + (n * n) - 5 * n / 7) * x;
}
int main() {
int x = 1;
cout << "After using the super secret function, we get " << superSecretFunction (x);
}
51510
-
nis repeatedly incremented by 3 until it is divisible by 5, and this happens whennis 5, 15, and 10 for the inputs of 2, 3, and 4 respectively. 234
-
Although the function returns
n,nmight not be its original value. 5 15 10
-
Take a closer look at the output statements.
567
-
Take a closer look at the
whileloop in the function.
Q-9: What is the output of the code below?
int loopFive (int n) {
while (n % 5 != 0) {
n = n + 3;
}
return n;
}
int main() {
cout << loopFive (2);
cout << loopFive (3);
cout << loopFive (4);
}
repeatBotcan only take one word as an argument.-
A
stringis any number of characters or words surrounded by double quotes, not just one word. nis declared to be 0 and 0 is always less than 5.-
The code doesn’t loop infinitely because of the value
nwas declared to be. Every time the
whileloop runs,nis reset to 0, so it will always be less than 5.-
The initialization of
noccurs outside thewhileloop, so the value ofndoes not get reset to 0. nis declared to be 0 and we continuously decrementnso it will always be less than 5.-
Since
nstarts at 0 and gets smaller, the conditional for thewhileloop will always be true, and thus the code runs forever.
Q-10: The super evil villian RePete wants to annoy the city by hacking into the city’s helper robots and making them repeat everything they say 5 times. However, there’s an error in his code and now the robots won’t stop repeating! Can you find the error?
void repeatBot (string input) {
int n = 0;
while (n < 5) {
cout << input << " ";
n--;
}
}
int main() {
repeatBot ("Hello, how may I help you?");
}
n > 5is not a valid conditional, so thewhileloop doesn’t execute.-
n > 5is a boolean statement and thus is a valid conditional. The value of
nnever gets modified in thewhileloop.-
nis incremented in thewhileloop after thecoutstatement. In the
coutstatement, only spaces are printed.-
inputis also printed. The conditional for the
whileloop is not met.-
Since
nis declared to be 0, 0 is not greater than 5, so thewhileloop does not execute.
Q-11: After making some changes to his code, RePete tries again. This time, however, the robots don’t repeat anything! Can you find the new error?
void repeatBot (string input) {
int n = 0;
while (n > 5) {
cout << input << " ";
n++;
}
}
int main() {
repeatBot ("Hello, how may I help you?");
}