Control Structures In Ruby - Study Mode
[#1] The following syntax is used for the ruby case statement. case expression
when expression , expression ... then
code ...
else
code
end
Correct Answer
(A) True
[#2] 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
Correct Answer
(D) syntax error
[#3] 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
Correct Answer
(C) 1 baby 2 baby 3 little child 4 little child 5 little child
[#4] 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
Correct Answer
(B) b alphabet b
[#5] 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.
Correct Answer
(A) True