Challenge - 5 Problems
Arithmetic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mixed arithmetic operations
What is the output of this Ruby code?
Ruby
result = 10 + 5 * 2 - 8 / 4 puts result
Attempts:
2 left
💡 Hint
Remember the order of operations: multiplication and division happen before addition and subtraction.
✗ Incorrect
Multiplication and division are done first: 5 * 2 = 10, 8 / 4 = 2. Then addition and subtraction: 10 + 10 - 2 = 18. But wait, check carefully: 10 + (5*2) - (8/4) = 10 + 10 - 2 = 18. So correct output is 18.
❓ Predict Output
intermediate2:00remaining
Integer division vs float division
What will this Ruby code print?
Ruby
a = 7 / 2 b = 7 / 2.0 puts a puts b
Attempts:
2 left
💡 Hint
Division with integers truncates the decimal part, but with floats it keeps it.
✗ Incorrect
7 / 2 is integer division, so result is 3. 7 / 2.0 involves a float, so result is 3.5.
🔧 Debug
advanced2:00remaining
Identify the error in arithmetic expression
What error does this Ruby code raise?
Ruby
x = 5 + * 3 puts x
Attempts:
2 left
💡 Hint
Look carefully at the operator usage between numbers.
✗ Incorrect
The expression '5 + * 3' is invalid syntax because two operators '+' and '*' are adjacent without a number between them.
❓ Predict Output
advanced2:00remaining
Modulo operator behavior
What is the output of this Ruby code?
Ruby
puts -13 % 5
Attempts:
2 left
💡 Hint
Modulo in Ruby always returns a non-negative remainder when the divisor is positive.
✗ Incorrect
In Ruby, -13 % 5 returns 2 because it finds the remainder that makes the result non-negative.
🧠 Conceptual
expert2:00remaining
Result of chained assignment with arithmetic
What is the value of variable 'a' after running this Ruby code?
Ruby
a = b = 10 b += 5 c = a + b
Attempts:
2 left
💡 Hint
Remember that 'a' and 'b' are assigned independently and changing 'b' after does not affect 'a'.
✗ Incorrect
Initially, both 'a' and 'b' are 10. Then 'b' is increased by 5 to 15. 'a' remains 10. So 'a' is 10 after all operations.