Data Structures & AlgorithmsHardGATE CS · DSA

Reverse the order of words, collapsing multiple spaces. What is printed?

Reverse the order of words, collapsing multiple spaces. What is printed?
s = "  new   the   is   oil "
w = s.split()
print("|" + " ".join(reversed(w)) + "|", len(s.split(" ")))
  1. A. |oil is the new| 14
  2. B. |oil is the new| 12
  3. C. |oil is the new| 4
  4. D. |oil is the new| 13

Correct answer: D. |oil is the new| 13

split() with no argument drops empty strings, but split(" ") keeps an empty string for every extra space. Output: |oil is the new| 13.

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

Exams that ask this topic