Ruby Programming Questions and Answers – The Case Conditional Statement

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “The Case Conditional Statement”.

1. What is the output of the given code?

age =  5
case age
when 0 .. 2
    puts "baby"
when 3 .. 6
    puts "little child"
when 7 .. 12
    puts "child"
when 13 .. 18
    puts "youth"
else
    puts "adult"
end

a) baby
b) adult
c) little child
d) youth
View Answer

Answer: c
Explanation: The age is already assigned the value 5 and so the case statement will check in which range does 5 lies and execute it.

advertisement
advertisement
Output:
little child

2. The following syntax is used for the ruby case statement.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
case expression
when expression , expression ... then
   code ...
else
   code 
end

a) True
b) False
View Answer

Answer: a
Explanation: The following syntax is used for ruby case statement.
advertisement

3. What is the output of the given code?

age =  4
case age
puts "baby" when 0 .. 2
puts "little child" when 3 .. 6
puts "child" when 7 .. 12
puts "youth" when 13 .. 18
puts "adult" else
end

a) adult
b) youth
c) child
d) syntax error
View Answer

Answer: d
Explanation: The following code will give the error because the syntax is not similar to case statements syntax.

advertisement
Output:
syntax error, unexpected tIDENTIFIER, expecting keyword_when
puts "baby" when 0 .. 2
 syntax error, unexpected keyword_when, expecting $end
puts "baby" when 0 .. 2

4. What is the output of the given code?

for counter in 1..5
case counter
when 0 .. 2
    puts counter
    puts "baby"
when 3 .. 6
    puts counter
    puts "little child"
when 7 .. 12
    puts counter
    puts "child"
else
    puts counter
    puts "adult"
end
end

a) adult 1..5
b) 3..6 littlechild
c) 1
baby
2
baby
3
little child
4
little child
5
little child
d) adult
View Answer

Answer:c
Explanation: The for loop will keep on incrementing from 1 to 5 and then prints the value in particular case statements.

Output:
1
baby
2
baby
3
little child
4
little child
5
little child

5. What is the output of the given code?

string = gets.chomp
case string
when string = "a"
print "alphabet a"
when string = "b"
print "alphabet b"
when string = "c"
print "alphabet c"
else 
    print "some other alphabet"
end

a) alphabet a
b) b
alphabet b
c) alphabet c
d) Syntax error
View Answer

Answer: b
Explanation: After taking input from the user will check which string is entered and goes to particular case statement.

Output:
b
alphabet b

6. The expression specified by the when clause is evaluated as the left operand. If no when clauses match, case executes the code of the else clause.
a) True
b) False
View Answer

Answer: a
Explanation: Else clause is executed only when no ‘when’ clauses match.

7. What is the output of the given code?

length=gets.chomp
case length.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end

a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error
View Answer

Answer: a
Explanation: Input string length is taken into consideration and then the case statement is executed.

Output:
ruby
length is 4

8. What is the output of the given code?

length=gets.chomp
case length.reverse.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end

a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error
View Answer

Answer: a
Explanation: The reverse method does not effect the length of the string.

Output:
ruby
length is 4

9. What is the output of the given code?

l=9
case l
print "ruby" when l==9
print "language" when l==10
end

a) l==9
b) l==10
c) Syntax error
d) Ruby
View Answer

Answer: c
Explanation: Syntax of case statement is not correct.

Output:
syntax error, unexpected tIDENTIFIER, expecting keyword_when
print "ruby" when l==9
 syntax error, unexpected keyword_when, expecting $end
print "ruby" when l==9

10. A case statement compares the expression specified by case and that specified by when using the === operator and executes the code of the when clause that matches.
a) True
b) False
View Answer

Answer: a
Explanation: A case statement is basically the multiple if-elsif-else statements.

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.

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.