0
0
Swiftprogramming~10 mins

Arithmetic operators and overflow 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 add two integers and store the result.

Swift
let sum = 5 [1] 3
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.
2fill in blank
medium

Complete the code to multiply two integers and store the result.

Swift
let product = 7 [1] 4
Drag options to blanks, or click blank then click option'
A/
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or division instead of multiplication.
3fill in blank
hard

Fix the error in the code to correctly handle overflow addition.

Swift
let result = UInt8.max [1] 1
Drag options to blanks, or click blank then click option'
A-
B+
C&+
D&-
Attempts:
3 left
💡 Hint
Common Mistakes
Using normal addition operator which causes runtime error on overflow.
4fill in blank
hard

Fill both blanks to perform overflow subtraction and multiplication.

Swift
let difference = UInt8.min [1] 1
let product = 10 [2] 20
Drag options to blanks, or click blank then click option'
A&-
B*
C+
D&+
Attempts:
3 left
💡 Hint
Common Mistakes
Using normal subtraction which causes error on underflow.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as strings and values as overflow sums.

Swift
let results = ["a": 5 [1] 250, "b": 10 [2] 245, "c": 15 [3] 240]
Drag options to blanks, or click blank then click option'
A&+
B+
C-
D&-
Attempts:
3 left
💡 Hint
Common Mistakes
Using normal + operator which can cause overflow errors.