Vidyalelo
Ruby Programming · Q15

Data Types and Variables in Ruby

Programming · Ruby Programming · question 15

Q15

What is the output of the expression: "5" + "2" in Ruby?

A.
"7"
B.
7
C.
52
Answer
D.
Error

Answer: Option C

Solution

Answer: Option C
Solution:
Question: What is the output of the expression: "5" + "2" in Ruby?

Explanation:
In Ruby, when you use the "+" operator with strings (text enclosed in double quotes), it performs string concatenation, not mathematical addition.

This means it joins the two strings together.

So, "5" + "2" combines "5" and "2" to create the new string "52".

Therefore, the correct answer is Option C: 52

Important Note: If you wanted to perform mathematical addition, you would need to use numbers without quotes, like 5 + 2, which would result in 7. But in this case, because the inputs are strings, Ruby treats the "+" as a string concatenation operator.