Ruby Programming Questions and Answers – For Loop

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “For Loop”.

1. What is the output of the given code?

for num in 1...5
  puts num
end

a) 1 2 3 4 5
b) 1 2 3 4
c) 2 3 4 5
d) None of the mentioned
View Answer

Answer: b
Explanation: As the range i.e 1…10 is exclusive the loop will loop till 4 and then it will come out of the loop.

advertisement
advertisement
Output:
1
2
3
4

2. What does the 1…10 indicate?
a) Inclusive range
b) Exclusive range
c) Both inclusive and exclusive range
d) None of the mentioned
View Answer

Answer: b
Explanation: 1…10 means start from one and go till 9 and don’t include 10.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the output of the given code?

for num in 1..3
  puts num
  for i in 1..2
  puts num*i
  end
end

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

Answer: b
Explanation: This is a nested loop.

advertisement
Output:
1
1
2
2
2
4
3
3
6

4. What is the output of the given code?

advertisement
m= 0
loop do
    m += 1
    print m
    break if m == 10
end

a) 12345678910
b) 1 2 3 4
c) 2 3 4 5
d) None of the mentioned
View Answer

Answer: a
Explanation: Loop till the condition is met and break when the condition is not satisfied.

Output:
12345678910

5. What is the output of the given code?

for num in 1..5
  puts num*num
end

a) 12345678910
b) 1 2 3 4
c) 1 4 9 16 25
d) None of the mentioned
View Answer

Answer: c
Explanation: Loop till the condition is met and break when the condition is not satisfied.

Output:
1
4
9
16
25

6. What is the output of the given code?

for num in 1..3
  puts num*num
  m= 0
loop do
    m += 1
    puts m
    break if m == 3
end
end

a) 12345678910
b) 1 1 2 3 4 1 2 3 9 1 2 3
c) 1 4 9 16 25
d) None of the mentioned
View Answer

Answer: c
Explanation: Loop till the condition is met and break when the condition is not satisfied.

Output:
1
1
2
3
4
1
2
3
9
1
2
3

7. What is the output of the given code?

loop do
    m += 1
    puts m
    break if m == 3
end

a) Garbage values
b) 1 1 2 3 4 1 2 3 9 1 2 3
c) 1 4 9 16 25
d) None of the mentioned
View Answer

Answer: a
Explanation: Loop till the condition is met and break when the condition is not satisfied.

Output:
Garbage values

8. What is the output of the given code?

i=1
for i in 5..10
puts i^2
end

a) 7 4 5 10 11 8
b) 25 36 49 64 81 100
c) 1 4 9 16 25
d) None of the mentioned
View Answer

Answer: b
Explanation: Initialize the value of i and then print i**2.

Output:
25
36
49
64
81
100

9. What does the 1..10 indicate?
a) Inclusive range
b) Exclusive range
c) Both inclusive and exclusive range
d) None of the mentioned
View Answer

Answer: a
Explanation: 1..10 means start from one and go till 9 and even include 10.

10. What is the output of the given code?

i=5
j=10
for i in 5..10 && j in 5..10
puts i**j
end

a) Syntax error
b) 25 36 49 64 81 100
c) 1 4 9 16 25
d) None of the mentioned
View Answer

Answer: a
Explanation: Initialize the value of i and j and then when condition is true print i**j.

Output:
syntax error, unexpected keyword_in, expecting keyword_do_cond or ';' or '\n'
for i in 5..10 && j in 5..10 syntax error, unexpected keyword_end, expecting $end

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming, 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.