Ruby Programming Questions and Answers – Until Loop

This set of Ruby Programming Questions and Answers for Experienced people focuses on “Until Loop”.

1. The complement of while loop is until loop.
a) True
b) False
View Answer

Answer: a
Explanation: The until loop is sort of like backward while.

2. What is the output of the given code?

counter = 1
until counter > 10
  puts counter
counter+=1 
  end

a) 1 2 3 4 5 6 7 8 9 10
b) 11 12 13 14 … infinite loop
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
View Answer

Answer: a
Explanation: Until the condition is false continue looping.

advertisement
advertisement
Output:
1
2
3
4
5
6
7
8
9
10

3. What is the output of the given code?

Note: Join free Sanfoundry classes at Telegram or Youtube
counter = 0
until counter >= 10
  puts counter
counter+=1 
  end

a) 1 2 3 4 5 6 7 8 9 10
b) 11 12 13 14 … infinite loop
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
View Answer

Answer: c
Explanation: Until the condition is false continue looping and here 10 will not be included because condition becomes true when counter is 10.

advertisement
Output:
0
1
2
3
4
5
6
7
8
9

4. What is the output of the given code?

advertisement
i = 3
while i > 0 do
  puts i
  i -= 1
end
 
j = 3
until j == 0 do
  puts j
  j -= 1
end

a) 1 2 3 1 2 3
b) 3 2 1 3 2 1
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
View Answer

Answer: c
Explanation: When it comes out of the while loop the until loop starts executing.

Output:
3
2
1
3
2
1

5. What is the output of the given code?

a="hungry"
until !a
puts "hungry"
a=!a
end

a) hungry
b) Nil
c) Error
d) None of the mentioned
View Answer

Answer: a
Explanation: Until the condition is not met execute the loop.

Output:
hungry

6. What is the output of the given code?

m= 8
loop do
    m += 2
    puts m
    break if m == 16
end

a) 10 12 14 16
b) Nil
c) Error
d) None of the mentioned
View Answer

Answer: a
Explanation: Execute the loop till the condition is met.

Output:
10
12
14
16

7. What is the output of the given code?

m=0
loop do
    print "ruby"
    m+=1
    break if m==5
end

a) rubyrubyrubyrubyrubyruby
b) rubyrubyrubyrubyruby
c) Error
d) None of the mentioned
View Answer

Answer: b
Explanation: Execute the loop till m!=5 and then break when m=5.

Output:
rubyrubyrubyrubyruby

8. What is the output of the given code?

m=0
loop do
    puts m*10
    m+=1
    break if m==5
end

a) 0 10 20 30 40
b) 10 20 30 40 50
c) Error
d) None of the mentioned
View Answer

Answer: a
Explanation: Execute the loop till m!=5 and then print m*10.

Output:
0
10
20
30
40

9. What is the output of the given code?

m=0
loop do
    puts 101
    m+=1
    break if m==5
end

a) 101 101 101 101 101
b) 10 20 30 40 50
c) Error
d) None of the mentioned
View Answer

Answer: a
Explanation: Execute the loop till m!=5 and then print 101 five times.

Output:
101
101
101
101
101

10. What is the output of the given code?

m=5
loop do
    m-=1
    break if m==0
end

a) 1 2 3 4 5
b) 10 20 30 40 50
c) Error
d) Nil
View Answer

Answer: d
Explanation: There is no print statement so the loop will execute 5 times and will not print anything.

Output:
nil

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming for Experienced people, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.