Bird
0
0

You want to write a predicate method palindrome? that returns true if a string reads the same forwards and backwards. Which code correctly implements this?

hard📝 Application Q8 of 15
Ruby - Methods
You want to write a predicate method palindrome? that returns true if a string reads the same forwards and backwards. Which code correctly implements this?
Adef palindrome?(str) str == str.reverse end
Bdef palindrome?(str) str.reverse end
Cdef palindrome?(str) str.length > 0 end
Ddef palindrome?(str) str == str.upcase end
Step-by-Step Solution
Solution:
  1. Step 1: Understand palindrome logic

    A palindrome reads the same forwards and backwards, so the string must equal its reverse.
  2. Step 2: Check each option

    def palindrome?(str) str == str.reverse end compares str with str.reverse, returning true if they match.
  3. Final Answer:

    def palindrome?(str)\n str == str.reverse\nend -> Option A
  4. Quick Check:

    Palindrome check = string equals reverse [OK]
Quick Trick: Compare string to its reverse for palindrome check [OK]
Common Mistakes:
MISTAKES
  • Returning reversed string instead of boolean
  • Checking length only
  • Comparing to uppercase version

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes