ProgrammingModerateTCS NQT pattern

What is printed?

What is printed?
print({x: x ** 2 for x in range(-2, 3)})
  1. A. {-2: 4, -1: 1, 0: 0, 1: 1, 2: 4}
  2. B. {0: 0, 1: 1, 2: 4}
  3. C. {-2: -4, -1: -1, 0: 0, 1: 1, 2: 4}
  4. D. {4: -2, 1: -1, 0: 0, 1: 1, 4: 2}

Correct answer: A. {-2: 4, -1: 1, 0: 0, 1: 1, 2: 4}

range(-2, 3) yields -2, -1, 0, 1, 2. Squaring each gives the key:value pairs shown; note two different keys (-2 and 2, -1 and 1) map to the same squared value, which is fine since dict keys are the x values, not the squares.

More Python Programming questions

Exams that ask this topic