Challenge - 5 Problems
Swift Numeric Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Underscores in numeric literals are ignored by the compiler and used only for readability.
✗ Incorrect
In Swift, underscores in numeric literals are ignored when the number is compiled. So 1_000_000 is the same as 1000000.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
0xF is 15 in decimal. The p2 means multiply by 2 to the power of 2.
✗ Incorrect
0xF is 15 decimal. p2 means multiply by 2^2 = 4. So 15 * 4 = 60.0.
🔧 Debug
advanced2:00remaining
Identify the error in numeric literal
Which option will cause a compile-time error in Swift due to invalid numeric literal format?
Attempts:
2 left
💡 Hint
Binary literals can only contain 0 or 1 digits.
✗ Incorrect
0b102 is invalid because binary literals can only have digits 0 or 1. The digit '2' is not allowed.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
The e3 means multiply by 10 to the power of 3.
✗ Incorrect
1.25e3 means 1.25 * 10^3 = 1250.0.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
All keys represent the same integer value 1 in different numeric literal formats.
✗ Incorrect
0x1, 0b1, and 1 all represent the integer 1, so the dictionary has only one unique key.