0
0
DSA Pythonprogramming~20 mins

Array Declaration and Initialization in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Array Initialization with List Multiplication
What is the output of the following Python code that initializes a 2D array using list multiplication and then modifies one element?
DSA Python
arr = [[0]*3]*3
arr[0][0] = 1
print(arr)
A[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
B[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
C[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
DSyntaxError
Attempts:
2 left
💡 Hint
Think about how list multiplication affects inner lists and references.
Predict Output
intermediate
2:00remaining
Output of Array Initialization Using List Comprehension
What is the output of this Python code that initializes a 2D array using list comprehension and modifies one element?
DSA Python
arr = [[0 for _ in range(3)] for _ in range(3)]
arr[0][0] = 1
print(arr)
A[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
B[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
C[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
DTypeError
Attempts:
2 left
💡 Hint
List comprehension creates independent inner lists.
🧠 Conceptual
advanced
2:00remaining
Understanding Array Initialization Behavior
Why does using arr = [[0]*3]*3 cause unexpected behavior when modifying elements in a 2D array?
ABecause it creates a 3x3 array with independent inner lists.
BBecause it initializes the array with zeros incorrectly.
CBecause it creates multiple references to the same inner list, so changes affect all rows.
DBecause Python does not support 2D arrays.
Attempts:
2 left
💡 Hint
Think about how list multiplication works with nested lists.
Predict Output
advanced
2:00remaining
Output of Array Initialization with Mixed Types
What is the output of this Python code that initializes an array with mixed types and prints it?
DSA Python
arr = [1, 'two', 3.0, [4], (5,)]
print(arr)
A[1, 2, 3, 4, 5]
B[1, 'two', 3.0, 4, 5]
CTypeError
D[1, 'two', 3.0, [4], (5,)]
Attempts:
2 left
💡 Hint
Python lists can hold different data types together.
🔧 Debug
expert
2:00remaining
Identify the Error in Array Initialization
What error does the following Python code raise when trying to initialize an array?
DSA Python
arr = [0]*3
arr[3] = 1
print(arr)
ASyntaxError
BIndexError
CTypeError
DNo error, prints [0, 0, 0, 1]
Attempts:
2 left
💡 Hint
Check the valid indices for the list.