MCQs on Control Flow | Ruby

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)

  1. In Ruby, which of the following is used to check multiple conditions with different results?
    a) if
    b) else
    c) elsif
    d) unless
  2. 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
  3. Which of the following Ruby keywords is used to execute code if a condition is true?
    a) unless
    b) elsif
    c) if
    d) else
  4. 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
  5. Which of the following statements will execute when none of the previous conditions are true?
    a) elsif
    b) else
    c) unless
    d) if
  6. 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
  7. 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
  8. 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
  9. Which of the following Ruby control flow statements allows checking whether a condition is false?
    a) else
    b) if
    c) unless
    d) elsif
  10. 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)

  1. Which Ruby statement is used to handle multiple conditions based on a variable’s value?
    a) if
    b) else
    c) case
    d) elsif
  2. How do you define the start of a case block in Ruby?
    a) case
    b) when
    c) else
    d) if
  3. 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
  4. How do you exit a case statement in Ruby?
    a) exit
    b) end
    c) break
    d) return
  5. 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
  6. 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)

  1. 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)
  2. 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
  3. How would you write a while loop that runs indefinitely in Ruby?
    a) while true
    b) while 1
    c) while nil
    d) while false
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. In a while loop, how do you stop the loop before the condition is false?
    a) break
    b) continue
    c) exit
    d) return
  12. Which Ruby keyword can be used to skip the current iteration in a loop?
    a) exit
    b) skip
    c) next
    d) return
  13. 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
  14. 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

QNoAnswer (Option with Text)
1c) elsif
2c) unless
3c) if
4c) if condition1 elsif condition2 else condition3
5b) else
6a) Greater
7b) unless
8a) elsif
9c) unless
10d) All of the above
11c) case
12a) case
13b) Marks a condition to check for
14b) end
15a) else
16b) Two
17a) while condition do
18b) Repeats until the condition becomes false
19a) while true
20a) 0 1 2
21b) Executes the block until the condition becomes true
22a) for variable in array
23a) 1 2 3
24a) `array.each do
25a) Iterates through an array and executes code for each element
26b) 2 4 6
27a) break
28c) next
29a) 0 1 2
30b) It automatically handles iteration and block execution

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top