■ Quiz: Loops & Switch Coverage
Part A – Identification (20 items)
1. What type of structure is used to repeat execution of a block of code until a condition is met?
2. What are the three main components of a loop?
3. Which type of loop checks the condition after executing the body?
4. Which type of loop checks the condition before executing the body?
5. Which loop is best used when the number of iterations is known in advance?
6. Which keyword is used to exit a loop or switch immediately?
7. What keyword in switch is equivalent to “else” in if-else?
8. What is the general syntax of a for loop?
9. Which loop guarantees at least one execution of the body?
10. What is the term for a loop inside another loop?
11. Which loop is typically used when the number of repetitions is indefinite?
12. In a switch, what happens if break is missing after a case?
13. What types of values can be used in a switch expression?
14. In a for loop, what part is executed only once before looping begins?
15. In a for loop, what part is executed after every iteration to change the counter variable?
16. What do we call a loop that never terminates?
17. What is the default case in a switch statement used for?
18. Which C standard requires support for at least 1023 cases in a switch?
19. Which looping structure is usually used for menu-driven programs?
20. Which loop variation counts backward (e.g., for(i=10; i>=1; i--))?
Part B – Output Tracing (3 items)
21. What is the output?
int i=1;
while(i<=3){
printf("%d ", i);
i++;
}
22. What is the output?
int x=5;
do{
printf("%d ", x);
x--;
}while(x>2);
23. What is the output if input = 2?
int choice=2;
switch(choice){
case 1: printf("Apple"); break;
case 2: printf("Banana"); break;
case 3: printf("Cherry"); break;
default: printf("Invalid");
}
Part C – Coding (3 items)
24. Write a C program using a for loop that prints the first 10 even numbers.
25. Write a C program using a while loop that asks the user to enter numbers until a negative
number is entered, then prints the sum of all positive numbers.
26. Write a C program that takes a grade (A, B, C, D, or F) as input and prints the description
(Excellent, Good, Average, Poor, Fail).
■ Answer Key
Part A – Identification
1. Loop/Iteration
2. Initialization, Condition, Update (plus Body)
3. do-while loop
4. while loop
5. for loop
6. break
7. default
8. for(initialization; condition; update){ }
9. do-while loop
10. Nested loop
11. while loop
12. Fall-through to the next case
13. int, char, enum, or integral types
14. Initialization
15. Update
16. Infinite loop
17. Executes when no case matches
18. C99
19. Switch
20. Decrementing for loop
Part B – Output Tracing
21. Output: 1 2 3
22. Output: 5 4 3
23. Output: Banana
Part C – Coding (Sample Answers)
Q24:
for(int i=2; i<=20; i+=2){
printf("%d ", i);
}
Q25:
int num, sum=0;
printf("Enter numbers (negative to stop):\n");
scanf("%d", &num);
while(num>=0){
sum+=num;
scanf("%d", &num);
}
printf("Sum = %d", sum);
Q26:
char grade;
scanf(" %c", &grade);
switch(grade){
case 'A': printf("Excellent"); break;
case 'B': printf("Good"); break;
case 'C': printf("Average"); break;
case 'D': printf("Poor"); break;
case 'F': printf("Fail"); break;
default: printf("Invalid Grade");
}