ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import itertools
print([''.join(p) for p in itertools.permutations('abc', 2)][:4])
  1. A. ['ab', 'ac', 'ba', 'bc']
  2. B. ['ab', 'ba', 'ac', 'ca']
  3. C. ['ab', 'ac', 'bc', 'ba']
  4. D. ['ab', 'bc', 'ac', 'ba']

Correct answer: A. ['ab', 'ac', 'ba', 'bc']

permutations(iterable, r) emits tuples in lexicographic order of the input positions. The first prefix is 'a' with each later letter, so ab then ac, then the prefix 'b' gives ba and bc.

More Python Programming questions

Exams that ask this topic