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
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
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
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
Explanation: All the expressions which are correct will evluate to true rest all will evaluate to false.
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
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
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
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
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
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
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.
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs
- Check Ruby Programming Books