Unit 3: Control Statements and Decision Making - Practice Quiz
1 What is the main purpose of control statements in C?
2 Which of the following is NOT a category of control statements in C?
3 Conditional statements in C execute a block of code based on the result of a:
4
In an if statement, the block of code executes only when the condition is:
5
What is the output of the following code?
c
int x = 5;
if (x > 3)
printf("Yes");
6
In an if-else statement, the else block executes when the condition is:
7
What is the output of the following code?
c
int a = 2;
if (a == 5)
printf("Equal");
else
printf("Not Equal");
8
A nested if statement means:
if without any condition
if statement inside another if statement
if used only inside a loop
if statements written side by side
9
Which keyword is used to define the default case in a switch statement?
10
What happens if break is omitted after a matching case in a switch statement?
11
The expression used in a switch statement must evaluate to which type of value?
12
In a while loop, when is the condition checked?
13
How many times is the body of a do-while loop guaranteed to execute at minimum?
14 Which loop checks its condition at the end (bottom) of the loop?
15
What is the output of the following code?
c
int i = 1;
while (i <= 3) {
printf("%d", i);
i++;
}
16
In a for loop, which part is executed only once, at the very beginning?
17
How many times will the following loop print Hi?
c
for (int i = 0; i < 4; i++)
printf("Hi");
18
What does the break statement do inside a loop?
19
What does the continue statement do inside a loop?
20
The goto statement transfers control to a:
21 Which category of control statements is used to repeatedly execute a block of code based on a condition?
22
What is the value of the expression 5 > 3 && 2 < 1 in C?
23
What does the following code print?
int x = 10;
if (x = 5)
printf("Yes");
else
printf("No");
24
What is the output of the following code?
int a = 7;
if (a % 2 == 0)
printf("Even");
else
printf("Odd");
25
In the code below, which else does the inner if associate with?
if (a > 0)
if (b > 0)
printf("A");
else
printf("B");
if statements
if (a > 0)
if (b > 0)
26
What is the output of the following switch statement?
int n = 2;
switch (n) {
case 1: printf("One");
case 2: printf("Two");
case 3: printf("Three");
default: printf("None");
}
27
Which data type is NOT allowed as a switch expression in C?
28
How many times does the body of this loop execute?
int i = 5;
while (i < 5) {
printf("%d", i);
i++;
}
29
How many times does the body of this loop execute?
int i = 5;
do {
printf("%d", i);
i++;
} while (i < 5);
30
What is the final value printed by this code?
int sum = 0, i = 1;
while (i <= 4) {
sum += i;
i++;
}
printf("%d", sum);
31
What is the output of the following code?
for (int i = 0; i < 3; i++)
printf("%d ", i * i);
32
How many times does the following loop execute?
for (int i = 10; i > 0; i -= 3)
printf("*");
33
What happens when all three expressions of a for loop are omitted, as in for(;;)?
34
What is the output of the following code?
for (int i = 1; i <= 5; i++) {
if (i == 3)
break;
printf("%d ", i);
}
35
What is the output of the following code?
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0)
continue;
printf("%d ", i);
}
36
In nested loops, what does a break statement inside the inner loop terminate?
37
What is the output of the following code?
int i = 0;
start:
printf("%d ", i);
i++;
if (i < 3)
goto start;
38
Which statement about the goto statement in C is correct?
break to work
39
What is the output of this ternary-based code?
int a = 4, b = 9;
int max = (a > b) ? a : b;
printf("%d", max);
40
What is the output of the following nested loop?
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 2; j++)
printf("%d%d ", i, j);
41
What is the output of the following code?
#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a > b)
if (a > 0)
printf("X");
else
printf("Y");
return 0;
}
Y
X
42
What does this program print?
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1: printf("1");
case 2: printf("2");
case 3: printf("3"); break;
default: printf("D");
}
return 0;
}
23D
23
233
2
43
What is the output of the following code?
#include <stdio.h>
int main() {
int i, sum = 0;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
if (i > 7) break;
sum += i;
}
printf("%d", sum);
return 0;
}
25
24
16
9
44
What is printed by this code?
#include <stdio.h>
int main() {
int i = 5;
do {
printf("%d ", i);
i -= 2;
} while (i > 0);
return 0;
}
5 3 1
5 3
3 1
5 3 1 -1
45
What is the output?
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++);
printf("%d", i);
return 0;
}
01234
0
5
46
For which value of n does this print Match?
if (n > 0) {
if (n % 3 == 0) {
if (n % 2 != 0)
printf("Match");
}
}
n = 6
n = -9
n = 12
n = 9
47
What is the output of this code?
#include <stdio.h>
int main() {
int i = 0, sum = 0;
loop:
i++;
if (i == 3) goto skip;
sum += i;
skip:
if (i < 5) goto loop;
printf("%d", sum);
return 0;
}
10
9
12
15
48
What is the output of the nested loop?
#include <stdio.h>
int main() {
int i, j, c = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (j == 2) break;
c++;
}
}
printf("%d", c);
return 0;
}
9
1
6
3
49
What does the following print?
#include <stdio.h>
int main() {
int x = 1, y = 0;
switch (x) {
case 1: y += 1;
case 2: y += 2;
default: y += 10;
case 3: y += 3;
}
printf("%d", y);
return 0;
}
16
1
13
6
50
What is the output?
#include <stdio.h>
int main() {
int i, j;
for (i = 0, j = 10; i < j; i++, j--)
;
printf("%d %d", i, j);
return 0;
}
5 5
6 4
5 6
4 6
51
What is the output of this program?
#include <stdio.h>
int main() {
int i = 1;
while (i <= 100) {
i *= 2;
}
printf("%d", i);
return 0;
}
128
64
256
100
52
What does this print for the assignment shown?
#include <stdio.h>
int main() {
int a = 3, b = 5;
int max = (a > b) ? a : (b > 10 ? b : a + b);
printf("%d", max);
return 0;
}
5
3
8
10
53
What is the output?
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
printf("%d", i);
}
return 0;
}
124
12345
1235
1245
54
Which statement about this code is correct?
int x = 2;
switch (x) {
case 1.5: printf("A"); break;
case 2: printf("B"); break;
}
A
B
55
What is the output of this code?
#include <stdio.h>
int main() {
int count = 0;
for (int i = 1; i <= 3; i++)
for (int j = i; j <= 3; j++)
count++;
printf("%d", count);
return 0;
}
5
3
6
9
56
What is printed?
#include <stdio.h>
int main() {
int a = 0;
if (a = 5)
if (a == 5)
printf("P");
else
printf("Q");
else
printf("R");
return 0;
}
P
Q
R
57
How many times does the body execute?
int i = 10;
while (i-- > 0) {
// body
}
58
What does this code output?
#include <stdio.h>
int main() {
int i = 1;
start:
if (i <= 3) {
printf("%d", i);
i++;
goto start;
}
printf("End");
return 0;
}
1End
123End
End
123
59
What is the output?
#include <stdio.h>
int main() {
int x = 5, y = 5, z;
z = x++ || ++y;
printf("%d %d %d", x, y, z);
return 0;
}
6 6 5
6 6 1
5 6 1
6 5 1
60
Which classification correctly groups these C control statements?
1. if, if-else, switch
2. while, do-while, for
3. break, continue, goto, return
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →