Ruby Questions and Answers Part-10

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

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

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

Answer: a
Explanation: 9 is not equal to 10 hence it will evaluate to 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

Answer: b
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

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

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

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

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

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

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

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) 12
c) 1
d) None of the mentioned

Answer: a
Explanation: end statement is required after every if statement.
Output: syntax error, unexpected $end, expecting keyword_end