C Decision Making and Loops MCQ

In C, decision-making structures (like if, if-else, and switch) allow a program to take a path out of several depending on the outcome of an expression or a condition. Loops (like for, while, and do-while) let a part of the code run multiple times based on a condition.

Let’s dive into C’s decision-making constructs and loops and assess your knowledge with some insightful multiple-choice questions.

1. What is the correct syntax for an if statement in C?

A) if expression { }
B) if: expression { }
C) if = expression { }
D) if (expression) { }

Answer:

D) if (expression) { }

Explanation:

The correct syntax involves placing the condition/expression inside parentheses.

2. Which loop guarantees at least one execution of its block of code?

A) for
B) while
C) do-while
D) All of the above

Answer:

C) do-while

Explanation:

The do-while loop tests the condition after executing the loop body, ensuring at least one execution.

3. How many primary decision-making constructs does C have?

A) 1
B) 2
C) 3
D) 4

Answer:

C) 3

Explanation:

C has if, if-else, and switch as its primary decision-making constructs.

4. The switch statement tests _________.

A) Only equality conditions
B) Relational conditions
C) Logical conditions
D) Complex conditions

Answer:

A) Only equality conditions

Explanation:

switch checks for direct equality against its cases.

5. What is the purpose of the break statement inside a switch case?

A) To end the program
B) To terminate the loop
C) To exit the switch and transfer control to the statement following the switch
D) To skip to the next case

Answer:

C) To exit the switch and transfer control to the statement following the switch

Explanation:

The break statement exits the current switch case.

6. Which loop is best for iterating over an array when you know its size?

A) for
B) while
C) do-while
D) switch

Answer:

A) for

Explanation:

The for loop is structured perfectly for iterating a known number of times.

7. What does the continue statement do?

A) Halts the program
B) Exits the loop
C) Skips the current iteration and moves to the next one
D) Executes the next iteration twice

Answer:

C) Skips the current iteration and moves to the next one

Explanation:

The continue statement skips the remainder of the current iteration and proceeds to the next loop iteration.

8. What is the purpose of default in a switch statement?

A) It is executed when no other cases match.
B) It sets the default value for the switch variable.
C) It specifies the default number of cases.
D) It is required in every switch statement.

Answer:

A) It is executed when no other cases match.

Explanation:

The default case is executed if no other case label matches the switch expression.

9. Which of the following loops checks the condition before executing the loop body?

A) for loop
B) while loop
C) do-while loop
D) None of the above

Answer:

B) while loop

Explanation:

The while loop checks its condition before executing the loop body. If the condition is false from the start, the body may not run even once.

10. How many times will the loop body execute in a do-while loop?

A) Zero
B) Once
C) Twice
D) It depends on the condition

Answer:

B) Once

Explanation:

Even if the condition is false, a do-while loop ensures that the loop body is executed at least once.

11. In which loop, the condition is checked at the end of the loop body?

A) for
B) while
C) do-while
D) Both A and B

Answer:

C) do-while

Explanation:

In the do-while loop, the condition is checked after the loop body is executed.

12. For a switch statement, which data type can’t be used for the switch variable?

A) int
B) char
C) double
D) Both A and B

Answer:

C) double

Explanation:

The switch statement does not support double as the switch variable data type.

13. Which of the following will exit a loop prematurely?

A) continue
B) return
C) exit
D) break

Answer:

D) break

Explanation:

The break statement is used to exit a loop prematurely.

14. Which statement skips the current iteration and continues with the next one?

A) skip
B) continue
C) next
D) resume

Answer:

B) continue

Explanation:

The continue statement skips the current iteration and jumps to the next one.

15. In a do-while loop, where is the condition tested?

A) Before entering the loop
B) In the middle of the loop
C) After the loop body has been executed
D) At the end of the program

Answer:

C) After the loop body has been executed

Explanation:

In a do-while loop, the condition is checked after the loop body has been executed.

16. What will be the output of the following code?

int x = 10, y = 20;
if (x > y)
    printf("x is greater");
else
    printf("y is greater");
A) x is greater
B) y is greater
C) x and y are equal
D) No output

Answer:

B) y is greater

Explanation:

Since 10 is not greater than 20, the other part will execute.

17. How many times will this loop execute?

int i = 0;
while(i < 3) {
    printf("%d ", i);
    i++;
}
A) Never
B) Once
C) Three times
D) Infinite times

Answer:

C) Three times

Explanation:

The loop will run for i values 0, 1, and 2.

With that, we conclude our quiz on decision-making and loops in C. We hope it served as a valuable tool to test your understanding. Keep diving deep into the world of C programming!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top