Bird
Raised Fist0
Rubyprogramming~10 mins

How Ruby interprets code at runtime - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print 'Hello, Ruby!' to the console.

Ruby
puts [1]
Drag options to blanks, or click blank then click option'
A'Hello Ruby'
BHello, Ruby!
C"Hello, Ruby!"
Dprint 'Hello, Ruby!'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Using print instead of puts when the instruction asks for puts.
2fill in blank
medium

Complete the code to define a method named 'greet' that takes a name and prints a greeting.

Ruby
def greet(name)
  puts "Hello, #{ [1] }!"
end
Drag options to blanks, or click blank then click option'
Auser
Bname
Cgreet
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the method parameter.
Forgetting that Ruby variables are case-sensitive.
3fill in blank
hard

Fix the error in the code to correctly add two numbers and print the result.

Ruby
a = 5
b = 10
puts a [1] b
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to put spaces around operators (though Ruby allows it).
4fill in blank
hard

Fill both blanks to create a hash with keys as symbols and values as strings.

Ruby
person = { [1]: "Alice", [2]: "Developer" }
Drag options to blanks, or click blank then click option'
Aname
Bage
Cjob
Dcity
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings as keys instead of symbols.
Choosing keys that don't match the values.
5fill in blank
hard

Fill all three blanks to create an array of numbers, select only even numbers, and print them.

Ruby
numbers = [[1], [2], [3]]
even_numbers = numbers.select { |n| n.even? }
puts even_numbers
Drag options to blanks, or click blank then click option'
A2
B4
C6
Deven
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd numbers in the array.
Forgetting to use the even? method to filter.

Practice

(1/5)
1. What does it mean that Ruby interprets code at runtime?
easy
A. Ruby only checks code syntax but does not run it.
B. Ruby converts all code to machine language before running.
C. Ruby runs code only after saving it to a file.
D. Ruby reads and runs code line by line as the program runs.

Solution

  1. Step 1: Understand interpretation

    Interpreting means reading and executing code step by step during program execution.
  2. Step 2: Compare options

    The option "Ruby reads and runs code line by line as the program runs." describes this process correctly; others describe compiling or partial actions.
  3. Final Answer:

    Ruby reads and runs code line by line as the program runs. -> Option D
  4. Quick Check:

    Interpretation = line-by-line execution [OK]
Hint: Interpret means run code step-by-step as you go [OK]
Common Mistakes:
  • Confusing interpretation with compilation
  • Thinking Ruby only checks syntax
  • Believing Ruby runs code after saving only
2. Which of the following is the correct way to print "Hello" in Ruby?
easy
A. echo "Hello"
B. console.log("Hello")
C. print("Hello")
D. printf("Hello %d")

Solution

  1. Step 1: Identify Ruby print syntax

    Ruby uses the method print or puts to output text; print("Hello") is valid.
  2. Step 2: Check other options

    echo is PHP, console.log is JavaScript, printf("Hello %d") is C-style but errors due to missing argument for %d.
  3. Final Answer:

    print("Hello") -> Option C
  4. Quick Check:

    Ruby print method = print("Hello") [OK]
Hint: Use print or puts to show text in Ruby [OK]
Common Mistakes:
  • Using JavaScript or PHP print commands
  • Missing parentheses in print
  • Confusing printf with print
3. What will be the output of this Ruby code?
count = 0
3.times do
  count += 1
end
print count
medium
A. 3
B. 0
C. 1
D. Error

Solution

  1. Step 1: Understand the times loop

    The times method runs the block 3 times, increasing count by 1 each time.
  2. Step 2: Calculate final count

    Starting from 0, after 3 increments, count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    0 + 1 + 1 + 1 = 3 [OK]
Hint: times runs block that many times, increment count each time [OK]
Common Mistakes:
  • Thinking count stays 0
  • Confusing times with for loop syntax
  • Expecting error due to block
4. Find the error in this Ruby code:
def greet
  puts "Hello"
end

greet(1)
medium
A. ArgumentError: wrong number of arguments
B. No error, prints Hello
C. SyntaxError due to missing parentheses
D. NameError: greet not defined

Solution

  1. Step 1: Check method definition

    greet is defined without parameters, so it expects zero arguments.
  2. Step 2: Check method call

    Calling greet(1) passes one argument, causing ArgumentError.
  3. Final Answer:

    ArgumentError: wrong number of arguments -> Option A
  4. Quick Check:

    Method params mismatch = ArgumentError [OK]
Hint: Method defined with no params, calling with one causes error [OK]
Common Mistakes:
  • Thinking Ruby ignores extra arguments
  • Confusing syntax error with argument error
  • Assuming greet is undefined
5. Given this Ruby code:
def add(x, y)
  x + y
end

result = add(2, 3) + add(4, 5)
print result

How does Ruby interpret and calculate the final output?
hard
A. It prints the string 'add(2, 3)add(4, 5)'
B. It adds 2 and 3, then adds 4 and 5, sums both results to print 14
C. It throws an error because add is called twice
D. It adds 2 and 3, ignores second add call, prints 5

Solution

  1. Step 1: Evaluate first add call

    add(2, 3) returns 5 by adding 2 + 3.
  2. Step 2: Evaluate second add call and sum

    add(4, 5) returns 9; sum 5 + 9 = 14.
  3. Step 3: Print the result

    print outputs 14 as the final result.
  4. Final Answer:

    14 -> Option B
  5. Quick Check:

    5 + 9 = 14 [OK]
Hint: Each method call runs fully, then sums results [OK]
Common Mistakes:
  • Thinking only first add runs
  • Expecting string concatenation instead of addition
  • Assuming error on multiple calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard