Home Subjects C Programming Control Flow

C Programming
Control Flow

C language from basics to advanced placement prep

52 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 52
Topics in C Programming
Q.21 Medium Control Flow
Which of the following is a valid use of the switch statement in C?
A switch(1.5) { case 1.5: break; }
B switch("hello") { case "hello": break; }
C switch(x) { case 1: case 2: case 3: printf("A"); }
D switch(x > 5) { case true: break; }
Correct Answer:  C. switch(x) { case 1: case 2: case 3: printf("A"); }
EXPLANATION

Switch works with integral types only. Multiple case labels before statements (fall-through) is valid. Strings and floats are not allowed.

Test
Q.22 Medium Control Flow
What does the following code segment accomplish?
int i = 0;
while(i < 5) {
if(i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
}
A Prints: 0 1 2 3 4
B Prints: 0 1 3 4
C Prints: 0 1
D Infinite loop
Correct Answer:  B. Prints: 0 1 3 4
EXPLANATION

When i==2, continue skips printf but still increments i. So 2 is not printed. Output: 0 1 3 4

Test
Q.23 Medium Control Flow
Consider the code:
int x = 5;
if(x > 3)
if(x > 10)
printf("A");
else
printf("B");
Which statement best describes the output?
A Prints 'A' because x > 3
B Prints 'B' due to else binding to inner if
C Prints nothing
D Compilation error due to ambiguity
Correct Answer:  B. Prints 'B' due to else binding to inner if
EXPLANATION

The else binds to the nearest if statement (inner if). Since x=5 is not > 10, the inner condition fails and else executes, printing 'B'.

Test
Q.24 Medium Control Flow
What is the behavior of this code?
int i = 0;
while(1) {
i++;
if(i > 5) break;
}
A Infinite loop
B Loop runs 6 times (i = 1 to 6)
C Loop runs 5 times (i = 1 to 5)
D Syntax error
Correct Answer:  B. Loop runs 6 times (i = 1 to 6)
EXPLANATION

while(1) creates an infinite loop. i increments until i > 5 (i becomes 6), then break exits. Loop runs 6 iterations.

Test
Q.25 Medium Control Flow
Predict the output:
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i + j == 1) continue;
printf("Done");
A Done
B No output
C Done is printed 3 times
D Syntax error
Correct Answer:  A. Done
EXPLANATION

continue only affects the inner loop, not the outer. The loops complete normally and 'Done' is printed once after both loops finish.

Test
Q.26 Medium Control Flow
What happens with this nested if?
if(1) if(0) printf("A"); else printf("B");
A A
B B
C No output
D Syntax error
Correct Answer:  B. B
EXPLANATION

The else binds to the nearest if (inner if). Outer if(1) is true, inner if(0) is false, so else executes printing 'B'.

Test
Q.27 Medium Control Flow
In a switch statement with integer cases, can a case have a floating-point constant?
A Yes, always
B No, never
C Only if explicitly cast
D Only in newer C standards
Correct Answer:  B. No, never
EXPLANATION

Case labels must be constant integral expressions (int, char, etc.). Floating-point constants are not allowed as case labels.

Test
Q.28 Medium Control Flow
What is the final value of x?
int x = 0;
for(int i = 1; i
A 9
B 15
C 10
D 8
Correct Answer:  A. 9
EXPLANATION

continue skips even values. x accumulates: 1(i=1) + 3(i=3) + 5(i=5) = 9. Values i=2,4 are skipped.

Test
Q.29 Medium Control Flow
What will be printed?
for(int i = 0; i < 3; i++) {
if(i == 1) goto skip;
printf("%d ", i);
}
skip: printf("End");
A 0 1 2 End
B 0 2 End
C 0 End
D 1 2 End
Correct Answer:  B. 0 2 End
EXPLANATION

When i=1, goto skip jumps to the skip label, skipping the printf. i continues to 2 in the loop. Output: 0 2 End

Test
Q.30 Medium Control Flow
What is the output?
int x = 5;
switch(x) {
case 5: printf("Five");
case 6: printf("Six");
default: printf("Default");
}
A Five
B FiveSixDefault
C FiveSix
D Six
Correct Answer:  B. FiveSixDefault
EXPLANATION

Without a break statement after case 5, control falls through to case 6 and default. This is called fall-through behavior in switch statements.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips