Ruby Questions and Answers Part-12

1. What will the following expression evaluate to?
true && false
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: Boolean operator && evaluates to true only when both the values are true.

2. What will be the output of the given code?
boolean_1 = 77 < 78 && 77 < 77
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: 77< 78 is true but 77< 77 is false hence the overall expression will evaluate to false.
Output:
False

3. What will the following expression evaluate to?
true || false
a) True
b) False
c) Error
d) None of the mentioned

Answer: a
Explanation: Boolean operator || evaluates to true only when both the values are true or one is true and other is false.

4. What will the following expression evaluate to?
(true && false) || (!true)
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: Boolean operator && evaluates to true only when both the values are true and !true means false hence the whole expression will evaluate to false.

5. What will the following expression evaluate to?
!true && !false
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: Boolean operator && evaluates to true only when both the values are true.

6. What will be the output of the given code?
boolean_1 = (3 < 4 || false) && (false || true)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: a
Explanation: 3< 4 is true and true or false will evaluate to true hence the overall expression will evaluate to true.
Output:
True

7. What will be the output of the given code?
boolean_1 = !(3 < 4 || false) && (false || true)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: 3< 4 is true and true or false will evaluate to true hence the overall expression will evaluate to true but the ! used will make the whole expression evaluate to false.
Output:
False

8. What will be the output of the given code?
boolean_1 = 2**3 != 3**2 || true
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: 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.
Output:
True

9. What will be the output of the given code?
boolean_1 = false || -10 > -9
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: a
Explanation: 10 is not greater than -9 hence the output will be false || false which is false.
Output:
False

10. What will be the output of the given code?
boolean_1 = !(700 / 10 == 70)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: 700/10=70 which is true but the ! before 700/10==70 will evaluate to false hence the overall expression will evaluate to false.
Output:
False