Data Structures & AlgorithmsHardGATE CS · DSA
A Python 2-D list is created two ways and one cell is set. What is printed?
A Python 2-D list is created two ways and one cell is set. What is printed?
a = [[0] * 3] * 3 b = [[0] * 3 for _ in range(3)] a[2][2] = 5 b[2][2] = 5 print(sum(map(sum, a)), sum(map(sum, b)))
- A. 14 5
- B. 15 6
- C. 15 5
- D. 5 5
Correct answer: C. 15 5
[[0] * 3] * 3 repeats a reference to ONE inner list, so a single assignment changes all three rows; the comprehension builds independent rows. Output: 15 5.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Rearrange a linked list so that nodes at odd POSITIONS (1st, 3rd, …) come first, followed by the even-position nodes, keeping relative…
- What is the length of the longest contiguous substring of ((()((()(()) that is a valid (balanced) parentheses string?
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[90, 11, 62, 33], [40, 71, 82, 13], [10, 81…
- A 2-D array A[0..11][0..29] of 4-byte elements is stored in row-major order starting at address 1000. What is the address of A[5][11]?