0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.