0
0
Swiftprogramming~20 mins

Numeric literal formats in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Numeric Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of integer literals with underscores
What is the output of this Swift code?
let number = 1_000_000
print(number)
Swift
let number = 1_000_000
print(number)
A1000000
B100000
C1000_000
D1_000_000
Attempts:
2 left
💡 Hint
Underscores in numeric literals are ignored by the compiler and used only for readability.
Predict Output
intermediate
2:00remaining
Output of hexadecimal floating-point literal
What is the output of this Swift code?
let hexFloat = 0xFp2
print(hexFloat)
Swift
let hexFloat = 0xFp2
print(hexFloat)
A15.0
B60.0
C240.0
D30.0
Attempts:
2 left
💡 Hint
0xF is 15 in decimal. The p2 means multiply by 2 to the power of 2.
🔧 Debug
advanced
2:00remaining
Identify the error in numeric literal
Which option will cause a compile-time error in Swift due to invalid numeric literal format?
Alet num = 0o755
Blet num = 0x1A3F
Clet num = 1_000_000
Dlet num = 0b102
Attempts:
2 left
💡 Hint
Binary literals can only contain 0 or 1 digits.
Predict Output
advanced
2:00remaining
Output of floating-point literal with exponent
What is the output of this Swift code?
let value = 1.25e3
print(value)
Swift
let value = 1.25e3
print(value)
A0.00125
B1.25
C1250.0
D125.0
Attempts:
2 left
💡 Hint
The e3 means multiply by 10 to the power of 3.
🧠 Conceptual
expert
2:00remaining
Number of items in dictionary from numeric literal keys
What is the number of items in the dictionary created by this Swift code?
let dict = [0x1: "one", 0b1: "binary one", 1: "decimal one"]
print(dict.count)
Swift
let dict = [0x1: "one", 0b1: "binary one", 1: "decimal one"]
print(dict.count)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
All keys represent the same integer value 1 in different numeric literal formats.