ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [[1, 2], [3, 4]] b = [row[:] for row in a] c = a[:] a[0][0] = 9 c[1][1] = 7 print(b, c)
- A. [[1, 2], [3, 4]] [[9, 2], [3, 7]]
- B. [[9, 2], [3, 4]] [[9, 2], [3, 7]]
- C. [[1, 2], [3, 4]] [[1, 2], [3, 7]]
- D. [[1, 2], [3, 4]] [[9, 2], [3, 4]]
Correct answer: A. [[1, 2], [3, 4]] [[9, 2], [3, 7]]
row[:] makes independent copies of each row, so b is unaffected by a[0][0]. a[:] is only a shallow copy, so c shares the same inner lists and shows both changes made through a and c.
More Python Programming questions
- 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…
- What is printed? python x = 7 print(x // 2 if x % 2 else x / 2, x > 3 > 2, not x > 3 == 4)
- What is printed? python print(pow(-2, 3, 5), pow(3, -1, 7), pow(2, 10, -7), (-2) 3 % 5)
- What is printed? python try: s = str(10 4301) print(len(s)) except ValueError: print('too many digits')
- What is printed? python print(' xt'.strip() == 'x', 'atb'.expandtabs(4), len('atb'.expandtabs(4)), 'abrncd'.splitlines())