81. The following syntax is used for the ruby case statement.
case expression
when expression , expression ... then
code ...
else
code
end
case expression
when expression , expression ... then
code ...
else
code
end
82. 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
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
83. 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
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
84. 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
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
85. 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.
86. 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
length=gets.chomp
case length.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
87. 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
length=gets.chomp
case length.reverse.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
88. What is the output of the given code?
l=9
case l
print "ruby" when l==9
print "language" when l==10
end
l=9
case l
print "ruby" when l==9
print "language" when l==10
end
89. 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.
90. While loop checks the condition and the loop keeps on running till the condition is true, it stops when the condition becomes false.
Read More Section(Control Structures in Ruby)
Each Section contains maximum 100 MCQs question on Control Structures in Ruby. To get more questions visit other sections.