Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Ruby method upcase do to a string?
easy
A. Converts all letters in the string to lowercase
B. Converts all letters in the string to uppercase
C. Removes spaces from the start and end of the string
D. Reverses the string

Solution

  1. Step 1: Understand the method name

    The method upcase suggests making letters uppercase.
  2. Step 2: Recall the method effect on strings

    upcase changes all letters to uppercase, not lowercase or trimming spaces.
  3. Final Answer:

    Converts all letters in the string to uppercase -> Option B
  4. Quick Check:

    upcase = uppercase letters [OK]
Hint: Remember: upcase = uppercase letters [OK]
Common Mistakes:
  • Confusing upcase with downcase
  • Thinking upcase removes spaces
  • Assuming upcase reverses the string
2. Which of the following is the correct syntax to remove spaces from the start and end of a string str in Ruby?
easy
A. str.strip()
B. str.upcase()
C. strip(str)
D. str.trim()

Solution

  1. Step 1: Identify the method to remove spaces

    The Ruby method to remove spaces at start and end is strip, called on the string object.
  2. Step 2: Check correct method call syntax

    In Ruby, methods are called with dot notation: str.strip(). There is no trim method in Ruby.
  3. Final Answer:

    str.strip() -> Option A
  4. Quick Check:

    strip method called with dot = str.strip() [OK]
Hint: Use dot notation: string.strip() removes spaces [OK]
Common Mistakes:
  • Using strip as a standalone function
  • Using trim instead of strip
  • Calling upcase to remove spaces
3. What is the output of the following Ruby code?
str = "  Hello World  "
puts str.strip.upcase
medium
A. " HELLO WORLD "
B. " hello world "
C. "hello world"
D. "HELLO WORLD"

Solution

  1. Step 1: Apply strip to the string

    strip removes spaces at the start and end, so " Hello World " becomes "Hello World".
  2. Step 2: Apply upcase to the stripped string

    upcase converts all letters to uppercase, so "Hello World" becomes "HELLO WORLD".
  3. Final Answer:

    "HELLO WORLD" -> Option D
  4. Quick Check:

    strip + upcase = "HELLO WORLD" [OK]
Hint: strip removes spaces, upcase makes uppercase [OK]
Common Mistakes:
  • Forgetting strip removes spaces
  • Applying upcase before strip
  • Expecting original spaces to remain
4. The following Ruby code is intended to convert a string name to lowercase and remove spaces at the start and end. What is wrong?
name = "  RubyLang  "
name.downcase.strip
medium
A. The method strip does not exist
B. The methods are called in the wrong order
C. The result is not saved or printed
D. The string should be converted to uppercase instead

Solution

  1. Step 1: Check method calls and order

    Both downcase and strip are valid methods and can be chained in any order.
  2. Step 2: Check if result is used

    The code calls methods but does not save or print the result, so the original string remains unchanged.
  3. Final Answer:

    The result is not saved or printed -> Option C
  4. Quick Check:

    Method calls without assignment or output do nothing visible [OK]
Hint: Always save or print method results to see changes [OK]
Common Mistakes:
  • Thinking method order matters here
  • Assuming methods change string in place
  • Confusing strip with a non-existent method
5. You have a list of names with inconsistent spacing and letter cases:
names = ["  Alice", "BOB  ", "  cHaRlie  "]

Which Ruby code correctly cleans this list by removing spaces and making all names lowercase?
hard
A. clean_names = names.map { |n| n.strip.downcase }
B. clean_names = names.map { |n| n.downcase.chomp }
C. clean_names = names.map { |n| n.strip.upcase }
D. clean_names = names.map { |n| n.downcase }

Solution

  1. Step 1: Understand the goal

    We want to remove spaces and convert all names to lowercase.
  2. Step 2: Check each option

    clean_names = names.map { |n| n.strip.downcase } uses strip then downcase, which removes spaces first, then lowercases. clean_names = names.map { |n| n.downcase.chomp } uses chomp after downcase, but chomp removes only trailing newlines, not spaces, so spaces remain. clean_names = names.map { |n| n.strip.upcase } uses upcase which is wrong. clean_names = names.map { |n| n.downcase } does not remove spaces.
  3. Step 3: Choose best practice

    The code that uses strip to remove whitespace and downcase to lowercase correctly cleans the names.
  4. Final Answer:

    clean_names = names.map { |n| n.strip.downcase } -> Option A
  5. Quick Check:

    strip then downcase cleans names correctly [OK]
Hint: Strip spaces first, then change case for clean strings [OK]
Common Mistakes:
  • Using upcase instead of downcase
  • Confusing chomp with strip
  • Forgetting to map over the list
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