Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (A) 1 2 3 4
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.

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (C) 1 2 3 1 2 3
Explanation: The program will print 1 2 3 two times.

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (B) 1 4 9 16
Explanation: The program will print 1 4 9 16.

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (D) 36 49 64 81 100
Explanation: The output for the following code is 36 49 64 81 100.

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (B) Syntax Error
Explanation: syntax error, unexpected keyword_in, expecting keyword_do_cond.

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (A) Prints the number from 1 to 4
Explanation: Counter value is initialized to 1 and it keeps on looping till the counter is less than 5.

Q. What will be the output of the given code?

Code:
counter = true
while counter !=false
  puts counter
end
  • (A) TRUE
  • (B) FALSE
  • (C) Syntax Error
  • (D) Infinite Loop
πŸ’¬ Discuss
βœ… Correct Answer: (D) Infinite Loop
Explanation: Counter value is true in all cases hence true will be printed infinite times.

Q. What will be the output of the given code?

Code:
while a&&b
puts a
end
  • (A) TRUE
  • (B) FALSE
  • (C) Syntax Error
  • (D) Name Error
πŸ’¬ Discuss
βœ… Correct Answer: (D) Name Error
Explanation: undefined local variable or method

Q. What will be the output of the given code?

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
πŸ’¬ Discuss
βœ… Correct Answer: (C) Infinite Loop
Explanation: Infinite loop is the correct answer.

Q. What will be the output of the given code?

Code:
i = 4
while i > 0 do
  print i
  i -= 1
end
  • (A) 321
  • (B) 3210
  • (C) 4321
  • (D) 43210
πŸ’¬ Discuss
βœ… Correct Answer: (C) 4321
Explanation: The do statement here indicates that till the while condition is true execute the instructions.