💎

Ruby MCQ

Test your Ruby knowledge with 100 multiple choice questions covering fundamentals to advanced concepts, with instant feedback and explanations.

100 Questions 40 Beginner 40 Intermediate 20 Advanced
1

Which command prints text to the console with a newline at the end in Ruby?

2

How do you create a comment in Ruby?

3

Which keyword is used to define a method in Ruby?

4

What is the correct way to create a variable named name with value "Alice"?

5

Which symbol is used to denote a Symbol literal in Ruby, e.g. :name?

6

How do you create an array containing the numbers 1, 2, and 3?

7

Which method returns the number of elements in an array?

8

What does the "each" method do when called on an array?

9

How do you create a hash (dictionary) mapping "name" to "Alice"?

10

Which keyword starts a conditional statement in Ruby?

11

What does "nil" represent in Ruby?

12

How do you define a class named "Dog" in Ruby?

13

What is the special variable name used to reference the current object instance inside a method?

14

Which symbol prefix denotes an instance variable in Ruby?

15

How do you concatenate two strings "Hello" and "World" with a space between them?

16

What is the result of "3 ** 2" in Ruby?

17

Which loop construct repeats code a fixed number of times, e.g. 5 times?

18

What does the "<=>" operator (spaceship operator) return?

19

How do you check if a string is empty?

20

What is the file extension for Ruby source files?

21

How do you create a range from 1 to 5 (inclusive)?

22

Which method converts a string "42" to an integer?

23

What does "attr_accessor :name" generate for a class?

24

How do you start an interactive Ruby shell from the command line?

25

What is the result of "nil.to_s"?

26

Which keyword is used to handle exceptions in Ruby?

27

What does "1.0 / 3" return in Ruby?

28

How do you define a constant in Ruby?

29

What does the "map" method do when called on an array?

30

Which operator checks for equality of value in Ruby?

31

How do you require another file or library in Ruby?

32

What does "puts [1, 2, 3]" output?

33

What is a "block" in Ruby?

34

Which method removes the last element from an array and returns it?

35

What does "String#upcase" do?

36

How do you define a method with a default parameter value?

37

What does "unless condition" do in Ruby?

38

What does ".freeze" do to an object?

39

Which keyword is used to create a module in Ruby?

40

What is the purpose of "Gemfile" in a Ruby project?

1

What is the difference between a Proc and a Lambda in Ruby?

2

What does the "&block" parameter syntax do in a method definition?

3

What is "duck typing" in Ruby?

4

What does "include" do when used inside a class definition with a module?

5

What is the difference between "extend" and "include" with modules?

6

What does "method_missing" allow a class to do?

7

What is the purpose of "yield" inside a method?

8

What does "Array#inject" (also known as reduce) do?

9

What is the difference between "==", "equal?", and "eql?" in Ruby?

10

What does "Comparable" module provide when a class implements "<=>"?

11

What is the purpose of "Enumerable" module?

12

What does "case/when" provide that an if/elsif chain also does, and how does it differ semantically?

13

What is the effect of "private" keyword in a class definition?

14

What does "Array#select" (alias filter) return?

15

What is the purpose of "respond_to?" method?

16

What does "class << self" inside a class definition do?

17

What does "Object#dup" do compared to "Object#clone"?

18

What is the purpose of keyword arguments in method definitions, e.g. "def greet(name:, greeting: 'Hello')"?

19

What does the splat operator "*" do in "def foo(*args)"?

20

What is the difference between "load" and "require" for including external Ruby files?

21

What does "Hash#each_pair" (or "each") yield when iterating over a hash?

22

What does "Object.new.tap { |o| puts o }" demonstrate about the "tap" method?

23

What is "monkey patching" in Ruby?

24

What does "ObjectSpace" allow in Ruby (conceptually)?

25

What is the purpose of "Struct.new" in Ruby?

26

What does "begin ... rescue ... ensure ... end" guarantee about the "ensure" block?

27

What does "String#gsub(pattern, replacement)" do?

28

What does "Kernel#freeze" combined with constants help prevent?

29

What is the purpose of the "protected" access modifier compared to "private"?

30

What does "Array#flatten" do?

31

What does "Object#send" allow you to do?

32

What is the difference between "Array#map!" and "Array#map"?

33

What does "Comparable#clamp(min, max)" do?

34

What does "Hash#fetch(key, default)" do differently from "hash[key]"?

35

What is the role of "Module#prepend" compared to "include"?

36

What does "Object#instance_variable_get(:@name)" do?

37

What is the purpose of "Array#each_with_index"?

38

What does "String#split(",")" return for the string "a,b,c"?

39

What does "Object#is_a?(SomeClass)" check, and how does it relate to "kind_of?"?

40

What does "Array#uniq" do?

1

What is the Ruby "ancestors" chain and how does method resolution order (MRO) work with multiple included modules?

2

What is the difference between "Object#instance_eval" and "Object#class_eval" (also "module_eval")?

3

How does Ruby's Garbage Collector (GC) generally manage memory, particularly with the "generational" GC introduced in later MRI versions?

4

What is the Global Interpreter Lock (GIL), also called GVL in MRI Ruby, and how does it affect threads?

5

What does "define_method" allow that a regular "def" does not?

6

What is "Refinements" in Ruby and how do they relate to monkey patching?

7

What is the purpose of "Fiber" in Ruby?

8

How does "Object#method_missing" combined with "respond_to_missing?" form a complete dynamic method pattern?

9

What is the difference between "BasicObject" and "Object" as a superclass for a custom class?

10

What does "ObjectSpace::WeakMap" provide and why might it be used?

11

What is the significance of "frozen_string_literal: true" magic comment at the top of a Ruby file?

12

What problem do "Ractors" (introduced in Ruby 3.0) aim to solve?

13

What does "Kernel#binding" return and how is it commonly used?

14

How does Ruby's "===" (case equality) operator differ across different classes such as Range, Regexp, Class, and Proc when used in "case/when"?

15

What is "ObjectSpace.each_object" used for, and what is a caveat of using it in production code?

16

What is the difference between "Module#const_get" with inheritance lookup versus directly accessing a constant via "::"?

17

What does "Comparable" combined with "<=>" returning nil signify, and how should methods like "<" handle it?

18

What is "constant reassignment warning" behavior in Ruby, and how does this relate to "freeze" on classes/modules?

19

How does Ruby's "Method#unbind" and "UnboundMethod#bind" pair work, and what is a practical use case?

20

What does it mean that Ruby's "Integer" class was unified (Fixnum and Bignum merged) in Ruby 2.4+, and what is the practical implication?