Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the correct message for the number using a switch statement.
Swift
let number = 3 switch number { case 1: print("One") case 2: print("Two") case [1]: print("Three") default: print("Other") }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a case value that does not match the variable, so the default case runs.
✗ Incorrect
The case for number 3 should be '3' to match the variable value and print "Three".
2fill in blank
mediumComplete the code to print "Weekend" for Saturday and Sunday using a switch statement.
Swift
let day = "Sunday" switch day { case "Saturday", [1]: print("Weekend") default: print("Weekday") }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a weekday instead of Sunday in the case.
✗ Incorrect
The case should include "Sunday" along with "Saturday" to print "Weekend" for both days.
3fill in blank
hardFix the error in the switch statement to correctly handle the character input.
Swift
let letter: Character = 'a' switch letter { case [1]: print("Vowel") default: print("Consonant") }
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to match multiple characters in one case using a string or list.
✗ Incorrect
Each case must match a single value. To check multiple vowels, multiple cases or a where clause is needed. Here, only 'a' is handled.
4fill in blank
hardFill both blanks to create a switch that prints the season based on the month number.
Swift
let month = 4 switch month { case 12, 1, 2: print("Winter") case 3, 4, [1]: print("Spring") case [2]: print("Summer") default: print("Fall") }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up month numbers for seasons.
✗ Incorrect
Spring includes months 3,4,5; Summer includes 7. So blanks are 5 and 7.
5fill in blank
hardFill all three blanks to create a switch that categorizes a score into grades.
Swift
let score = 85 switch score { case [1]: print("Fail") case [2]: print("Pass") case [3]: print("Excellent") default: print("Invalid score") }
Attempts:
3 left
💡 Hint
Common Mistakes
Using overlapping or incorrect ranges causing wrong category.
✗ Incorrect
Scores 0-49 fail, 50-79 pass, 80-100 excellent. The ranges match these categories.
