Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print "Hello, Swift!" in a Playground.
Swift
print([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string.
Using single quotes instead of double quotes.
✗ Incorrect
In Swift, strings must be enclosed in double quotes to be printed correctly.
2fill in blank
mediumComplete the code to declare a variable named 'count' with value 10 in Swift REPL.
Swift
var count = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting numbers inside quotes, which makes them strings.
Using variable names instead of values.
✗ Incorrect
Variables holding numbers should be assigned numeric values without quotes.
3fill in blank
hardFix the error in the code to add two numbers and print the result.
Swift
let sum = 5 [1] 3 print(sum)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using invalid operators.
✗ Incorrect
The plus sign (+) adds two numbers in Swift.
4fill in blank
hardFill both blanks in the loop to build a dictionary that maps words to their lengths for words longer than 3 characters. (Assume `let words = ["hello", "hi", "world", "a"]` is defined.)
Swift
var lengths: [String: Int] = [:] for word in words { if word.count > 3 { lengths[[1]] = [2] } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.length' which is not valid in Swift (use .count).
Using 'count' alone without specifying the word.
✗ Incorrect
In Swift, use a for loop to build the dictionary, with 'word' as key and 'word.count' as value.
5fill in blank
hardFill all three blanks in the loop to create a dictionary from a list of tuples where keys are uppercase and values are doubled. (Assume `let data: [(String, Int)] = [("apple", 5), ("banana", 3)]` is defined.)
Swift
var transformed: [String: Int] = [:] for ([3], [2]) in data { transformed[[1].uppercased()] = [2] * 2 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names that don't match between the tuple and usage.
Not uppercasing the key or doubling the value.
Using 'val' instead of 'value'.
✗ Incorrect
Use 'key' for the first and third blanks to access the tuple key, and 'value' for the second blank to double the value. Note the tuple destructuring in the for loop.