0
0
Swiftprogramming~10 mins

Type inference by the compiler 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 declare a variable with type inferred as Int.

Swift
var number = [1]
Drag options to blanks, or click blank then click option'
A"42"
B3.14
C42
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number makes it a String, not Int.
Using a decimal number makes it a Double, not Int.
2fill in blank
medium

Complete the code to declare a constant with type inferred as Double.

Swift
let pi = [1]
Drag options to blanks, or click blank then click option'
A3.14159
B3
C"3.14159"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer literal will infer Int, not Double.
Using quotes makes it a String, not Double.
3fill in blank
hard

Fix the error in the code by completing the assignment with correct type inference.

Swift
var isActive = [1]
Drag options to blanks, or click blank then click option'
A1
Btrue
C"true"
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes makes it a String, not Bool.
Using numbers like 1 or 0 does not infer Bool.
4fill in blank
hard

Fill both blanks to create a dictionary with type inferred by the compiler.

Swift
let ages = [[1]: [2]]
Drag options to blanks, or click blank then click option'
A"Alice"
B30
C"Bob"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using Boolean values as dictionary values causes type mismatch.
Using numbers as keys without quotes causes errors.
5fill in blank
hard

Fill all three blanks to filter the dictionary using a trailing closure with type inference and a condition.

Swift
let filteredAges = ages.filter { [1], [2] in [2] [3] 25 }
Drag options to blanks, or click blank then click option'
Aname
Bage
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter names for keys or values.
Using the less than symbol reverses the filter condition.