0
0
Swiftprogramming~10 mins

Array creation and type inference 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 create an empty array of integers.

Swift
var numbers = [[1]]()
Drag options to blanks, or click blank then click option'
ADouble
BString
CInt
DBool
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the intended array elements.
Forgetting to specify the type inside the brackets.
2fill in blank
medium

Complete the code to create an array with three string elements.

Swift
let fruits = ["Apple", "Banana", [1]]
Drag options to blanks, or click blank then click option'
A"Orange"
B"Carrot"
C"Potato"
D"Milk"
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a non-fruit string.
Forgetting the quotes around the string.
3fill in blank
hard

Fill in the blank so that Swift infers the array type as [Double].

Swift
var values = [1, 2, [1]]
Drag options to blanks, or click blank then click option'
A3.5
B"3.5"
C3
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number.
Using an integer literal (causes inference to [Int]).
4fill in blank
hard

Fill both blanks to create an array of booleans with two elements.

Swift
let flags: [Bool] = [[1], [2]]
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C"true"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of booleans.
Using numbers instead of booleans.
5fill in blank
hard

Fill all three blanks to create a dictionary from strings to integers.

Swift
let scores = [[1]: [2], [3]: 90]
Drag options to blanks, or click blank then click option'
A"Alice"
B85
C"Bob"
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers as keys without quotes.
Using strings as values instead of integers.