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

4/6

Page

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

The elsif statement can add many alternatives to if/else statements.

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is output of the given code?
counter=-3
   if counter<=5
   puts (counter)
   counter=counter+1
   puts (counter)
   elsif counter>5
   puts (counter)
   counter=2*counter
   puts(counter)
   end

Select an option to see the answer and solution.

What is the output of the given code?
 if !true
   print "False"
   elsif !true || true
   print "True"
   end

Select an option to see the answer and solution.

What is the output of the given code?
variable = false
   if variable
   print "false"
   elsif !variable
   print "true" 
   end

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Syntax for unless conditional statement is
unless conditional [then]
   code
else
   code 
end

Select an option to see the answer and solution.

What is the output of the given code?
x=3
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
end

Select an option to see the answer and solution.

What is the output of the given code?
var =  1
print "1 -- Value is set\n" if var
print "2 -- Value is set\n" unless var
var = false
print "3 -- Value is set\n" unless var

Select an option to see the answer and solution.

What is the output of the given code?
hungry=false
unless hungry
 print "Not hungry"
else
 print "Hungry"
end

Select an option to see the answer and solution.

The following syntax is also used for unless conditional statement.
code unless conditional

Select an option to see the answer and solution.

What is the output of the given code?
counter=12
unless counter
 print counter+1
else
 print counter+2
end

Select an option to see the answer and solution.

What is the output of the given code?
unless true && false
print "false"
else 
    print "ruby"
end

Select an option to see the answer and solution.

What is the output of the given code?
print "2 is less than 3" unless 2>3

Select an option to see the answer and solution.

What is the output of the given code?
x=8
y=10
unless x>y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end

Select an option to see the answer and solution.

What is the output of the given code?
x="ruby".length
y="language".length
puts x,y
unless x>y 
print "length of x is less than that of y"
end

Select an option to see the answer and solution.

What is the output of the given code?
x=8
y=10
unless x<y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.