This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “The Elsif Conditional Statement”.
1. Which of the following is valid conditional statement in Ruby?
a) elseif
b) elsif
c) else if
d) elseiff
View Answer
Explanation: We use elsif conditional statement in Ruby.
2. The elsif conditional statement is written with an expression.
a) True
b) False
View Answer
Explanation: The syntax of elsif conditional statement is elsif with an expression.
3. The elsif statement can add many alternatives to if/else statements.
a) True
b) False
View Answer
Explanation: We can add many conditions between if/else statements using elsif conditional statement.
4. What is the output of the given code?
counter=6 if counter<=5 puts (counter) counter=counter+1 puts (counter) elsif counter>5 puts (counter) counter=2*counter puts(counter) else puts(counter) counter=counter-1 puts(counter) end
a) 5
10
b) 6
12
c) Syntax error
d) None of the mentioned
View Answer
Explanation: The counter value is 6 hence elsif part will get executed.
Output: 6 12
5. What is output of the given code?
counter=-3 if counter<=5 puts (counter) counter=counter+1 puts (counter) elsif counter>5 puts (counter) counter=2*counter puts(counter) end
a) -3
-2
b) -3
-6
c) Syntax error
d) None of the mentioned
View Answer
Explanation: As the counter value is negative the if condition is true hence the if block is executed.
Output: -3 -2
6. What is the output of the given code?
if !true print "False" elsif !true || true print "True" end
a) True
b) False
c) Syntax eroor
d) None of the mentioned
View Answer
Explanation: !true || true evaluates to true, hence elsif block gets executed.
Output: True
7. What is the output of the given code?
variable = false if variable print "false" elsif !variable print "true" end
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: !variable will evaluate to true so elsif block gets executed.
Output: True
8. What is the output of the given code?
x=7 y=9 if x==y print "equal" elsif x>y print "greater" else print "less" end
a) equal
b) greater
c) less
d) none of the mentioned
View Answer
Explanation: As x is less than y, hence else part will evaluate to true.
Output: less
9. What is the output of the given code?
a=true b=false if a && b puts "False" elsif a || b puts "True" else puts "neither true nor false" end
a) false
b) true
c) neither true nor false
d) none of the mentioned
View Answer
Explanation: True or false will always evaluate to true, hence the elsif block is executed.
Output: True
10. What is the output of the given code?
a=10 b=2 if a>b a=a*b puts (a) elsif a<b a=a*a+b puts (a) else a=a-b puts (a) end
a) 20
b) 102
c) -98
d) None of the mentioned
View Answer
Explanation: As a>b and this condition is satisfied by if block hence the if block will execute and evaluate to 20.
Output: 20
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.
- Check Ruby Programming Books
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs