This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “The Else Conditional Statement”.
1. What is the use of else statement?
a) When the if condition is false then the next else condition will get executed
b) When the if condition is false then the elsif condition will get executed
c) When the if condition is false and if else condition is true then only it will get executed
d) None of the mentioned
View Answer
Explanation: When the if expression gives false value and the else condition is true then only it will get executed.
2. Is the following syntax correct?
if conditional code... elsif conditional code.. else code end
a) True
b) False
View Answer
Explanation: All the three statements execute only when it’s true.
3. What is the output of the given code?
if 1>2 puts "false" else puts "True"
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: End statement is always must after the completion of else statement.
Output: Syntax error
4. What is the output of the code?
if 1>2 puts "false" end else puts "True" end
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: No two end statements required after if and else just one end statement is sufficient.
Output: Syntax error
5. What is the output of the code?
variable=true if variable puts "true" else puts "false" end
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: The condition is satisfied hence the if block code is executed.
Output: True
6. What is the output of the code?
variable="true".reverse if variable puts "true" else puts "false" end
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: Condition is satisfied and value is not changed.
Output: True
7. What is the output of the code?
variable=true if !variable puts "true" else puts "false" end
a) False
b) True
c) Syntax error
d) None of the mentioned
View Answer
Explanation: Condition is not satisfied so else part is executed.
Output: False
8. What is the output of the code?
variable="true".length if variable puts "true" else puts "false" end
a) False
b) True
c) Syntax error
d) 4
View Answer
Explanation: Condition is satisfied, and it is not effected by length method.
Output: True
9. Which of the following is valid conditional statement?
a) else
b) els
c) if else
d) None of the mentioned
View Answer
Explanation: Else is valid conditional statement.
10. What is the output of given code?
counter=1 if counter<=5 puts (counter) counter=counter+1 else puts(counter) counter=counter-1 end
a) 1, 2
b) 1,2,3,4,5
c) 1 2 1
d) 1
2
View Answer
Explanation: Value of counter is printed and then incremented.
Output: 1 2
11. What is the output of given code?
#counter=1 if counter<=5 puts (counter) counter=counter+1 else puts(counter) counter=counter-1 end
a) Undefined local variable counter
b) 1,2,3,4,5
c) 1 2 1
d) 1
2
View Answer
Explanation: Counter value must not be commented.
Output: Undefined local variable counter
12. It is necessary that always if should come with else block?
a) True
b) False
View Answer
Explanation: Not necessary, if can execute alone.
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
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books