The + operator adds two numbers together. For example, 2 + 3 equals 5.
Click to reveal answer
beginner
How do you perform division in Ruby and what is the result type?
Use the / operator to divide numbers. If both numbers are integers, Ruby returns an integer (floor division). To get a float result, use at least one float number, e.g., 5.0 / 2 returns 2.5.
Click to reveal answer
beginner
What is the difference between * and ** in Ruby?
* multiplies two numbers, while ** raises a number to the power of another. For example, 3 * 4 is 12, but 3 ** 4 is 81 (3 to the power of 4).
Click to reveal answer
beginner
What does the % operator do in Ruby?
The % operator gives the remainder of division between two numbers. For example, 7 % 3 equals 1 because 7 divided by 3 leaves a remainder of 1.
Click to reveal answer
intermediate
How can you combine arithmetic operators in Ruby?
You can combine operators like +, -, *, /, and % in expressions. Ruby follows standard math order: parentheses first, then exponentiation, multiplication/division/modulus, and finally addition/subtraction.
Click to reveal answer
What is the result of 10 / 4 in Ruby if both are integers?
A3
B2.5
C4
D2
✗ Incorrect
When dividing two integers, Ruby returns the integer part only (floor division), so 10 / 4 equals 2.
Which operator raises a number to a power in Ruby?
A**
B*
C+
D%
✗ Incorrect
The ** operator is used for exponentiation in Ruby.
What does 15 % 4 return?
A1
B3
C11
D4
✗ Incorrect
The modulus operator % returns the remainder. 15 divided by 4 leaves remainder 3.
Which operator has the highest precedence in Ruby arithmetic?
A**
B*
C+
D-
✗ Incorrect
Exponentiation ** has higher precedence than multiplication, division, addition, and subtraction.
What is the result of 2 + 3 * 4 in Ruby?
A20
B24
C14
D10
✗ Incorrect
Multiplication happens before addition, so 3 * 4 = 12, then 2 + 12 = 14.
Explain how Ruby handles division between two integers and how to get a decimal result.
Think about how dividing 5 by 2 behaves differently with integers and floats.
You got /3 concepts.
Describe the order of operations for arithmetic operators in Ruby.
Remember the math rule PEMDAS but adapted for Ruby operators.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the correct symbol for multiplication in Ruby?
easy
A. -
B. *
C. +
D. /
Solution
Step 1: Recall basic arithmetic symbols in Ruby
Ruby uses standard arithmetic symbols: + for addition, - for subtraction, * for multiplication, / for division.
Step 2: Identify the multiplication symbol
The symbol for multiplication is the asterisk (*).
Final Answer:
* -> Option B
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
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division.
Step 2: Identify the correct expression for remainder
10 % 3 calculates the remainder when 10 is divided by 3, which is 1.
Final Answer:
10 % 3 -> Option D
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
Step 1: Understand integer division in Ruby
When dividing two integers, Ruby performs integer division, discarding the decimal part.
Step 2: Calculate 15 divided by 4 as integer division
15 / 4 equals 3.75, but integer division returns 3.
Final Answer:
3 -> Option A
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
Step 1: Analyze the division operation
The code divides 10 by 0, which is mathematically undefined.
Step 2: Understand Ruby's behavior on division by zero
Ruby raises a ZeroDivisionError when dividing by zero.
Final Answer:
Division by zero error -> Option A
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
Step 1: Calculate sum of numbers
Sum is 7 + 8 + 9 = 24.
Step 2: Divide by 3 as float division
Dividing by 3.0 forces float division, giving 8.0 instead of integer 8.
Final Answer:
(7 + 8 + 9) / 3.0 -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.