ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
s = "a1b22c333"
digits = "".join(ch for ch in s if ch.isdigit())
letters = [ch for ch in s if ch.isalpha()]
print(digits, sum(map(int, digits)), letters, s.translate(str.maketrans("abc", "xyz", "2")))
  1. A. 122333 122333 ['a', 'b', 'c'] x1yz333
  2. B. 122333 14 ['a', 'b', 'c'] x1y22z333
  3. C. 122333 14 ['a', 'b', 'c'] x1yz333
  4. D. 123 6 ['a', 'b', 'c'] x1yz333

Correct answer: C. 122333 14 ['a', 'b', 'c'] x1yz333

Digit characters are kept in order: '122333', whose digit sum is 1+2+2+3+3+3 = 14. maketrans's third argument lists characters to delete, so the 2s vanish while a, b, c become x, y, z.

More Python Programming questions

Exams that ask this topic