0
0
Swiftprogramming~10 mins

Why testing 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 print a test message.

Swift
print([1])
Drag options to blanks, or click blank then click option'
A"Test passed!"
BTest passed!
Ctest passed
Dprint("Test passed!")
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the text.
Trying to print the message without quotes.
2fill in blank
medium

Complete the code to check if a number is positive.

Swift
let number = 5
if number [1] 0 {
    print("Positive number")
}
Drag options to blanks, or click blank then click option'
A==
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than symbol instead of greater than.
Using equality instead of comparison.
3fill in blank
hard

Fix the error in the function to return true if input is even.

Swift
func isEven(_ number: Int) -> Bool {
    return number [1] 2 == 0
}
Drag options to blanks, or click blank then click option'
A/
B-
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using division instead of modulo.
Using multiplication or subtraction by mistake.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

Swift
let words = ["apple", "cat", "banana", "dog"]
let lengths = [word: [1] for word in words if word.[2] > 3]
Drag options to blanks, or click blank then click option'
Aword.count
Bcount
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or size which are not valid in Swift strings.
Forgetting to use .count property.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 2.

Swift
let data = ["a": 1, "b": 3, "c": 5]
let result = [[1]: [2] for (key, value) in data where value [3] 2]
Drag options to blanks, or click blank then click option'
Akey.uppercased()
Bvalue
C>
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of key.uppercased().
Using wrong comparison operators like < or ==.