lecture 8
lecture 8
including the use of switch, break, continue, and structured programming practices:
MCQs:
Answer: B) The code will "fall through," executing all subsequent cases
3. What will be the output of the following program if the user inputs B?
cpp
Copy code
char grade;
cout << "Enter grade: ";
cin >> grade;
switch (grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Very Good"; break;
case 'C': cout << "Good"; break;
case 'D': cout << "Poor"; break;
case 'F': cout << "Fail"; break;
default: cout << "Invalid grade"; break;
}
o A) Excellent
o B) Very Good
o C) Good
o D) Invalid grade
Answer: B) It skips the current iteration and moves to the next iteration of the loop.
5. What is the correct output when the user inputs 5 for i in the following program?
cpp
Copy code
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
cout << "Value of i: " << i << endl;
}
o A) 0 1 2 3 4 6 7 8 9
o B) 0 1 2 3 4 5 6 7 8 9
o C) 0 1 2 3 4
o D) 1 2 3 4 5 6 7 8 9
Answer: A) 0 1 2 3 4 6 7 8 9
Answer: B) To handle both uppercase and lowercase letters, you need separate cases for
each letter.
9. What is the behavior of the switch statement when no case matches the value being
evaluated?
o A) The program crashes
o B) The default case (if present) is executed
o C) The program will execute the first case block
o D) It will result in an infinite loop
10. What will be the net payable salary when the input salary is 15,000, according to the
following program?
cpp
Copy code
int salary;
float deduction, netPayable;
o A) 15,000
o B) 14,000
o C) 13,500
o D) 12,500
Answer: B) 14,000
Answer: C) It leads to unstructured jumps in code, making the program harder to follow
12. What would be the output of the following code snippet if the user enters A for the
grade?
cpp
Copy code
char grade;
cin >> grade;
switch (grade) {
case 'A':
case 'a': cout << "Excellent"; break;
case 'B':
case 'b': cout << "Very Good"; break;
case 'C':
case 'c': cout << "Good"; break;
case 'D':
case 'd': cout << "Poor"; break;
case 'F':
case 'f': cout << "Fail"; break;
default: cout << "Invalid grade"; break;
}
o A) Excellent
o B) Very Good
o C) Good
o D) Invalid grade
Answer: A) Excellent
13. In the salary deduction example, how is the salary divided to apply different
deductions?
o A) The salary is divided by 2 to determine the deduction rate.
o B) The salary is divided by 10,000 to categorize it into three ranges.
o C) The salary is divided by 5,000 for small deductions.
o D) The salary is not divided; deductions are applied directly.
14. What is the advantage of using switch over multiple if-else conditions in C++?
o A) switch is always faster
o B) It simplifies the code and improves readability when handling multiple
conditions
o C) if-else conditions are more efficient in all cases
o D) switch can handle non-integer data types
Answer: B) It simplifies the code and improves readability when handling multiple
conditions
More MCQs:
15. Which of the following is an advantage of using a switch statement over multiple
if-else statements?
o A) It allows for complex conditions with ranges of values.
o B) It improves readability and makes the code easier to manage for multiple
conditions.
o C) It is only suitable for boolean conditions.
o D) It works only for integers.
Answer: B) It improves readability and makes the code easier to manage for multiple
conditions.
16. Which of the following is a correct syntax for the switch statement in C++?
o A) switch (expression) { case value: // statement; break; }
o B) switch(expression) { case value: // statement }
o C) switch (value) { statement; }
o D) switch { expression } case value: statement; break;
17. What will be the output of the following code if the user enters the grade d?
cpp
Copy code
char grade;
cin >> grade;
switch (grade) {
case 'A':
case 'a': cout << "Excellent"; break;
case 'B':
case 'b': cout << "Very Good"; break;
case 'C':
case 'c': cout << "Good"; break;
case 'D':
case 'd': cout << "Poor"; break;
case 'F':
case 'f': cout << "Fail"; break;
default: cout << "Invalid grade"; break;
}
o A) Excellent
o B) Very Good
o C) Good
o D) Poor
Answer: D) Poor
18. In a switch statement, what happens when the break statement is omitted?
o A) The program will execute the next case even if it doesn’t match.
o B) The program will crash.
o C) The program will automatically return to the main function.
o D) It causes an infinite loop.
Answer: A) The program will execute the next case even if it doesn’t match.
19. What is the output of the following program when the input salary is 18,000?
cpp
Copy code
int salary;
float deduction, netPayable;
o A) 17,000
o B) 18,000
o C) 16,200
o D) 17,400
Answer: A) 17,000
20. What will happen if you omit the default case in a switch statement?
o A) The program will skip the entire switch block.
o B) The program will not compile.
o C) If no case matches, nothing will happen, and the control will exit the switch
statement.
o D) The program will enter an infinite loop.
Answer: C) If no case matches, nothing will happen, and the control will exit the switch
statement.
Answer: A) To skip the current iteration of the loop and proceed to the next iteration.
22. In a program, what is the effect of writing continue inside a for loop with an if
condition?
o A) The program will skip the remaining part of the loop and continue with the
next iteration when the condition is met.
o B) The loop will terminate when the condition is met.
o C) The program will go back to the first iteration of the loop.
o D) It will print the value of i every time.
Answer: A) The program will skip the remaining part of the loop and continue with the
next iteration when the condition is met.
cpp
Copy code
char grade = 'B';
switch(grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Very Good"; break;
case 'C': cout << "Good"; break;
default: cout << "Invalid grade"; break;
}
o A) Excellent
o B) Very Good
o C) Good
o D) Invalid grade
25. What does the default case in a switch statement help with?
o A) It provides a case for each possible value.
o B) It is a required case for switch statements.
o C) It handles situations when no other case matches the value being evaluated.
o D) It skips the current iteration of the loop.
Answer: C) It handles situations when no other case matches the value being evaluated.
26. Which of the following is a correct use of the continue statement inside a for loop?
o A) It skips the current iteration and continues to the next iteration.
o B) It terminates the entire loop.
o C) It restarts the loop from the beginning.
o D) It exits the switch statement.
Answer: A) It skips the current iteration and continues to the next iteration.
27. If a switch statement checks for an integer and one of the cases is case 5:, what
would be the result if the variable being evaluated is 5?
o A) The code in case 5: will execute.
o B) The code in the default case will execute.
o C) The program will exit without executing any code.
o D) The program will print Invalid case.
28. What is the correct way to handle both uppercase and lowercase inputs in a switch
statement for a character variable?
o A) Use multiple case labels for both uppercase and lowercase values.
o B) Convert all inputs to uppercase or lowercase before using the switch.
o C) Use a for loop to handle each case.
o D) switch only works with uppercase values.
Answer: A) Use multiple case labels for both uppercase and lowercase values.
29. In the following switch statement, what will happen if the salary is 30,000?
cpp
Copy code
int salary;
float deduction;