ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The string " hello world " is split by the Python-style rule that runs of spaces are treated as one separator and empty pieces are dropped. The procedure below counts the words. What is printed?

The string " hello world " is split by the Python-style rule that runs of spaces are treated as one separator and empty pieces are dropped. The procedure below counts the words. What is printed?
count = number of non-empty pieces after splitting on runs of spaces
Print count
  1. A. 8
  2. B. 2
  3. C. 17
  4. D. 3

Correct answer: B. 2

Splitting on runs of spaces leaves the two words hello and world, so the count is 2. Splitting on single spaces produces empty pieces that inflate the count, the string length is 17, and adding one to the non-empty count gives 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic