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.
advertisement

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.

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

3. What is the output of the given code?

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.

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?

advertisement
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.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn