Vidyalelo
Ruby Programming · all questions

Control Structures in Ruby
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

120

Questions

5/6

Page

Pick an option on a question to see the right answer and solution.

The following syntax is used for the ruby case statement.
case expression
when expression , expression ... then
   code ...
else
   code 
end

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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.

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is the output of the given code?
l=9
case l
print "ruby" when l==9
print "language" when l==10
end

Select an option to see the answer and solution.

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.

Select an option to see the answer and solution.

While loop checks the condition and the loop keeps on running till the condition is true, it stops when the condition becomes false.

Select an option to see the answer and solution.

What is the output of the given code?
counter = 1
while counter < 11
  puts counter
  counter = counter + 1
end

Select an option to see the answer and solution.

What is the output of the given code?
counter = true
while counter !=false
  puts counter
end

Select an option to see the answer and solution.

What is the output of the given code?
i = 0
while i < 5
  puts i
   i=(i+1)**2
end

Select an option to see the answer and solution.

What is the output of the given code?
a=5
b=15
while a&&b
puts a+b
end

Select an option to see the answer and solution.

What is the output of the given code?
a=5
b=15
while b>a
puts a*(b-a)
while a>b
  a+=1
  b-=1
end
end

Select an option to see the answer and solution.

What is the output of the given code?
i = 3
while i > 0 do
  print i
  i -= 1
end

Select an option to see the answer and solution.

What is the output of the given code?
i = 50
while i > 25 do
  print 50/i
  i -= 1
end

Select an option to see the answer and solution.

What is the output of the given code?
a = 5
b=10
while a<b do
  puts a*b
  a+=2
  b-=2
  end

Select an option to see the answer and solution.

What is the output of the given code?
i = 50 
j=55
while i > 25 && j>35 do
  puts 50*j/i
  i -= 1
  j-=2
end

Select an option to see the answer and solution.

What is the output of the given code?
i = 50 
j=55
while i > 25 && i*j<100 do
  puts (50*j)/i
  i -= 1
  j-=2
end

Select an option to see the answer and solution.