0
0
Rubyprogramming~20 mins

Puts, print, and p output differences in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output difference between puts and print
What is the output of this Ruby code snippet?
Ruby
puts "Hello"
print "World"
AHello\nWorld
BHelloWorld\n
C"Hello"\n"World"
DHello\nWorld\n
Attempts:
2 left
💡 Hint
Remember that puts adds a newline after output, print does not.
Predict Output
intermediate
2:00remaining
Output difference between p and puts
What is the output of this Ruby code snippet?
Ruby
p "Hello"
puts "Hello"
A"Hello"\nHello
B"Hello"\n"Hello"
CHello\nHello
DHello\n"Hello"
Attempts:
2 left
💡 Hint
p shows the inspected version of the object, puts prints the string as is.
Predict Output
advanced
2:00remaining
Output of p with an array vs puts with an array
What is the output of this Ruby code snippet?
Ruby
p [1, 2, 3]
puts [1, 2, 3]
A1\n2\n3\n[1, 2, 3]
B[1, 2, 3]\n[1, 2, 3]
C[1, 2, 3]\n1\n2\n3
D1 2 3\n[1, 2, 3]
Attempts:
2 left
💡 Hint
p prints the inspected array, puts prints each element on its own line.
🧠 Conceptual
advanced
2:00remaining
Behavior difference of puts, print, and p with nil
Which statement correctly describes the output when printing nil with puts, print, and p in Ruby?
Aputs prints "nil", print prints nothing, p prints nothing
Bputs prints a blank line, print prints nothing, p prints "nil"
Cputs prints nothing, print prints a blank line, p prints "nil"
Dputs prints "nil", print prints "nil", p prints a blank line
Attempts:
2 left
💡 Hint
Think about how each method handles nil values and output.
🔧 Debug
expert
3:00remaining
Why does print not add a newline but puts does?
Consider this Ruby code: print "Hello" print "World" puts "Hello" puts "World" What is the output and why does print behave differently from puts?
AHello\nWorldHelloWorld because print adds newline only after first call
BHello\nWorld\nHelloWorld because print adds newline, puts does not
CHelloWorld\nHelloWorld because both print and puts add newline
DHelloWorldHello\nWorld\n because print outputs exactly what is given without newline, puts adds newline after output
Attempts:
2 left
💡 Hint
Think about how print and puts handle newlines differently.