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?
Answer:
Explanation:
The correct syntax involves placing the condition/expression inside parentheses.
2. Which loop guarantees at least one execution of its block of code?
Answer:
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?
Answer:
Explanation:
C has if, if-else, and switch as its primary decision-making constructs.
4. The switch statement tests _________.
Answer:
Explanation:
switch checks for direct equality against its cases.
5. What is the purpose of the break statement inside a switch case?
Answer:
Explanation:
The break statement exits the current switch case.
6. Which loop is best for iterating over an array when you know its size?
Answer:
Explanation:
The for loop is structured perfectly for iterating a known number of times.
7. What does the continue statement do?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The switch statement does not support double as the switch variable data type.
13. Which of the following will exit a loop prematurely?
Answer:
Explanation:
The break statement is used to exit a loop prematurely.
14. Which statement skips the current iteration and continues with the next one?
Answer:
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?
Answer:
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");
Answer:
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++;
}
Answer:
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!
Comments
Post a Comment