Ruby Programming Questions and Answers – The Else Conditional Statement

«
»

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

Answer: c
Explanation: When the if expression gives false value and the else condition is true then only it will get executed.
advertisement

2. Is the following syntax correct?

if conditional 
   code...
   elsif conditional 
   code..
   else
   code
   end

a) True
b) False
View Answer

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

Answer: c
Explanation: End statement is always must after the completion of else statement.

advertisement
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

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

Answer: b
Explanation: The condition is satisfied hence the if block code is executed.

advertisement
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

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

Answer: a
Explanation: Condition is not satisfied so else part is executed.

advertisement
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

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

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

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

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

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

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn