0% found this document useful (0 votes)
1 views

lecture 8

The document contains multiple-choice questions (MCQs) focused on key concepts related to the switch statement, break and continue statements, and structured programming practices in C++. Each question provides options and the correct answer, covering topics such as the purpose of switch statements, behavior of break and continue, handling of cases, and best practices in C++. The document serves as a study guide for understanding the use of these programming constructs in C++.

Uploaded by

nadirqalib786ns
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

lecture 8

The document contains multiple-choice questions (MCQs) focused on key concepts related to the switch statement, break and continue statements, and structured programming practices in C++. Each question provides options and the correct answer, covering topics such as the purpose of switch statements, behavior of break and continue, handling of cases, and best practices in C++. The document serves as a study guide for understanding the use of these programming constructs in C++.

Uploaded by

nadirqalib786ns
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Here are multiple-choice questions (MCQs) based on the key concepts and examples discussed,

including the use of switch, break, continue, and structured programming practices:

MCQs:

1. What is the primary purpose of the switch statement in C++?


o A) To handle multiple conditions by comparing an expression to predefined
constants
o B) To break out of a loop
o C) To stop the program from executing
o D) To iterate through a list of values

Answer: A) To handle multiple conditions by comparing an expression to predefined


constants

2. What happens if a break statement is omitted in a switch statement?


o A) The program will throw an error
o B) The code will "fall through," executing all subsequent cases
o C) The switch statement will terminate immediately
o D) It will cause an infinite loop

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) Very Good


4. Which of the following statements is true about the continue statement in C++?
o A) It terminates the current loop entirely.
o B) It skips the current iteration and moves to the next iteration of the loop.
o C) It breaks out of a switch statement.
o D) It stops the entire program.

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

6. Which of the following is a proper use of the switch statement in C++?


o A) Checking multiple boolean conditions
o B) Iterating through a list of values
o C) Handling a specific set of constant values efficiently
o D) Breaking out of loops

Answer: C) Handling a specific set of constant values efficiently

7. How can the default case in a switch statement be best described?


o A) It executes if none of the other cases match.
o B) It is required in every switch statement.
o C) It executes before any other case.
o D) It is used to break out of the switch statement.

Answer: A) It executes if none of the other cases match.

8. Which statement is true about case sensitivity in switch statements?


o A) Switch statements are case-insensitive by default.
o B) To handle both uppercase and lowercase letters, you need separate cases for
each letter.
o C) The switch statement converts all inputs to lowercase before comparison.
o D) Case sensitivity doesn't apply to character data types in switch.

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

Answer: B) The default case (if present) is executed

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;

cin >> salary;

switch (salary / 10000) {


case 0: deduction = 0; break;
case 1: deduction = 1000; break;
default: deduction = salary * 7 / 100; break;
}

netPayable = salary - deduction;


cout << "Net Payable = " << netPayable;

o A) 15,000
o B) 14,000
o C) 13,500
o D) 12,500

Answer: B) 14,000

11. Why is it recommended to avoid using goto statements in C++?


o A) It causes the program to run faster
o B) It makes the flow of control more structured and readable
o C) It leads to unstructured jumps in code, making the program harder to follow
o D) It automatically breaks out of loops

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.

Answer: B) The salary is divided by 10,000 to categorize it into three ranges.

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;

Answer: A) 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;

cin >> salary;

switch (salary / 10000) {


case 0: deduction = 0; break;
case 1: deduction = 1000; break;
default: deduction = salary * 7 / 100; break;
}

netPayable = salary - deduction;


cout << "Net Payable = " << 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.

21. What is the main purpose of the continue statement in loops?


o A) To skip the current iteration of the loop and proceed to the next iteration.
o B) To terminate the loop immediately.
o C) To exit the switch statement.
o D) To run a nested loop.

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.

23. Which statement is true about nested switch statements in C++?


o A) You cannot use a switch inside another switch statement.
o B) A switch statement can be used inside another switch statement.
o C) Nested switch statements are not supported in C++.
o D) Only one switch statement can exist in a program.

Answer: B) A switch statement can be used inside another switch statement.

24. What is the expected output of the following code snippet?

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

Answer: B) Very Good

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.

Answer: A) The code in case 5: will execute.

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;

cin >> salary;

switch (salary / 10000) {


case 0: deduction = 0; break;
case 1: deduction = 1000; break;
default: deduction = salary * 7 / 100; break;
}

cout << "Deduction: " << deduction;

o A) The deduction will be 2,100.


o B) The deduction will be 3,000.
o C) The deduction will be 2,100.
o D) The deduction will be 0.

Answer: A) The deduction will be 2,100.


30. Which of the following is an example of good practice in switch statements?
o A) Always use default to handle unexpected cases.
o B) Avoid using break statements.
o C) Use goto for easier flow control.
o D) Write all cases without indentation.

Answer: A) Always use default to handle unexpected cases.

You might also like