Bird
0
0

Find the bug in this code that tries to keep track of word order:

medium📝 Debug Q7 of 15
NLP - Sequence Models for NLP
Find the bug in this code that tries to keep track of word order:
sentence = ['go', 'home']
state = ''
for i in range(len(sentence)):
    state += sentence[i+1][0]
print(state)
ALoop should use 'for word in sentence'
BConcatenation operator is wrong
CIndex out of range error due to i+1
DVariable 'state' should be integer
Step-by-Step Solution
Solution:
  1. Step 1: Check loop range and indexing

    Loop runs i from 0 to len(sentence)-1, but accessing sentence[i+1] causes index error at last iteration.
  2. Step 2: Understand Python indexing

    sentence has length 2, so sentence[2] is out of range.
  3. Final Answer:

    Index out of range error due to i+1 -> Option C
  4. Quick Check:

    Index error from i+1 = A [OK]
Quick Trick: Check index limits when using i+1 [OK]
Common Mistakes:
MISTAKES
  • Ignoring index error
  • Thinking concatenation is wrong
  • Changing variable type unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes