Select an option to see the answer and solution.
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.
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)
endSelect an option to see the answer and solution.
counter=-3
if counter<=5
puts (counter)
counter=counter+1
puts (counter)
elsif counter>5
puts (counter)
counter=2*counter
puts(counter)
endSelect an option to see the answer and solution.
if !true
print "False"
elsif !true || true
print "True"
endSelect an option to see the answer and solution.
variable = false
if variable
print "false"
elsif !variable
print "true"
endSelect an option to see the answer and solution.
x=7
y=9
if x==y
print "equal"
elsif x>y
print "greater"
else
print "less"
endSelect an option to see the answer and solution.
a=true
b=false
if a && b
puts "False"
elsif a || b
puts "True"
else
puts "neither true nor false"
endSelect an option to see the answer and solution.
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)
endSelect an option to see the answer and solution.
unless conditional [then]
code
else
code
endSelect an option to see the answer and solution.
x=3
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
endSelect an option to see the answer and solution.
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 varSelect an option to see the answer and solution.
hungry=false
unless hungry
print "Not hungry"
else
print "Hungry"
endSelect an option to see the answer and solution.
code unless conditionalSelect an option to see the answer and solution.
counter=12
unless counter
print counter+1
else
print counter+2
endSelect an option to see the answer and solution.
unless true && false
print "false"
else
print "ruby"
endSelect an option to see the answer and solution.
print "2 is less than 3" unless 2>3Select an option to see the answer and solution.
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"
endSelect an option to see the answer and solution.
x="ruby".length
y="language".length
puts x,y
unless x>y
print "length of x is less than that of y"
endSelect an option to see the answer and solution.
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"
endSelect an option to see the answer and solution.
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"
endSelect an option to see the answer and solution.