In Ruby, control flow is a powerful tool that helps in executing code based on conditions, making decisions with if-else, case statements, and performing iterations with loops like while, for, and each.
Conditional Statements (Questions 1-10)
In Ruby, which of the following is used to check multiple conditions with different results? a) if b) else c) elsif d) unless
Which of the following will execute the code inside the block only if the condition is false? a) else b) if c) unless d) elsif
Which of the following Ruby keywords is used to execute code if a condition is true? a) unless b) elsif c) if d) else
What is the correct syntax for checking multiple conditions in Ruby? a) if condition1 then condition2 b) if condition1 else condition2 c) if condition1 elsif condition2 else condition3 d) elsif condition1 condition2
Which of the following statements will execute when none of the previous conditions are true? a) elsif b) else c) unless d) if
What will the following Ruby code print? x = 10 if x > 5 puts "Greater" else puts "Lesser" end a) Greater b) Lesser c) nil d) Error
Which Ruby statement is used when you want to check a condition that must be false for code to run? a) if b) unless c) elsif d) else
In Ruby, which of the following keywords is used to check an additional condition after if and else? a) elsif b) unless c) then d) else
Which of the following Ruby control flow statements allows checking whether a condition is false? a) else b) if c) unless d) elsif
Which of these is a valid Ruby conditional structure? a) if condition b) unless condition c) elsif condition d) All of the above
Case Statements (Questions 11-16)
Which Ruby statement is used to handle multiple conditions based on a variable’s value? a) if b) else c) case d) elsif
How do you define the start of a case block in Ruby? a) case b) when c) else d) if
In Ruby, what does the when keyword do inside a case statement? a) Starts a new block of code b) Marks a condition to check for c) Exits the case block d) Continues the loop
How do you exit a case statement in Ruby? a) exit b) end c) break d) return
Which Ruby keyword can be used at the end of a case statement to handle all unmatched conditions? a) else b) default c) nil d) rescue
Given the following code, what will the output be? x = 2 case x when 1 puts "One" when 2 puts "Two" else puts "Other" end a) One b) Two c) Other d) nil
Loops (Questions 17-30)
Which of the following is the correct syntax for a while loop in Ruby? a) while condition do b) while (condition) c) do while condition d) while (condition)
What does a while loop in Ruby do? a) Repeats until the condition becomes true b) Repeats until the condition becomes false c) Executes code only once d) Exits the loop when the condition is true
How would you write a while loop that runs indefinitely in Ruby? a) while true b) while 1 c) while nil d) while false
What will the following Ruby code output? i = 0 while i < 3 puts i i += 1 end a) 0 1 2 b) 1 2 3 c) 0 1 2 3 d) nil
What is the until loop in Ruby used for? a) Executes the block if the condition is true b) Executes the block until the condition becomes true c) Executes the block if the condition is false d) Loops until a certain condition is false
Which of the following is used to define a for loop in Ruby? a) for variable in array b) for array do c) for each item in array d) for array.each do
What will the following Ruby code output? for i in 1..3 puts i end a) 1 2 3 b) 1 2 c) 2 3 4 d) nil
What is the correct syntax for the each method in Ruby? a) array.each do |x| b) array.each |x| do c) do |x| array.each d) array.each |x| end
What does the each method in Ruby do? a) Iterates through an array and executes code for each element b) Executes code only once c) Stops the loop after one iteration d) Modifies the array elements
What will be the output of the following code? [1, 2, 3].each { |num| puts num * 2 } a) 1 2 3 b) 2 4 6 c) 1 4 9 d) nil
In a while loop, how do you stop the loop before the condition is false? a) break b) continue c) exit d) return
Which Ruby keyword can be used to skip the current iteration in a loop? a) exit b) skip c) next d) return
What is the result of running the following Ruby code? i = 0 until i == 3 puts i i += 1 end a) 0 1 2 b) 1 2 3 c) 0 1 2 3 d) nil
Which of the following is a benefit of using the each method in Ruby? a) It supports only arrays b) It automatically handles iteration and block execution c) It cannot be used for hashes d) It limits iteration to a specific range
Answer Key
QNo
Answer (Option with Text)
1
c) elsif
2
c) unless
3
c) if
4
c) if condition1 elsif condition2 else condition3
5
b) else
6
a) Greater
7
b) unless
8
a) elsif
9
c) unless
10
d) All of the above
11
c) case
12
a) case
13
b) Marks a condition to check for
14
b) end
15
a) else
16
b) Two
17
a) while condition do
18
b) Repeats until the condition becomes false
19
a) while true
20
a) 0 1 2
21
b) Executes the block until the condition becomes true
22
a) for variable in array
23
a) 1 2 3
24
a) `array.each do
25
a) Iterates through an array and executes code for each element
26
b) 2 4 6
27
a) break
28
c) next
29
a) 0 1 2
30
b) It automatically handles iteration and block execution