What is the behavior of this code?
int i = 0;
while(1) {
i++;
if(i > 5) break;
}
Answer: B
while(1) creates an infinite loop. i increments until i > 5 (i becomes 6), then break exits. Loop runs 6 iterations.
Q.122Medium
Consider the code:
int x = 5;
if(x > 3)
if(x > 10)
printf("A");
else
printf("B");
Which statement best describes the output?
Answer: B
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'.
Q.123Medium
What does the following code segment accomplish?
int i = 0;
while(i < 5) {
if(i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
}
Answer: B
When i==2, continue skips printf but still increments i. So 2 is not printed. Output: 0 1 3 4
Q.124Medium
Which of the following is a valid use of the switch statement in C?
Answer: C
Switch works with integral types only. Multiple case labels before statements (fall-through) is valid. Strings and floats are not allowed.
Q.125Medium
Analyze: What is printed by this code?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= i; j++) {
printf("%d", i);
}
printf(" ");
}
Answer: D
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.
Advertisement
Q.126Medium
What happens when you use break in a nested loop structure?
Answer: B
break statement exits only the innermost loop it's in. To break outer loop, you need additional logic or goto statement.
Q.127Medium
Consider the code:
int x = 0;
while(1) {
if(++x > 3) break;
printf("%d ", x);
}
What is the output?
Answer: B
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).
Q.128Medium
What is the key distinction between if-else and switch statements in terms of evaluation?
Answer: B
switch typically uses jump tables for efficient multi-way branching with integral values, while if-else evaluates boolean expressions sequentially.
Q.129Medium
What does the following code segment do?
int i = 0;
do {
printf("%d ", i);
} while(++i < 3);
Answer: A
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
Q.130Medium
Consider:
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
Answer: B
Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.
Q.131Medium
What does continue do in a for loop with multiple statements in body?
Answer: A
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
Q.132Medium
What is the output of the following code?
int x = 5;
if(x > 3)
if(x < 10)
printf("Pass");
else
printf("Fail");
Answer: A
The inner if condition (x < 10) is true, so 'Pass' is printed. The else is associated with the inner if, not the outer one.
Q.133Medium
What is the output?
int x = 10;
while(x-- > 5) {
printf("%d ", x);
}
Answer: C
x-- returns the value before decrementing. Loop runs while value > 5: prints 9,8,7,6 (when x becomes 5, condition fails).
Q.134Medium
Which of the following statements about switch case is TRUE?
Answer: C
Without break, execution falls through to the next case. Default can be anywhere, and modern C allows switch with other types.
Q.135Medium
What will this code output?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
Answer: A
Nested loop: outer loop runs 3 times, inner loop runs i times. Prints 1, then 12, then 123 on separate lines.
Q.136Medium
How many times will the loop execute?
int i = 0;
while(i++ < 5) {
printf("%d ", i);
}
Answer: B
i++ returns value before incrementing. Condition checks: 0<5(T), 1<5(T), 2<5(T), 3<5(T), 4<5(T), 5<5(F). Loop runs 5 times.
Q.137Medium
What will be the output of this code?
int n = 5;
switch(n) {
case 4: printf("Four");
case 5: printf("Five");
case 6: printf("Six");
break;
default: printf("Other");
}
Answer: B
n=5 matches case 5. Without break, it falls through to case 6 before encountering break. Prints 'FiveSix'.
Q.138Medium
Which control structure would be most efficient to validate if a number is within one of several specific values?
Answer: C
switch statement is optimized for multiple discrete value checks and is more readable than chained if-else statements.
Q.139Medium
In C programming, what is the difference between break in a loop and break in a switch statement?
Answer: C
Both break statements exit their immediate enclosing structure. In loops, break exits the loop; in switch, break exits the switch block.
Q.140Medium
What is the output of this code?
char ch = 'B';
switch(ch) {
case 'A':
case 'B':
case 'C': printf("Vowel");
break;
default: printf("Consonant");
}
Answer: A
ch='B' matches case 'B', which has no statements. Execution falls through to case 'C' which has no statements, then prints 'Vowel' and breaks.