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.
advertisement

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?

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?

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.

advertisement
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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn