ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
nums = [3, 1, 2] nums2 = sorted(nums, reverse=True) nums.sort() print(nums, nums2, nums is sorted(nums))
- A. [1, 2, 3] [3, 2, 1] False
- B. [3, 2, 1] [1, 2, 3] False
- C. [1, 2, 3] [3, 2, 1] True
- D. [1, 2, 3] [1, 2, 3] False
Correct answer: A. [1, 2, 3] [3, 2, 1] False
sort() works in place and returns None, but sorted() always returns a new list. nums and the new sorted list are different objects, so the identity test is False.
More Python Programming questions
- What is printed? python nums = [13, 22, 5, 31, 8, 3] print(sorted(nums, key=lambda x: x % 10), sorted(nums, key=lambda x: x % 10…
- What is printed? python def gen(): yield 1 return 5 yield 2 it = gen() print(list(it), next(it, 'done'))
- What is printed? python from itertools import accumulate print(list(accumulate([2, 3, 4], lambda a, b: a b)), list(accumulate([1, -1, 2])))
- What is printed? python a = [[1, 2], [3, 4]] b = [row[:] for row in a] c = a[:] a[0][0] = 9 c[1][1] = 7 print(b, c)
- What is printed? python print(len(str(2 64)), (2 64).bitlength(), 2 64 % 9, pow(2, 64, 9))
- Which technique gives the lowest total cost for answering q range-sum queries on a fixed Python list of length n, where n and q are both 2…