0
0
Rubyprogramming~20 mins

String methods (upcase, downcase, strip) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code snippet. What will it print?
Ruby
str = "  Hello World  "
puts str.strip.upcase
A"hello world"
B" HELLO WORLD "
C"HELLO WORLD"
D"Hello World"
Attempts:
2 left
💡 Hint
Remember that strip removes spaces at the start and end, and upcase makes all letters uppercase.
Predict Output
intermediate
2:00remaining
What does this Ruby code print?
Look at this code. What is the output?
Ruby
text = "  Ruby Programming  "
puts text.downcase.strip
A"RUBY PROGRAMMING"
B" ruby programming "
C"Ruby Programming"
D"ruby programming"
Attempts:
2 left
💡 Hint
Think about what downcase and strip do to the string.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This Ruby code tries to print a string in uppercase after stripping spaces, but it raises an error. What is the cause?
Ruby
str = nil
puts str.strip.upcase
ANoMethodError because <code>nil</code> has no method <code>strip</code>
BSyntaxError because <code>strip</code> is used incorrectly
CTypeError because <code>upcase</code> cannot be called on a string
DNo error, it prints an empty string
Attempts:
2 left
💡 Hint
Think about what happens if you call a method on nil in Ruby.
🧠 Conceptual
advanced
2:00remaining
What is the result of chaining these methods?
Given the string " Ruby Rocks! ", what does str.downcase.strip.upcase return?
Ruby
str = "  Ruby Rocks!  "
result = str.downcase.strip.upcase
puts result
A"RUBY ROCKS!"
B"ruby rocks!"
C" RUBY ROCKS! "
D"Ruby Rocks!"
Attempts:
2 left
💡 Hint
Remember the order: downcase then strip then upcase.
📝 Syntax
expert
2:00remaining
Which option causes a syntax error?
Which of these Ruby code snippets will cause a syntax error?
A
str = " hello "
puts str.strip.upcase
B
str = "hello"
puts str.strip.upcase(
C
str = "hello"
puts str.strip.upcase
D
str = " hello "
puts str.strip.downcase
Attempts:
2 left
💡 Hint
Look for missing or extra characters in method calls.