Challenge - 5 Problems
Ruby Number Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code with integer division?
Consider the following Ruby code snippet. What will it print?
Ruby
result = 7 / 2 puts result
Attempts:
2 left
💡 Hint
In Ruby, dividing two integers with `/` returns an integer if both are integers.
✗ Incorrect
In Ruby, dividing two integers with `/` performs integer division, so 7 / 2 equals 3, discarding the remainder.
❓ Predict Output
intermediate2:00remaining
What is the output when dividing an integer by a float?
Look at this Ruby code. What will it print?
Ruby
result = 7 / 2.0 puts result
Attempts:
2 left
💡 Hint
When one number is a float, Ruby returns a float result.
✗ Incorrect
Since 2.0 is a float, Ruby performs floating-point division, so 7 / 2.0 equals 3.5.
🧠 Conceptual
advanced2:00remaining
What error does this Ruby code raise?
What error will this Ruby code produce when run?
Ruby
num = 5.0 num = num.to_i + "3"
Attempts:
2 left
💡 Hint
Adding an integer and a string directly causes a type problem.
✗ Incorrect
You cannot add an integer and a string directly in Ruby; this causes a TypeError.
❓ Predict Output
advanced2:00remaining
What is the output of this Ruby code mixing floats and integers?
What will this Ruby code print?
Ruby
a = 10 b = 3 c = a / b.to_f puts c.class puts c
Attempts:
2 left
💡 Hint
Converting one operand to float changes the division result type.
✗ Incorrect
b.to_f converts b to float, so a / b.to_f returns a float. The class is Float and value is 3.3333333333333335.
❓ Predict Output
expert2:00remaining
What is the output of this Ruby code using Float precision?
Examine this Ruby code. What will it print?
Ruby
x = 0.1 + 0.2 puts x == 0.3 puts x
Attempts:
2 left
💡 Hint
Floating point numbers can have small precision errors.
✗ Incorrect
Due to floating point precision, 0.1 + 0.2 is not exactly 0.3, so the equality is false and the value prints as 0.30000000000000004.