Data Structures & AlgorithmsEasyTCS NQT · Infosys pattern

What is printed? (Python)

What is printed? (Python)
a = [3, 8, 1, 9, 4]
a.insert(2, 7)
a.pop()
a.remove(9)
print(a, len(a))
  1. A. [3, 8, 7, 1, 4] 5
  2. B. [3, 7, 8, 1] 4
  3. C. [3, 8, 7, 1] 4
  4. D. [3, 8, 1, 7] 4

Correct answer: C. [3, 8, 7, 1] 4

insert(2, 7) puts 7 at index 2: [3, 8, 7, 1, 9, 4]; pop() removes the last element (4); remove(9) deletes the first 9 by value. Result [3, 8, 7, 1], length 4. insert and remove are O(n) because elements shift.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic