Ruby Programming Questions and Answers – The Elsif Conditional Statement

This set of Ruby Programming Questions and Answers for Freshers 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

Answer: b
Explanation: We use elsif conditional statement in Ruby.

2. The elsif conditional statement is written with an expression.
a) True
b) False
View Answer

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

Answer: a
Explanation: We can add many conditions between if/else statements using elsif conditional statement.
advertisement
advertisement

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

Answer: b
Explanation: The counter value is 6 hence elsif part will get executed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Output:
6
12

5. What is output of the given code?

advertisement
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

Answer: a
Explanation: As the counter value is negative the if condition is true hence the if block is executed.

advertisement
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

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

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

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

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

Answer: a
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 for Freshers, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.