Ruby MCQ On Loops
This section focuses on "Loops" in Ruby. These Multiple Choice Questions (MCQ) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. Which of the following is not a type of loop in ruby?
A. For Loop
B. Foreach Loop
C. Until Loop
D. While Loop
View Answer
Ans : B
Explanation: Foreach loop is not there in ruby.
2. What is true about Until loop?
A. Executes code while conditional is true
B. In until loop increment is not required
C. Executes code while conditional is false
D. None of the above
View Answer
Ans : C
Explanation: Executes code while conditional is false. An until statement`s conditional is separated from code by the reserved word do, a newline, or a semicolon.
3. What is true about while loop?
A. Executes code while conditional is true
B. In while loop increment is not required
C. Executes code while conditional is false
D. None of the above
View Answer
Ans : A
Explanation: Executes code while conditional is true. A while loop`s conditional is separated from code by the reserved word do, a newline, backslash , or a semicolon.
4. What is the use of break statement?
A. Terminates the most External loop.
B. Terminates all loop.
C. Terminates the program.
D. Terminates the most internal loop.
View Answer
Ans : D
Explanation: Terminates the most internal loop. Terminates a method with an associated block if called within the block (with the method returning nil).
5. Which statement is used to Jumps to the next iteration of the most internal loop?
A. break
B. next
C. redo
D. retry
View Answer
Ans : B
Explanation: Next : Jumps to the next iteration of the most internal loop. Terminates execution of a block if called within a block
6. Which statement is used to Restarts this iteration of the most internal loop, without checking loop condition?
A. break
B. next
C. redo
D. retry
View Answer
Ans : C
Explanation: Redo : Restarts this iteration of the most internal loop, without checking loop condition. Restarts yield or call if called within a block.
7. Which statement is used to restarts the invocation of the iterator call?
A. break
B. next
C. redo
D. retry
View Answer
Ans : D
Explanation: If retry appears in the iterator, the block, or the body of the for expression, restarts the invocation of the iterator call. Arguments to the iterator is re-evaluated.
8. What does the 1..5 indicate?
A. Exclusive range
B. Both inclusive and exclusive range
C. Inclusive range
D. None of the above
View Answer
Ans : C
Explanation: 1..5 means start from one and go till 4 and even include 5.
9. What does end represent?
A. keyword represents the ending of loop
B. keyword represents the start of loop
C. keyword represents the start and ending of loop
D. keyword represents the infinite loop
View Answer
Ans : A
Explanation: end: This keyword represents the ending of 'for' loop block which started from 'do' keyword.
10. Which of the following is Exit Controlled loop?
A. do..while
B. For
C. Until
D. While
View Answer
Ans : A
Explanation: do..while is Exit-Controlled loop because it tests the condition which presents at the end of the loop body.
11. What will be the output of the given code?
for num in 1...5
puts num
end
A. 1 2 3 4
B. 1 2 3 4 5
C. 0 1 2 3 4
D. 0 1 2 3 4 5
View Answer
Ans : A
Explanation: As the range i.e 1...5 is exclusive the loop will loop till 4 and then it will come out of the loop.
12. What will be the output of the given code?
for i in 1...3
for j in 1..3
puts j
end
end
A. 1 2 1 2
B. 0 1 2 0 1 2
C. 1 2 3 1 2 3
D. 1 2 3 1 2
View Answer
Ans : C
Explanation: The program will print 1 2 3 two times.
13. What will be the output of the given code?
for num in 1...4
puts num*num
end
A. 0 1 4 9
B. 1 4 9 16
C. 0 1 2 3
D. 1 2 3
View Answer
Ans : B
Explanation: The program will print 1 4 9 16.
14. What will be the output of the given code?
i=1
for i in 6..10
puts i**2
end
A. 36 49 64 81
B. 49 64 81 100
C. 49 64 81
D. 36 49 64 81 100
View Answer
Ans : D
Explanation: The output for the following code is 36 49 64 81 100.
15. What will be the output of the given code?
for i in 1..5 && j in 5..10
puts i+j
end
A. 6 8 10 12 14 16
B. Syntax Error
C. 6 8 10 12 14
D. Type Error
View Answer
Ans : B
Explanation: syntax error, unexpected keyword_in, expecting keyword_do_cond.
16. What will be the output of the given code?
counter = 1
while counter < 5
puts counter
counter = counter + 1
end
A. Prints the number from 1 to 4
B. Prints the number from 1 to 5
C. Prints the number from 2 to 5
D. Prints the number from 2 to 4
View Answer
Ans : A
Explanation: Counter value is initialized to 1 and it keeps on looping till the counter is less than 5.
17. What will be the output of the given code?
counter = true
while counter !=false
puts counter
end
A. TRUE
B. FALSE
C. Syntax Error
D. Infinite Loop
View Answer
Ans : D
Explanation: Counter value is true in all cases hence true will be printed infinite times.
18. What will be the output of the given code?
while a&&b
puts a
end
A. TRUE
B. FALSE
C. Syntax Error
D. Name Error
View Answer
Ans : D
Explanation: undefined local variable or method
19. What will be the output of the given code?
a=1
b=1
while a&&b
puts a+b
end
A. 2
B. 2 2 2 2 2
C. Infinite Loop
D. Syntax Error
View Answer
Ans : C
Explanation: Infinite loop is the correct answer.
20. What will be the output of the given code?
i = 4
while i > 0 do
print i
i -= 1
end
A. 321
B. 3210
C. 4321
D. 43210
View Answer
Ans : C
Explanation: The do statement here indicates that till the while condition is true execute the instructions.
Discussion