0
0
Swiftprogramming~10 mins

No implicit fallthrough in switch in Swift - Interactive Code Practice

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

Complete the code to match the number to its English word using a switch statement.

Swift
let number = 2
var word = ""
switch number {
case 1:
    word = "One"
case 2:
    word = [1]
default:
    word = "Unknown"
}
print(word)
Drag options to blanks, or click blank then click option'
A"Two"
B"2"
C2
D"Second"
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number instead of a string.
Forgetting the quotes around the word.
2fill in blank
medium

Complete the switch statement to print the correct message for the grade.

Swift
let grade = "B"
switch grade {
case "A":
    print("Excellent")
case [1]:
    print("Good")
default:
    print("Needs Improvement")
}
Drag options to blanks, or click blank then click option'
A"C"
B"D"
C"B"
D"F"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a character without quotes.
Using the wrong grade letter.
3fill in blank
hard

Fix the error in the switch statement by completing the missing case value.

Swift
let day = 3
switch day {
case 1:
    print("Monday")
case [1]:
    print("Tuesday")
default:
    print("Other day")
}
Drag options to blanks, or click blank then click option'
A2
B"2"
C3
D"3"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for the case.
Using the wrong number for the case.
4fill in blank
hard

Fill both blanks to create a switch that prints the correct season for the month number.

Swift
let month = 4
switch month {
case [1]:
    print("Winter")
case [2]:
    print("Spring")
default:
    print("Other season")
}
Drag options to blanks, or click blank then click option'
A12
B3
C4
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values instead of integers.
Mixing up the month numbers for seasons.
5fill in blank
hard

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

Swift
let dayNumber = 6
switch dayNumber {
case [1]:
    print("Weekday")
case [2]:
    print("Weekend")
case [3]:
    print("Weekend")
default:
    print("Invalid day")
}
Drag options to blanks, or click blank then click option'
A1
B6
C7
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning weekend to wrong day numbers.
Using strings instead of integers.