0
0
NumPydata~20 mins

np.ones() for one-filled arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-Filled Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.ones() with shape (3, 2)
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.ones((3, 2))
print(arr)
A
[[1 1]
 [1 1]
 [1 1]]
B
[[1. 1.]
 [1. 1.]
 [1. 1.]]
C[1. 1. 1. 1. 1. 1.]
D
[[0. 0.]
 [0. 0.]
 [0. 0.]]
Attempts:
2 left
💡 Hint
np.ones() creates an array filled with 1.0 by default with the given shape.
data_output
intermediate
1:30remaining
Shape and dtype of np.ones array
What are the shape and data type of the array created by np.ones((4,))?
NumPy
import numpy as np
arr = np.ones((4,))
print(arr.shape, arr.dtype)
A(1, 4) float32
B(4, 1) int64
C(4,) float64
D(4,) int32
Attempts:
2 left
💡 Hint
Default dtype for np.ones is float64 unless specified.
🔧 Debug
advanced
2:00remaining
Error in np.ones() usage
What error does this code produce?
NumPy
import numpy as np
arr = np.ones(3, 2)
print(arr)
ATypeError: data type not understood
BTypeError: 'int' object is not iterable
CTypeError: _ones() takes from 1 to 2 positional arguments but 3 were given
DTypeError: data type not specified
Attempts:
2 left
💡 Hint
np.ones(shape, dtype) interprets the second argument as dtype; check if it's valid.
🚀 Application
advanced
1:30remaining
Creating a 3D array of ones with dtype int
Which code creates a 3x3x3 array filled with integer ones?
Anp.ones((3,3,3), dtype=int)
Bnp.ones((3,3,3), type=int)
Cnp.ones([3,3,3], type=int)
Dnp.ones(3,3,3, dtype=int)
Attempts:
2 left
💡 Hint
dtype is the correct keyword for data type in numpy arrays.
🧠 Conceptual
expert
2:30remaining
Memory usage difference between np.ones and np.zeros
Which statement about memory usage is true when comparing np.ones((1000,1000)) and np.zeros((1000,1000)) with default dtype?
Anp.ones uses less memory because ones are stored more efficiently.
Bnp.ones uses more memory because it stores 1s instead of 0s.
Cnp.zeros uses less memory because zeros can be compressed internally.
DBoth use the same amount of memory because they store the same number of elements with the same dtype.
Attempts:
2 left
💡 Hint
Consider how numpy stores array data in memory regardless of values.