0
0
Swiftprogramming~10 mins

Tuples for grouped values 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 a tuple named 'person' with a name and age.

Swift
let person = ("John", [1])
Drag options to blanks, or click blank then click option'
A30
B"John"
Cnil
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for age.
Leaving the blank empty.
2fill in blank
medium

Complete the code to access the first element of the tuple 'coordinates'.

Swift
let coordinates = (x: 10, y: 20)
let xValue = coordinates.[1]
Drag options to blanks, or click blank then click option'
Ay
Bx
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the label 'y' instead of 'x'.
Using numeric indexes instead of labels.
3fill in blank
hard

Fix the error in the code by completing the tuple declaration correctly.

Swift
let result = (status: "OK", code: [1])
Drag options to blanks, or click blank then click option'
Anil
B"200"
Ctrue
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Putting 200 in quotes, making it a string.
Using a boolean or nil instead of a number.
4fill in blank
hard

Fill both blanks to create a tuple with named elements and access the second element.

Swift
let point = ([1]: 5, [2]: 10)
let yValue = point.y
Drag options to blanks, or click blank then click option'
Ax
Bz
Cy
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong labels that don't match the access.
Mixing up the order of labels.
5fill in blank
hard

Fill all three blanks to create a tuple, access its elements, and print the name.

Swift
let person = ([1]: "Alice", [2]: 28)
let name = person.[3]
print(name)
Drag options to blanks, or click blank then click option'
Aname
Bage
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong labels that don't match the tuple.
Accessing 'age' instead of 'name' when printing.