Ruby MCQ On Operators
This section focuses on "Operators" in Ruby. These Multiple Choice Questions (mcq) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. Which of the following is not type of operator in ruby?
A. Arithmetic Operators
B. Comparison Operators
C. Similar operator
D. Parallel Assignment
View Answer
Ans : C
Explanation: Similar operator is not type of operator in ruby.
2. Which operator is used to find exponent in ruby?
A. *
B. **
C. ^
D. %
View Answer
Ans : B
Explanation: ** is used find exponent in ruby.
3. eql? Operator is used for?
A. Used to test equality within a when clause of a case statement.
B. if the receiver and argument have both the same type and equal values.
C. if the receiver and argument have the same object id.
D. All of the above
View Answer
Ans : B
Explanation: eql? Return True if the receiver and argument have both the same type and equal values.
4. equal? Operator is used for?
A. Used to test equality within a when clause of a case statement.
B. if the receiver and argument have both the same type and equal values.
C. if the receiver and argument have the same object id.
D. All of the above
View Answer
Ans : C
Explanation: equal? return True if the receiver and argument have the same object id.
5. Which of the following is not a type of Bitwise Operators?
A. <<
B. ^
C. ~
D. None of the above
View Answer
Ans : D
Explanation: All the above operator comes under Bitwise Operator.
6. Guess the operator : If Condition is true ? Then value X : Otherwise value Y?
A. Range Operators
B. Ternary Operator
C. Parallel Operator
D. Arithmetic operator
View Answer
Ans : B
Explanation: first evaluates an expression for a true or false value and then Ternary Operator executes one of the two given statements depending upon the result of the evaluation
7. In range operator ... is used for ?
A. Creates a range from start point to end point inclusive.
B. Creates a range from start point to end point exclusive.
C. Creates a range from start point inclusive to end point exclusive.
D. Creates a range from start point exclusive to end point inclusive.
View Answer
Ans : B
Explanation: ... used for Creates a range from start point to end point exclusive.
8. Which operator is used to check variable and method are defined or not?
A. define
B. define?
C. defined?
D. defined
View Answer
Ans : C
Explanation: defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined.
9. Among the following which operator has highest Precedence?
A. ::
B. .
C. []
D. **
View Answer
Ans : A
Explanation: :: has highest Precedence in ruby.
10. Among the following which operator has lowest Precedence?
A. .
B. ^
C. ...
D. >>
View Answer
Ans : C
Explanation: ... has lowest precedence.
11. What will be the output of the given code?
boolean_var = 15 < 16 && 15 < 15
puts boolean_var
A. TRUE
B. FALSE
C. Type Error
D. Syntax Error
View Answer
Ans : B
Explanation: 15<16 is true but 15<15 is false hence the overall expression will evaluate to false.
12. What will be the output of the given code?
num=4<<2
puts num
A. 4
B. 8
C. 16
D. 32
View Answer
Ans : C
Explanation: left operands value is moved left by the number of bits specified by the right operand. So after shifting 0011 by 2 times the output will be 16.
13. What will be the output of the given code?
num=4>>2
puts num
A. -2
B. 0
C. 2
D. 1
View Answer
Ans : D
Explanation: left operands value is moved right by the number of bits specified by the right operand.So the answer is 1.
14. What will be the output of the given code?
num=(10<11)||(11===11)? (11===11.0): 0
puts num
A. TRUE
B. FALSE
C. 1
D. 0
View Answer
Ans : B
Explanation: First check 10<11 it return true then no need to check 11===11 , condition becomes true now go to next statement 11===11.0 and it will return true.
15. What will be the output of the given code?
boolean_var = 3**2 != 2**3 || true
puts boolean_var
A. TRUE
B. FALSE
C. 1
D. 0
View Answer
Ans : A
Explanation: 2**3=8 and 3**2=9 both the values are not equal is true and true or true will evaluate to true hence the overall expression will evaluate to true.
16. What will be the output of the given code?
boolean_var = false || -20 > -18
puts boolean_var
A. TRUE
B. FALSE
C. Type Error
D. Syntax Error
View Answer
Ans : B
Explanation: -20 is not greater than -18 hence the output will be false || false which is false.
17. What will be the output of the given code?
boolean_var = !(100 / 10 === 10)
puts boolean_var
A. TRUE
B. FALSE
C. Type Error
D. Syntax Error
View Answer
Ans : B
Explanation: 100/10=10 which is true but the ! before 100/10==10 will evaluate to false hence the overall expression will evaluate to false.
18. What will be the output of the given code?
boolean_var = !true || (true || 36 != 6**2)
puts boolean_var
A. TRUE
B. FALSE
C. Error
D. None of the above
View Answer
Ans : A
Explanation: 36 != 6**2 is false and true || false is true but true || !true is true hence the overall expression will evaluate to true.
19. What will the following expression evaluate to?
true || false
A. TRUE
B. FALSE
C. Syntax Error
D. None of the above
View Answer
Ans : A
Explanation: Boolean operator || evaluates to false only when both the values are false.
20. What will the following expression evaluate to?
!true && !false
A. TRUE
B. FALSE
C. Syntax Error
D. None of the above
View Answer
Ans : B
Explanation: Boolean operator && evaluates to true only when both the values are true.
Discussion