Challenge - 5 Problems
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how list multiplication affects inner lists and references.
✗ Incorrect
Using [[0]*3]*3 creates three references to the same inner list. Changing one element affects all rows.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
List comprehension creates independent inner lists.
✗ Incorrect
Each inner list is a separate object, so changing one element affects only that row.
🧠 Conceptual
advanced2:00remaining
Understanding Array Initialization Behavior
Why does using
arr = [[0]*3]*3 cause unexpected behavior when modifying elements in a 2D array?Attempts:
2 left
💡 Hint
Think about how list multiplication works with nested lists.
✗ Incorrect
List multiplication copies references, not the objects themselves, causing all rows to point to the same list.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Python lists can hold different data types together.
✗ Incorrect
Python lists can contain elements of different types including integers, strings, floats, lists, and tuples.
🔧 Debug
expert2: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)
Attempts:
2 left
💡 Hint
Check the valid indices for the list.
✗ Incorrect
The list has indices 0, 1, 2. Accessing index 3 is out of range and raises IndexError.