Topic 4: Loops
Question 1
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int cnt = 10; int num = 1;
do {
cnt = cnt + 1;
num = num – 1;
} while (num != 0);
cout << cnt;
return 0;
}Select one:a. 10b. 11
c. 12d. Infinite loop causes infinite output.
Feedback
The correct answer is: 11
Question 2
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 30; int y = 0;
if( x < 20 ) {
y = 30;
}
cout << y;
return 0;
}Select one:a. 20b. 10c. 0
d. 30
Feedback
The correct answer is: 0
Question 3
Correct2.50 points out of 2.50Flag question
Question text
What is the value assigned to variable z for the following code, assuming the initial values of a = 2, b = 2, and c = 50?
mult = 0;
while (a < 20) {
mult = b * a;
if (mult > c) break;
a = a + 1;
}
z = a;Select one:a. 10b. 20
c. 50d. 100
Feedback
The correct answer is: 20
Question 4
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
string cityName = “La Jolla”;
cityName.at(2) = ‘_’;
cout << cityName;
return 0;
}Select one:a. L_ Jollab. La_Jolla
c. La _ollad. ________
Feedback
The correct answer is: La_Jolla
Question 5
Correct2.50 points out of 2.50Flag question
Question text
What is the value of encodedVal if userChar is the char ‘D’?
switch (userChar) {
case ‘a’:
case ‘A’:
encodedVal = 1;
break;
case ‘b’:
case ‘B’:
encodedVal = 2;
break;
case ‘c’:
case ‘C’:
encodedVal = 3;
break;
default:
encodedVal = 27;
break;
}Select one:a. 1b. 2c. 3d. 27
Feedback
The correct answer is: 27
Question 6
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 3; int y = 5;
while (y > x) {
cout << y << ” “;
y = y + 1;
}
return 0;
}Select one:a. 3 4 5b. 3 4c. Loop is never entered. No output.d. Infinite loop causes infinite output.
Feedback
The correct answer is: Infinite loop causes infinite output.
Question 7
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int i = 0;
int j = 2;
for (i = 0; i <= 6; i = i + 2) {
cout << (i * j) << ” “;
}
return 0;
}Select one:a. 0 4 8 12 16b. 0 4 8 12
c. 0 2 4 6d. 0 2 4 6 8
Feedback
The correct answer is: 0 4 8 12
Question 8
Correct2.50 points out of 2.50Flag question
Question text
What is stored in isSuccessful after executing the following statements?
bool isDiligent = true;
bool isKind = true;
bool isSuccessful = false;
if (isDiligent && isKind) {
isSuccessful = true;
}Select one:a. falseb. True
c. 2d. Compilation error in code.
Feedback
The correct answer is: True
Question 9
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 16; int y = 0;
if( x > 20 ) {
y = 20;
}
else {
y = 10;
}
cout << y;
return 0;
}Select one:a. 20b. 16c. 10
d. 0
Feedback
The correct answer is: 10
Question 10
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 5;
while (x > 0) {
cout << x << ” “;
x = x – 1;
}
cout << “Go!”;
return 0;
}Select one:a. 5 4 3 2 1 Go!
b. 4 3 2 1 0 Go!c. 5 4 3 2 1 0 Go!d. 5 5 4 3 2 1 Go!
Feedback
The correct answer is: 5 4 3 2 1 Go!
Question 11
Correct2.50 points out of 2.50Flag question
Question text
How many times will the output statement execute in the following code?
int main() {
int i = 0; int j = 0;
for(i = 1; i <= 3; ++i) {
for(j = 1; j <= 5; ++j) {
cout << i << j << ” “;
}
}
return 0;
}Select one:a. 3b. 8c. 15
d. 24
Feedback
The correct answer is: 15
Question 12
Correct2.50 points out of 2.50Flag question
Question text
Which expression determines whether x is between 10 and 20, and y is between 0 and 99?Select one:a. ((x < y) || (x > y) && !(y > 99) && (x > 10)b. ((x == 10) || (x == 20)) && ((y == 0) || (y == 99))c. (x > 10) || (x < 20) || (y > 0) || (y < 99)d. (x > 10) && (x < 20) && (y > 0) && (y < 99)
Feedback
The correct answer is: (x > 10) && (x < 20) && (y > 0) && (y < 99)
Question 13
Incorrect0.00 points out of 2.50Flag question
Question text
Which statement results in x having a maximum value of 200?Select one:a. x = (x > 200) ? x : 200;b. x = (x > 200) ? 200 : x;c. x = (x < 200) ? x : 300;d. x = (x < 200) ? 200 : x;
Feedback
The correct answer is: x = (x > 200) ? 200 : x;
Question 14
Correct2.50 points out of 2.50Flag question
Question text
Which expression determines whether x is not negative?Select one:a. !(x > 0)b. !(x >= 1)c. !(x < 0)
d. !(x == 0)
Feedback
The correct answer is: !(x < 0)
Question 15
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int i = 0;
while (i < 5) {
cout << i << ” “;
i = i + 1;
}
return 0;
}Select one:a. 1 2 3 4 5b. 0 1 2 3 4
c. 0 1 2 3 4 5d. 1 2 3 4
Feedback
The correct answer is: 0 1 2 3 4
Question 16
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
string cityState = “Kansas City, Kansas.”;
cout << cityState.find(“City”);
return 0;
}Select one:a. -1b. 6c. 7
d. 8
Feedback
The correct answer is: 7
Question 17
Correct2.50 points out of 2.50Flag question
Question text
Which expression determines whether x is greater than y or x is greater than z?Select one:a. (x > y) && (x > z)b. (x > y) || (x > z)
c. (x < y) || (x < z)d. (x > y) && (y > z)
Feedback
The correct answer is: (x > y) || (x > z)
Question 18
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 15; int y = 30;
if (x > 0) {
y = 0;
}
if (x < 20) {
y = 10;
}
cout << y;
return 0;
}Select one:a. 0b. 10
c. 15d. 30
Feedback
The correct answer is: 10
Question 19
Correct2.50 points out of 2.50Flag question
Question text
What is the output of the following code?
int main() {
int x = 1; int y = 1;
while ( y < 4 ) {
cout << (x * y) << ” “;
y = y + 1;
x = x + 2;
}
return 0;
}Select one:a. 1 4 9b. 1 6c. 1 6 15
d. 1 6 15 28
Feedback
The correct answer is: 1 6 15
Question 20
Correct2.50 points out of 2.50Flag question
Question text
Define a variable of the type enumeration type StopLight named currLight.Select one:a. StopLight = currLight;b. StopLight currLight;
c. currLight = StopLight;d. currLight StopLight;
Feedback
The correct answer is: StopLight currLight;
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
