Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 361–370 of 490
Topics in C Programming
Q.361 Medium Control Flow
Consider:
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
A Saves memory
B Allows same code for multiple case values without repetition
C Improves execution speed
D Makes code unreadable intentionally
Correct Answer:  B. Allows same code for multiple case values without repetition
EXPLANATION

Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.

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

do-while executes body first (prints 0), then checks ++i < 3. Loop continues for i=1,2. When i=3, condition fails. Output: 0 1 2

Test
Q.363 Medium Control Flow
What is the key distinction between if-else and switch statements in terms of evaluation?
A if-else is faster than switch
B if-else evaluates conditions, switch uses jump table for integral constants
C switch can handle float types but if-else cannot
D if-else allows multiple conditions, switch allows only one
Correct Answer:  B. if-else evaluates conditions, switch uses jump table for integral constants
EXPLANATION

switch typically uses jump tables for efficient multi-way branching with integral values, while if-else evaluates boolean expressions sequentially.

Test
Q.364 Medium Control Flow
Consider the code:
int x = 0;
while(1) {
if(++x > 3) break;
printf("%d ", x);
}
What is the output?
A 0 1 2
B 1 2 3
C 1 2
D Infinite loop with no output
Correct Answer:  B. 1 2 3
EXPLANATION

Pre-increment ++x happens first. When x becomes 4, condition is true and loop breaks. Values printed: x=1,2,3 (before x reaches 4).

Test
Q.365 Medium Control Flow
What happens when you use break in a nested loop structure?
A Breaks both inner and outer loop
B Breaks only the immediate (inner) loop
C Breaks all loops in the program
D Causes compilation error
Correct Answer:  B. Breaks only the immediate (inner) loop
EXPLANATION

break statement exits only the innermost loop it's in. To break outer loop, you need additional logic or goto statement.

Test
Q.366 Medium Control Flow
Analyze: What is printed by this code?
for(int i = 1; i
A 1 22 333
B 1 2 3
C 123 123 123
D 1 22 333 (with spaces)
Correct Answer:  D. 1 22 333 (with spaces)
EXPLANATION

Outer loop: i=1,2,3. Inner loop repeats i times. When i=1: print '1' once. i=2: print '2' twice. i=3: print '3' thrice. Each iteration separated by space.

Test
Q.367 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.368 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.369 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.370 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
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