0
0
Swiftprogramming~10 mins

Swift REPL and Playgrounds - Interactive Code Practice

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

Complete the code to print "Hello, Swift!" in a Playground.

Swift
print([1])
Drag options to blanks, or click blank then click option'
A'Hello, Swift!'
B"Hello, Swift!"
CHello, Swift!
DHello Swift
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string.
Using single quotes instead of double quotes.
2fill in blank
medium

Complete 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'
A"10"
Bcount
Cten
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Putting numbers inside quotes, which makes them strings.
Using variable names instead of values.
3fill in blank
hard

Fix 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using invalid operators.
4fill in blank
hard

Fill 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'
Aword
Bword.count
Cword.length
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.length' which is not valid in Swift (use .count).
Using 'count' alone without specifying the word.
5fill in blank
hard

Fill 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'
Akey
Bvalue
Dval
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'.