61. The elsif statement can add many alternatives to if/else statements.
62. 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
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
63. 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
counter=-3
if counter<=5
puts (counter)
counter=counter+1
puts (counter)
elsif counter>5
puts (counter)
counter=2*counter
puts(counter)
end
64. What is the output of the given code?
if !true
print "False"
elsif !true || true
print "True"
end
if !true
print "False"
elsif !true || true
print "True"
end
65. What is the output of the given code?
variable = false
if variable
print "false"
elsif !variable
print "true"
end
variable = false
if variable
print "false"
elsif !variable
print "true"
end
66. 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
x=7
y=9
if x==y
print "equal"
elsif x>y
print "greater"
else
print "less"
end
67. 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
a=true
b=false
if a && b
puts "False"
elsif a || b
puts "True"
else
puts "neither true nor false"
end
68. 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
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
69. Syntax for unless conditional statement is
unless conditional [then]
code
else
code
end
unless conditional [then]
code
else
code
end
70. 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
x=3
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
end
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.