Ruby MCQ
Test your Ruby knowledge with 100 multiple choice questions covering fundamentals to advanced concepts, with instant feedback and explanations.
Which command prints text to the console with a newline at the end in Ruby?
2How do you create a comment in Ruby?
3Which keyword is used to define a method in Ruby?
4What is the correct way to create a variable named name with value "Alice"?
5Which symbol is used to denote a Symbol literal in Ruby, e.g. :name?
6How do you create an array containing the numbers 1, 2, and 3?
7Which method returns the number of elements in an array?
8What does the "each" method do when called on an array?
9How do you create a hash (dictionary) mapping "name" to "Alice"?
10Which keyword starts a conditional statement in Ruby?
11What does "nil" represent in Ruby?
12How do you define a class named "Dog" in Ruby?
13What is the special variable name used to reference the current object instance inside a method?
14Which symbol prefix denotes an instance variable in Ruby?
15How do you concatenate two strings "Hello" and "World" with a space between them?
16What is the result of "3 ** 2" in Ruby?
17Which loop construct repeats code a fixed number of times, e.g. 5 times?
18What does the "<=>" operator (spaceship operator) return?
19How do you check if a string is empty?
20What is the file extension for Ruby source files?
21How do you create a range from 1 to 5 (inclusive)?
22Which method converts a string "42" to an integer?
23What does "attr_accessor :name" generate for a class?
24How do you start an interactive Ruby shell from the command line?
25What is the result of "nil.to_s"?
26Which keyword is used to handle exceptions in Ruby?
27What does "1.0 / 3" return in Ruby?
28How do you define a constant in Ruby?
29What does the "map" method do when called on an array?
30Which operator checks for equality of value in Ruby?
31How do you require another file or library in Ruby?
32What does "puts [1, 2, 3]" output?
33What is a "block" in Ruby?
34Which method removes the last element from an array and returns it?
35What does "String#upcase" do?
36How do you define a method with a default parameter value?
37What does "unless condition" do in Ruby?
38What does ".freeze" do to an object?
39Which keyword is used to create a module in Ruby?
40What is the purpose of "Gemfile" in a Ruby project?
What is the difference between a Proc and a Lambda in Ruby?
2What does the "&block" parameter syntax do in a method definition?
3What is "duck typing" in Ruby?
4What does "include" do when used inside a class definition with a module?
5What is the difference between "extend" and "include" with modules?
6What does "method_missing" allow a class to do?
7What is the purpose of "yield" inside a method?
8What does "Array#inject" (also known as reduce) do?
9What is the difference between "==", "equal?", and "eql?" in Ruby?
10What does "Comparable" module provide when a class implements "<=>"?
11What is the purpose of "Enumerable" module?
12What does "case/when" provide that an if/elsif chain also does, and how does it differ semantically?
13What is the effect of "private" keyword in a class definition?
14What does "Array#select" (alias filter) return?
15What is the purpose of "respond_to?" method?
16What does "class << self" inside a class definition do?
17What does "Object#dup" do compared to "Object#clone"?
18What is the purpose of keyword arguments in method definitions, e.g. "def greet(name:, greeting: 'Hello')"?
19What does the splat operator "*" do in "def foo(*args)"?
20What is the difference between "load" and "require" for including external Ruby files?
21What does "Hash#each_pair" (or "each") yield when iterating over a hash?
22What does "Object.new.tap { |o| puts o }" demonstrate about the "tap" method?
23What is "monkey patching" in Ruby?
24What does "ObjectSpace" allow in Ruby (conceptually)?
25What is the purpose of "Struct.new" in Ruby?
26What does "begin ... rescue ... ensure ... end" guarantee about the "ensure" block?
27What does "String#gsub(pattern, replacement)" do?
28What does "Kernel#freeze" combined with constants help prevent?
29What is the purpose of the "protected" access modifier compared to "private"?
30What does "Array#flatten" do?
31What does "Object#send" allow you to do?
32What is the difference between "Array#map!" and "Array#map"?
33What does "Comparable#clamp(min, max)" do?
34What does "Hash#fetch(key, default)" do differently from "hash[key]"?
35What is the role of "Module#prepend" compared to "include"?
36What does "Object#instance_variable_get(:@name)" do?
37What is the purpose of "Array#each_with_index"?
38What does "String#split(",")" return for the string "a,b,c"?
39What does "Object#is_a?(SomeClass)" check, and how does it relate to "kind_of?"?
40What does "Array#uniq" do?
What is the Ruby "ancestors" chain and how does method resolution order (MRO) work with multiple included modules?
2What is the difference between "Object#instance_eval" and "Object#class_eval" (also "module_eval")?
3How does Ruby's Garbage Collector (GC) generally manage memory, particularly with the "generational" GC introduced in later MRI versions?
4What is the Global Interpreter Lock (GIL), also called GVL in MRI Ruby, and how does it affect threads?
5What does "define_method" allow that a regular "def" does not?
6What is "Refinements" in Ruby and how do they relate to monkey patching?
7What is the purpose of "Fiber" in Ruby?
8How does "Object#method_missing" combined with "respond_to_missing?" form a complete dynamic method pattern?
9What is the difference between "BasicObject" and "Object" as a superclass for a custom class?
10What does "ObjectSpace::WeakMap" provide and why might it be used?
11What is the significance of "frozen_string_literal: true" magic comment at the top of a Ruby file?
12What problem do "Ractors" (introduced in Ruby 3.0) aim to solve?
13What does "Kernel#binding" return and how is it commonly used?
14How does Ruby's "===" (case equality) operator differ across different classes such as Range, Regexp, Class, and Proc when used in "case/when"?
15What is "ObjectSpace.each_object" used for, and what is a caveat of using it in production code?
16What is the difference between "Module#const_get" with inheritance lookup versus directly accessing a constant via "::"?
17What does "Comparable" combined with "<=>" returning nil signify, and how should methods like "<" handle it?
18What is "constant reassignment warning" behavior in Ruby, and how does this relate to "freeze" on classes/modules?
19How does Ruby's "Method#unbind" and "UnboundMethod#bind" pair work, and what is a practical use case?
20What does it mean that Ruby's "Integer" class was unified (Fixnum and Bignum merged) in Ruby 2.4+, and what is the practical implication?