Ruby Coding MCQ Questions and Answers

Welcome to Ruby Programming Coding MCQ Questions. Here, we present 20 MCQs (Multiple-Choice Questions) with code snippets to test your understanding of Ruby programming coding and logical stills. Let's get started!

1. What will be the output of the following Ruby program?

puts 10 + 5 + "Hello"
a) TypeError
b) "15Hello"
c) "Hello15"
d) "10 + 5 + Hello"

2. What does this Ruby code snippet output?

x = 8
y = 3
puts x % y
a) 2
b) 2.67
c) 3
d) None of the above

3. Consider the following Ruby code. What will it print?

x = 10
def change(x)
x = 20
end
change(x)
puts x
a) 10
b) 20
c) nil
d) Error

4. What is the output of the following Ruby function?

s1 = "hello"
s2 = "hello"
puts s1 == s2
a) true
b) false
c) nil
d) Error

5. What will this Ruby program output?

puts "5" + 2 + 3
a) TypeError
b) "523"
c) "10"
d) "53"

6. Analyze the following Ruby code snippet. What is its output?

puts 10.5.class
a) Float
b) Decimal
c) Double
d) Number

7. Consider the following Ruby code. What will it print?

i = 0
puts i += 1
a) 0
b) 1
c) Error
d) nil

8. What does this Ruby code output?

x = 5
puts x > 3 ? "Yes" : "No"
a) Yes
b) No
c) true
d) false

9. What will the following Ruby code print?

numbers = [1, 2, 3]
puts numbers.size
a) 3
b) 2
c) 1
d) Error

10. What does the following Ruby program output?

x = 5
y = 10
x, y = y, x
puts x, y
a) 5, 10
b) 10, 5
c) 10, 10
d) None of the above

11. Analyze the following Ruby code snippet. What will it output?

s = "hello"
puts s[1]
a) h
b) e
c) l
d) o

12. What will the following Ruby code output?

puts "Hello".upcase
a) HELLO
b) hello
c) Hello
d) Error

13. Consider the following Ruby code. What will it print?

result = 10 / 4
puts result
a) 2.5
b) 2
c) 2.0
d) None of the above

14. What is the output of the following Ruby function?

puts "abc".sub('b', '2')
a) a2c
b) abc
c) ac
d) ab2c

15. What will this Ruby program output?

flag = 10 > 9 && 5 > 4
puts flag
a) true
b) false
c) Error
d) nil

16. What does the following Ruby code snippet output?

array = [1, 2, 3]
array[1] = 10
puts array
a) [1, 10, 3]
b) [1, 2, 3]
c) Error
d) [10, 2, 3]

17. Consider the following Ruby code. What will it print?

s = "Hello"
t = s
t += " World"
puts s
a) Hello World
b) Hello
c) HelloHello
d) Error

18. What does the following Ruby code output?

m = {'a' => 1, 'b' => 2}
m['c'] = 3
puts m.length
a) 2
b) 3
c) Error
d) None of the above

19. What does this code snippet output?

puts 'A' + '1'
a) A1
b) Error
c) 66
d) None of the above

Comments