ProgrammingModerateTCS NQT pattern
What is printed?
What is printed?
print({x: x ** 2 for x in range(-2, 3)})- A. {-2: 4, -1: 1, 0: 0, 1: 1, 2: 4}
- B. {0: 0, 1: 1, 2: 4}
- C. {-2: -4, -1: -1, 0: 0, 1: 1, 2: 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
- What is printed? python data = [1, 2, 2, 3, 3, 3] print({x for x in data if x > 1})
- What is printed? python print([[y for y in range(x)] for x in range(4)])
- What is printed? python print(list(x for x in range(5) if x % 2 == 0)[1:3])
- What is printed? python print({k: v for k, v in zip(['a', 'b', 'a'], [1, 2, 3])})
- What is printed? python matrix = [[1, 2, 3], [4, 5, 6]] print([[row[i] for row in matrix] for i in range(3)])
- What is printed? python print({x % 3 for x in range(10)})