0
0
Rubyprogramming~20 mins

Integer and Float number types in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Number Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A3
B3.5
C4
D7
Attempts:
2 left
💡 Hint
In Ruby, dividing two integers with `/` returns an integer if both are integers.
Predict Output
intermediate
2: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
A3.5
B3
C3.0
D2.5
Attempts:
2 left
💡 Hint
When one number is a float, Ruby returns a float result.
🧠 Conceptual
advanced
2: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"
AZeroDivisionError
BNoMethodError
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Adding an integer and a string directly causes a type problem.
Predict Output
advanced
2: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
A
Integer
3
B
Float
3.3333333333333335
C
Float
3
D
Integer
3.3333333333333335
Attempts:
2 left
💡 Hint
Converting one operand to float changes the division result type.
Predict Output
expert
2: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
A
false
0.3
B
true
0.3
C
false
0.30000000000000004
D
true
0.30000000000000004
Attempts:
2 left
💡 Hint
Floating point numbers can have small precision errors.