0
0
Swiftprogramming~10 mins

Limitations and best practices 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 declare a constant in Swift.

Swift
let [1] = 10
Drag options to blanks, or click blank then click option'
Afunc
Bvar
Cscore
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like var or func as names.
Leaving the blank empty.
2fill in blank
medium

Complete the code to safely unwrap an optional using if-let.

Swift
if let value = optional[1] {
    print(value)
}
Drag options to blanks, or click blank then click option'
AValue
B.
C!
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using ! which forces unwrap and can cause crashes.
Using uppercase letters or dots incorrectly.
3fill in blank
hard

Fix the error in the function declaration to follow Swift best practices.

Swift
func greet(name: String[1] String) {
    print("Hello, \(name)!")
}
Drag options to blanks, or click blank then click option'
A:
B,
C;
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using colons or semicolons instead of commas.
Missing commas causing syntax errors.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters keys with values greater than 5.

Swift
let filtered = dictionary.filter { $0.value [1] 5 }.[2] { $0.key: $0.value }
Drag options to blanks, or click blank then click option'
A>
Bmap
Cfilter
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter twice instead of map.
Using wrong comparison operators.
5fill in blank
hard

Fill all three blanks to create a safe optional binding with a guard statement.

Swift
guard let [1] = optionalValue [2] else {
    [3]
}
Drag options to blanks, or click blank then click option'
Avalue
Belse
Creturn
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of else.
Not returning or exiting in the else block.