0
0
Swiftprogramming~10 mins

Why operator safety matters in Swift - Test Your Understanding

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

Complete the code to add two numbers safely using Swift operators.

Swift
let sum = 5 [1] 3
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using subtraction (-) instead of addition (+).
Using multiplication (*) or division (/) by mistake.
2fill in blank
medium

Complete the code to check if two values are equal using a safe operator.

Swift
if a [1] b { print("Equal") }
Drag options to blanks, or click blank then click option'
A==
B=
C!=
D=>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using single equals (=) which assigns instead of compares.
Using '=>' which is not a comparison operator in Swift.
3fill in blank
hard

Fix the error in the code by choosing the correct operator for safe division.

Swift
let result = 10 [1] 0
Drag options to blanks, or click blank then click option'
A/
B//
Cรท
D?
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '//' which is not a division operator in Swift.
Using 'รท' which is not recognized in Swift code.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters keys safely.

Swift
let filtered = dictionary.filter { $0.key [1] "a" && $0.value [2] 10 }
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using incorrect operators that cause logic errors.
Mixing up greater than and less than operators.
5fill in blank
hard

Fill all three blanks to safely create a dictionary with keys uppercased and values filtered.

Swift
let safeDict = dictionary.reduce(into: [:]) { result, pair in
    if pair.value [1] 5 {
        result[[2]] = [3]
    }
}
Drag options to blanks, or click blank then click option'
A>
Bpair.key.uppercased()
Cpair.value
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using less than operator which filters wrong entries.
Not uppercasing the key.
Assigning the key instead of the value.