Bird
Raised Fist0
Rubyprogramming~10 mins

Arithmetic operators in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add two numbers and print the result.

Ruby
result = 5 [1] 3
puts result
Drag options to blanks, or click blank then click option'
A*
B-
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' subtracts instead of adding.
Using '*' multiplies instead of adding.
2fill in blank
medium

Complete the code to multiply two numbers and print the result.

Ruby
result = 4 [1] 7
puts result
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds instead of multiplying.
Using '/' divides instead of multiplying.
3fill in blank
hard

Fix the error in the code to correctly divide two numbers and print the result.

Ruby
result = 10 [1] 2
puts result
Drag options to blanks, or click blank then click option'
A/
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' multiplies instead of dividing.
Using '-' subtracts instead of dividing.
4fill in blank
hard

Fill both blanks to calculate the remainder of division and print it.

Ruby
result = 17 [1] 5
puts result.to_s [2] " remainder"
Drag options to blanks, or click blank then click option'
A%
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' instead of '%' for remainder.
Using '-' or '*' instead of '+' for concatenation.
5fill in blank
hard

Fill all three blanks to create a hash with keys as numbers and values as their squares, but only for numbers greater than 3.

Ruby
squares = (1..5).select { |[3]| [3] > 3 }.map { |[3]| [ [1], [2] ] }.to_h
Drag options to blanks, or click blank then click option'
Ax
Bx * x
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using incorrect operators for multiplication.

Practice

(1/5)
1. Which of the following is the correct symbol for multiplication in Ruby?
easy
A. -
B. *
C. +
D. /

Solution

  1. Step 1: Recall basic arithmetic symbols in Ruby

    Ruby uses standard arithmetic symbols: + for addition, - for subtraction, * for multiplication, / for division.
  2. Step 2: Identify the multiplication symbol

    The symbol for multiplication is the asterisk (*).
  3. Final Answer:

    * -> Option B
  4. Quick Check:

    Multiplication = * [OK]
Hint: Remember * means multiply in most languages including Ruby [OK]
Common Mistakes:
  • Confusing + with multiplication
  • Using / instead of *
  • Using - instead of *
2. Which of the following Ruby expressions correctly calculates the remainder of 10 divided by 3?
easy
A. 10 - 3
B. 10 / 3
C. 10 * 3
D. 10 % 3

Solution

  1. Step 1: Understand the modulus operator

    The modulus operator (%) returns the remainder after division.
  2. Step 2: Identify the correct expression for remainder

    10 % 3 calculates the remainder when 10 is divided by 3, which is 1.
  3. Final Answer:

    10 % 3 -> Option D
  4. Quick Check:

    Remainder = % [OK]
Hint: Use % to get remainder after division [OK]
Common Mistakes:
  • Using / instead of %
  • Using * or - which do not give remainder
  • Confusing % with division
3. What is the output of the following Ruby code?
result = 15 / 4
puts result
medium
A. 3
B. SyntaxError
C. 4
D. 3.75

Solution

  1. Step 1: Understand integer division in Ruby

    When dividing two integers, Ruby performs integer division, discarding the decimal part.
  2. Step 2: Calculate 15 divided by 4 as integer division

    15 / 4 equals 3.75, but integer division returns 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Integer division truncates decimals = 3 [OK]
Hint: Integer division drops decimals unless floats used [OK]
Common Mistakes:
  • Expecting float result without floats
  • Confusing / with %
  • Thinking output is 4 or 3.75
4. Identify the error in this Ruby code snippet:
num = 10
result = num / 0
puts result
medium
A. Division by zero error
B. Syntax error due to missing operator
C. No error, output is 0
D. Variable 'num' is undefined

Solution

  1. Step 1: Analyze the division operation

    The code divides 10 by 0, which is mathematically undefined.
  2. Step 2: Understand Ruby's behavior on division by zero

    Ruby raises a ZeroDivisionError when dividing by zero.
  3. Final Answer:

    Division by zero error -> Option A
  4. Quick Check:

    Dividing by zero causes error [OK]
Hint: Never divide by zero; it causes runtime error [OK]
Common Mistakes:
  • Thinking output is 0
  • Assuming syntax error
  • Ignoring runtime exceptions
5. You want to calculate the average of three numbers: 7, 8, and 9 in Ruby. Which expression correctly computes the average as a float?
hard
A. 7 + 8 + 9 / 3
B. (7 + 8 + 9) / 3
C. (7 + 8 + 9) / 3.0
D. (7 + 8 + 9) % 3

Solution

  1. Step 1: Calculate sum of numbers

    Sum is 7 + 8 + 9 = 24.
  2. Step 2: Divide by 3 as float division

    Dividing by 3.0 forces float division, giving 8.0 instead of integer 8.
  3. Final Answer:

    (7 + 8 + 9) / 3.0 -> Option C
  4. Quick Check:

    Use float divisor for float average [OK]
Hint: Divide by float (3.0) to get decimal average [OK]
Common Mistakes:
  • Using integer division losing decimals
  • Misusing % instead of /
  • Wrong operator precedence without parentheses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard