Control Structures In Ruby
Name: _____________________
Date: _____________________
Instructions: Answer all questions. Write your answers clearly in the space provided.
The following syntax is used for the ruby case statement. case expression
when expression , expression ... then
code ...
else
code
end
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
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
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
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.
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
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
What is the output of the given code? l=9
case l
print "ruby" when l==9
print "language" when l==10
end
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.
While loop checks the condition and the loop keeps on running till the condition is true, it stops when the condition becomes false.
What is the output of the given code? if 11>2
puts "Eleven is greater than two"
end
print "You'r right"
What is the output of the given code? if 79>78
puts "True".upcase
if 9>8
puts "True".reverse
if 7==7
puts "equal".downcase
end
end
end
What is the output of the given code? if 79>78
puts "True".upcase
if 9>8
puts "True".Upcase
if 7==7
puts "equal".downcase
end
end
end
If statement inside if statement is called Nested if statements.
What is the output of the given code? x=1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x!=0
puts "x is 1"
else
puts "I can't guess the number"
end
It's a good habit to give two spaces between if statement and condition.
What is the use of else statement?
Is the following syntax correct? if conditional
code...
elsif conditional
code..
else
code
end
What is the output of the given code? if 1>2
puts "false"
else
puts "True"
What is the output of the code? if 1>2
puts "false"
end
else
puts "True"
end
Which of the following is used to create a range in Ruby?
What is the output of the following code snippet? for i in 1..5
print i, " "
end
In Ruby, what does the `break` keyword do when used within nested loops?
Which of the following is NOT a valid way to exit a loop in Ruby?
In Ruby, what does the `until` keyword do?
What does the `retry` keyword do when used within a loop in Ruby?
In Ruby, what does the `case` statement allow you to compare against?
In Ruby, which of the following is NOT a valid loop construct?
What is the purpose of the loop keyword in Ruby?
Which of the following is used to define a block of code that executes repeatedly until a condition is met in Ruby?
In Ruby, what is the purpose of the `redo` keyword?
Which keyword in Ruby is used to exit a loop prematurely?
In Ruby, which of the following is used to handle multiple conditions with one expression?
In Ruby, what does the `else` keyword do?
In Ruby, what does the `elsif` keyword do?
The following syntax is correct for if conditional statement. if condition
code
end
If expression. The expression can be of which type?
What error does the if condition gives if not terminated with end statement?
What is the output of the following? if 1<2
print "one is less than two"
end
What is the output of the code? if 11<2
print "Eleven is less than two"
end
print "11 is greater"
What is the output of the given code? var = 1
print "1 -- Value is set
" if var
print "2 -- Value is set
" unless var
var = false
print "3 -- Value is set
" unless var
What is the output of the given code? hungry=false
unless hungry
print "Not hungry"
else
print "Hungry"
end
The following syntax is also used for unless conditional statement. code unless conditional
What is the output of the given code? counter=12
unless counter
print counter+1
else
print counter+2
end
What is the output of the given code? unless true && false
print "false"
else
print "ruby"
end
What is the output of the given code? print "2 is less than 3" unless 2>3
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
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
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
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
What is the output of the given code? for num in 1...5
puts num
end
What does the 1...10 indicate?
What is the output of the given code? for num in 1..3
puts num
for i in 1..2
puts num*i
end
end
What is the output of the given code? m= 0
loop do
m += 1
print m
break if m == 10
end
What is the output of the given code? for num in 1..5
puts num*num
end
What is the output of the given code? for num in 1..3
puts num*num
m= 0
loop do
m += 1
puts m
break if m == 3
end
end
What is the output of the given code? loop do
m += 1
puts m
break if m == 3
end
What is the output of the given code? i=1
for i in 5..10
puts i^2
end
What does the 1..10 indicate?
What is the output of the given code? i=5
j=10
for i in 5..10 && j in 5..10
puts i**j
end
What is the output of the given code? counter = 1
while counter < 11
puts counter
counter = counter + 1
end
What is the output of the given code? counter = true
while counter !=false
puts counter
end
What is the output of the given code? i = 0
while i < 5
puts i
i=(i+1)**2
end
What is the output of the given code? a=5
b=15
while a&&b
puts a+b
end
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
What is the output of the given code? i = 3
while i > 0 do
print i
i -= 1
end
What is the output of the given code? i = 50
while i > 25 do
print 50/i
i -= 1
end
What is the output of the given code? a = 5
b=10
while a<b do
puts a*b
a+=2
b-=2
end
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
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
The complement of while loop is until loop.
What is the output of the given code? counter = 1
until counter > 10
puts counter
counter+=1
end
What is the output of the given code? counter = 0
until counter >= 10
puts counter
counter+=1
end
What is the output of the given code? i = 3
while i > 0 do
puts i
i -= 1
end
j = 3
until j == 0 do
puts j
j -= 1
end
What is the output of the given code? a="hungry"
until !a
puts "hungry"
a=!a
end
What is the output of the given code? m= 8
loop do
m += 2
puts m
break if m == 16
end
What is the output of the given code? m=0
loop do
print "ruby"
m+=1
break if m==5
end
What is the output of the given code? m=0
loop do
puts m*10
m+=1
break if m==5
end
What is the output of the given code? m=0
loop do
puts 101
m+=1
break if m==5
end
What is the output of the given code? m=5
loop do
m-=1
break if m==0
end
What does the `next` keyword do in a loop in Ruby?
In Ruby, what does the `elsif` keyword allow you to do?
Which of the following is NOT a logical operator in Ruby?
What is the output of the following code snippet? x = 5
while x > 0
puts x
x -= 1
end
What does the `retry` keyword do in Ruby?
Which loop in Ruby allows you to execute code while a condition is true?
What does the unless keyword do in Ruby?
What is the purpose of the next keyword in a loop in Ruby?
What is the output of the following code snippet? x = 1
loop do
puts x
x += 1
break if x > 5
end
In Ruby, what is the purpose of the `case` statement when combined with `when` clauses?
The elsif statement can add many alternatives to if/else statements.
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
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
What is the output of the given code? if !true
print "False"
elsif !true || true
print "True"
end
What is the output of the given code? variable = false
if variable
print "false"
elsif !variable
print "true"
end
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
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
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
Syntax for unless conditional statement is unless conditional [then]
code
else
code
end
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
What is the keyword used to define a conditional statement in Ruby?
Which of the following loops in Ruby is executed at least once?
In Ruby, what does the unless keyword do?
What is the purpose of the case statement in Ruby?
Which loop in Ruby allows you to iterate over a collection of elements?
What is the result of the expression 5 > 3 ? "Yes" : "No" in Ruby?
What does the break keyword do in a loop in Ruby?
Which of the following is NOT a comparison operator in Ruby?
What is the purpose of the redo keyword in Ruby?
What is the output of the following code snippet? x = 10
unless x > 15
puts "Hello"
end
What is the output of the code? variable=true
if variable
puts "true"
else
puts "false"
end
What is the output of the code? variable="true".reverse
if variable
puts "true"
else
puts "false"
end
What is the output of the code? variable=true
if !variable
puts "true"
else
puts "false"
end
What is the output of the code? variable="true".length
if variable
puts "true"
else
puts "false"
end
Which of the following is valid conditional statement?
What is the output of given code? counter=1
if counter<=5
puts (counter)
counter=counter+1
else
puts(counter)
counter=counter-1
end
What is the output of given code? #counter=1
if counter<=5
puts (counter)
counter=counter+1
else
puts(counter)
counter=counter-1
end
It is necessary that always if should come with else block?
Which of the following is valid conditional statement in Ruby?
The elsif conditional statement is written with an expression.