Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Regular Expressions
What will be the output of this Ruby code?
text = "OpenAI GPT-4"
index = text =~ /GPT/
puts index
Anil
B0
C7
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Locate the pattern in the string

    The string is "OpenAI GPT-4". The substring "GPT" starts at index 7 (counting from 0).
  2. Step 2: Understand what =~ returns

    The operator returns the index of the first match, so it returns 7.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Index of "GPT" in string = 7 [OK]
Quick Trick: Count characters from zero to find match index [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Expecting true/false instead of index
  • Assuming nil if unsure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes