Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Switch statement execution in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to execute the correct case in the switch statement.

C Sharp (C#)
int number = 2;
switch (number) {
    case 1:
        Console.WriteLine("One");
        break;
    case [1]:
        Console.WriteLine("Two");
        break;
    default:
        Console.WriteLine("Other");
        break;
}
Drag options to blanks, or click blank then click option'
A3
B2
C0
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a case label that does not match the variable value.
Forgetting to include the break statement.
2fill in blank
medium

Complete the code to print the correct message for the given day number.

C Sharp (C#)
int day = 5;
switch (day) {
    case 1:
        Console.WriteLine("Monday");
        break;
    case 5:
        Console.WriteLine("[1]");
        break;
    default:
        Console.WriteLine("Invalid day");
        break;
}
Drag options to blanks, or click blank then click option'
ASunday
BSaturday
CWednesday
DFriday
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the day number with the wrong weekday name.
Not matching the case value with the variable.
3fill in blank
hard

Fix the error in the switch statement to correctly handle the input character.

C Sharp (C#)
char grade = 'B';
switch (grade) {
    case 'A':
        Console.WriteLine("Excellent");
        break;
    case [1]:
        Console.WriteLine("Good");
        break;
    default:
        Console.WriteLine("Needs Improvement");
        break;
}
Drag options to blanks, or click blank then click option'
A'B'
B"B"
CB
D"b"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
Not quoting the character at all.
4fill in blank
hard

Fill both blanks to complete the switch statement that prints the season based on the month number.

C Sharp (C#)
int month = 3;
switch (month) {
    case [1]:
    case 4:
    case 5:
        Console.WriteLine("Spring");
        break;
    case [2]:
    case 7:
    case 8:
        Console.WriteLine("Summer");
        break;
    default:
        Console.WriteLine("Other season");
        break;
}
Drag options to blanks, or click blank then click option'
A3
B6
C9
D12
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up month numbers for seasons.
Not using the correct case labels.
5fill in blank
hard

Fill all three blanks to create a switch statement that prints the type of day based on the day number.

C Sharp (C#)
int day = 7;
switch (day) {
    case [1]:
    case 7:
        Console.WriteLine("Weekend");
        break;
    case [2]:
    case [3]:
    case 5:
        Console.WriteLine("Weekday");
        break;
    default:
        Console.WriteLine("Invalid day");
        break;
}
Drag options to blanks, or click blank then click option'
A6
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing weekend and weekday day numbers.
Not including all necessary case labels.

Practice

(1/5)
1.

What is the main purpose of a switch statement in C#?

easy
A. To repeat a block of code multiple times
B. To execute different code blocks based on the value of a variable
C. To define a function
D. To declare variables

Solution

  1. Step 1: Understand the role of switch

    A switch statement selects one of many code blocks to execute based on a variable's value.
  2. Step 2: Compare with other options

    Loops repeat code, functions define reusable code, and variable declarations create variables, none of which match switch's purpose.
  3. Final Answer:

    To execute different code blocks based on the value of a variable -> Option B
  4. Quick Check:

    Switch chooses code by value = A [OK]
Hint: Switch chooses code based on variable value [OK]
Common Mistakes:
  • Confusing switch with loops
  • Thinking switch defines functions
  • Believing switch declares variables
2.

Which of the following is the correct syntax to start a switch statement in C#?

?
easy
A. switch (variable) { case 1: break; }
B. switch variable { case 1: break; }
C. switch (variable) case 1: break;
D. switch { variable case 1: break; }

Solution

  1. Step 1: Recall switch syntax

    The correct syntax uses parentheses around the variable and braces around cases: switch (variable) { ... }
  2. Step 2: Check each option

    switch (variable) { case 1: break; } matches correct syntax; others miss parentheses or braces or have wrong order.
  3. Final Answer:

    switch (variable) { case 1: break; } -> Option A
  4. Quick Check:

    Parentheses and braces required = C [OK]
Hint: Switch needs (variable) and { cases } syntax [OK]
Common Mistakes:
  • Omitting parentheses around variable
  • Missing braces around cases
  • Incorrect order of keywords
3.

What will be the output of this C# code?

int day = 3;
switch (day) {
  case 1:
    Console.WriteLine("Monday");
    break;
  case 2:
    Console.WriteLine("Tuesday");
    break;
  case 3:
    Console.WriteLine("Wednesday");
    break;
  default:
    Console.WriteLine("Invalid day");
    break;
}
medium
A. Wednesday
B. Monday
C. Tuesday
D. Invalid day

Solution

  1. Step 1: Identify the switch variable value

    The variable day is 3.
  2. Step 2: Match case with variable

    Case 3 matches, so it prints "Wednesday" and then breaks.
  3. Final Answer:

    Wednesday -> Option A
  4. Quick Check:

    day=3 prints Wednesday [OK]
Hint: Match variable value to case label [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Choosing default when case matches
  • Mixing case numbers and output
4.

Find the error in this switch statement and select the fix:

int number = 2;
switch (number) {
  case 1:
    Console.WriteLine("One");
  case 2:
    Console.WriteLine("Two");
  default:
    Console.WriteLine("Default");
}
medium
A. Add semicolon after switch statement
B. Remove the default case
C. Change switch to if
D. Add break; after each case block

Solution

  1. Step 1: Identify missing break statements

    Each case lacks a break, causing fall-through to next cases.
  2. Step 2: Fix by adding breaks

    Adding break after each case stops fall-through and executes only matched case.
  3. Final Answer:

    Add break; after each case block -> Option D
  4. Quick Check:

    Break stops fall-through = A [OK]
Hint: Add break to stop fall-through in switch [OK]
Common Mistakes:
  • Ignoring fall-through behavior
  • Removing default case unnecessarily
  • Misusing if instead of switch
5.

Given this code, what will be printed?

int score = 85;
switch (score / 10) {
  case 10:
  case 9:
    Console.WriteLine("Excellent");
    break;
  case 8:
    Console.WriteLine("Good");
    break;
  case 7:
  case 6:
    Console.WriteLine("Pass");
    break;
  default:
    Console.WriteLine("Fail");
    break;
}
hard
A. Excellent
B. Fail
C. Good
D. Pass

Solution

  1. Step 1: Calculate switch expression value

    score is 85, so score / 10 is 8 (integer division).
  2. Step 2: Match case for value 8

    Case 8 prints "Good" and breaks.
  3. Final Answer:

    Good -> Option C
  4. Quick Check:

    85/10=8 prints Good [OK]
Hint: Divide score by 10, match case labels [OK]
Common Mistakes:
  • Not using integer division
  • Missing combined cases for 9 and 10
  • Choosing default incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard