Ruby Programming Questions and Answers – Comparisons

«
»

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Comparisons”.

1. The == ‘is equal to’ is known as relational operator.
a) True
b) False
View Answer

Answer: a
Explanation: The == is used to compare two things are equal or not and hence it is known as comparator/relational operator.

2. The ‘=’ is used for assigning value to variable?
a) True
b) False
View Answer

Answer: a
Explanation: The ‘=’ is known as assignment operator.

3. What will the following code evaluate to?

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
a=9!=10

a) True
b) False
c) Syntax error
d) None of the mentioned
View Answer

Answer: a
Explanation: 9 is not equal to 10 hence it will evaluate to true.

Output:
True

4. What is the output of the given code?

advertisement
test_1 = 17 > 16
puts(test_1)
test_2 = 21 <= 30
puts(test_2)
test_3 = 9 >= 9
puts(test_3)
test_4 = -11 > 4
puts(test_4)

a) True false true false
b) True True True False
c) False False True True
d) None of the mentioned
View Answer

Answer: b
Explanation: All the expressions which are correct will evluate to true rest all will evaluate to false.

advertisement
Output:
true
true
true
false

5. What is the output of the given code?

a="string"
b="strings"
if(a==b)
    print ("a and b are same")
else 
    print "Not same"
end

a) a and b are same
b) Not same
c) a==b
d) None of the mentioned
View Answer

Answer: b
Explanation: a and b are not similar hence the else condition will get executed.

Output:
Not same

6. Assignment operator is also known as relational operator.
a) True
b) False
View Answer

Answer: a
Explanation: Assignment operator is used for assigning a value.

7. What is the output of the given code?

a=10
b=9
if(a>b)
    print ("a greater than b")
else 
    print "Not greater"
end

a) a greater than b
b) Not greater
c) Syntax error
d) None of the mentioned
View Answer

Answer: a
Explanation: Assigning the value to the variable and checking the condition.

Output:
a greater than b

8. Which of the following are used for comparison?
a) Equal to
b) Not equal to
c) Less than or greater than
d) All of the mentioned
View Answer

Answer: d
Explanation: All of the mentioned above are used for comparisons.

9. What is the output of the given code?

if(a==10 && b=9)
    print "true"
else
    print "false"
end

a) True
b) False
c) Error
d) None of the mentioned
View Answer

Answer: c
Explanation: We are assigning the value to the variable b which is not correct.

Output:
warning: found = in conditional, should be ==

10. What is the output of the given code?

counter=1
if counter<=5
    puts (counter)
    counter=counter+1

a) Syntax error
b) 1 2
c) 1
d) None of the mentioned
View Answer

Answer: a
Explanation: end statement is required after every if statement.

Output: syntax error, unexpected $end, expecting keyword_end

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.