C# Switch MCQ

1. Which of the following is the correct syntax for a switch statement in C#?

a) switch(variable) { case value: /* code */ }
b) switch variable { case value: /* code */ }
c) switch(variable) { when value: /* code */ }
d) switch(variable) { if value: /* code */ }

Answer:

a) switch(variable) { case value: /* code */ }

Explanation:

The correct syntax includes the 'switch' keyword followed by the variable in parentheses, and 'case' keywords for different values.

2. What keyword is used to terminate a case in a switch statement in C#?

a) end
b) stop
c) break
d) exit

Answer:

c) break

Explanation:

The 'break' keyword is used to terminate a case in a switch statement and exit the switch block.

3. How does a switch statement in C# behave if the break statement is omitted in a case?

a) It will throw a compile-time error.
b) It will continue executing the next case.
c) It will exit the switch statement.
d) It will repeat the current case.

Answer:

b) It will continue executing the next case.

Explanation:

If the 'break' statement is omitted, the switch statement will perform "fall-through" and execute the code in the next case.

4. Which of the following data types can be used in a C# switch statement?

a) int
b) string
c) char
d) All of the above

Answer:

d) All of the above

Explanation:

The switch statement in C# can be used with several data types including int, string, and char.

5. What is the role of the 'default' case in a switch statement in C#?

a) To define the initial case
b) To catch any values not matched by other cases
c) To set a default return value
d) To optimize the switch performance

Answer:

b) To catch any values not matched by other cases

Explanation:

The 'default' case in a switch statement is executed if none of the other case values match the switch expression.

6. Can a case in a switch statement in C# have multiple values?

a) Yes, by using commas to separate values
b) No, each case can have only one value
c) Yes, by using the 'or' operator
d) No, it throws a syntax error

Answer:

a) Yes, by using commas to separate values

Explanation:

In C#, a case statement can match multiple values by specifying them as a comma-separated list.

7. What is the output of the following code?


int x = 2;
switch (x)
{
    case 1:
    case 2:
    Console.WriteLine("One or Two");
    break;
    default:
    Console.WriteLine("Other");
}
a) One or Two
b) Other
c) No output
d) Error

Answer:

a) One or Two

Explanation:

Since x is 2, the case for 2 is executed, printing "One or Two".

8. How do you handle multiple conditions with the same code in a switch statement?

a) By using if-else statements inside the switch
b) By stacking case statements without a break until the last one
c) By creating a function and calling it in each case
d) By using the '&&' operator in the case statement

Answer:

b) By stacking case statements without a break until the last one

Explanation:

In C#, you can stack multiple case statements one after the other without a break statement until the last one to handle multiple conditions with the same code.

9. Which of the following is a valid use of the switch statement in C#?

a) switch(x) { case 1: /* code */; case 2: /* code */; default: /* code */ }
b) switch(x) { 1: /* code */; 2: /* code */; else: /* code */ }
c) switch(x) { when 1: /* code */; when 2: /* code */; otherwise: /* code */ }
d) switch(x) { if 1: /* code */; if 2: /* code */; else: /* code */ }

Answer:

a) switch(x) { case 1: /* code */; case 2: /* code */; default: /* code */ }

Explanation:

The correct syntax for a switch statement in C# uses 'case' for different values and 'default' for the default case.

10. In C#, can a switch statement be nested inside another switch statement?

a) Yes
b) No
c) Only inside a default case
d) Only with a special compiler directive

Answer:

a) Yes

Explanation:

C# allows nesting of switch statements, where one switch statement can be placed inside another.

11. What is required to use a string in a switch statement in C#?

a) The string must be constant.
b) The string should be converted to a char array.
c) No special requirements; strings can be used directly.
d) The string needs to be normalized.

Answer:

c) No special requirements; strings can be used directly.

Explanation:

In C#, strings can be used directly in switch statements without any special requirements.

12. What happens if the switch expression matches no case and there is no default case?

a) The first case is executed
b) The last case is executed
c) An exception is thrown
d) Nothing is executed

Answer:

d) Nothing is executed

Explanation:

If there's no matching case and no default case, the switch statement does nothing.

13. Can a switch statement in C# be used with boolean values?

a) Yes, but only with true and false cases
b) No, switch statements don't support boolean values
c) Yes, with any boolean expression
d) No, it requires conversion to an integer

Answer:

a) Yes, but only with true and false cases

Explanation:

A switch statement in C# can be used with boolean values, but the cases must be explicitly true or false.

14. How do you ensure that a switch case in C# falls through to the next case?

a) By using the fallthrough keyword
b) By omitting the break statement
c) By writing 'continue;'
d) C# does not allow fall-through by default

Answer:

b) By omitting the break statement

Explanation:

In C#, a case will fall through to the next one if the break statement is omitted. Note that explicit fall-through is not allowed; only when no code is executed in a case.

15. Which of the following is not a valid type for switch expression in C#?

a) int
b) enum
c) float
d) char

Answer:

c) float

Explanation:

In C#, the switch statement does not support the float data type for the switch expression.

Leave a Comment

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

Scroll to Top