Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Basics and Runtime
What will be the output of the following Ruby code?
puts "Hi"
print "There"
p "!"
AHi There! "!"
BHi There"!"
C"!"erehT iH
DHi There "!"
Step-by-Step Solution
Solution:
  1. Step 1: Analyze puts "Hi"

    This prints "Hi" followed by a newline.
  2. Step 2: Analyze print "There"

    This prints "There" without a newline, so the cursor stays on the same line.
  3. Step 3: Analyze p "!"

    p prints the inspect form of the string, which includes quotes, and adds a newline. So it prints "!" with quotes and a newline.
  4. Final Answer:

    Hi There "!" -> Option D
  5. Quick Check:

    puts adds newline, print no newline, p shows quotes = A [OK]
Quick Trick: Remember p shows quotes and adds newline [OK]
Common Mistakes:
MISTAKES
  • Ignoring quotes printed by p
  • Assuming print adds newline
  • Mixing output lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes