Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the np.ones() function do in NumPy?
easy
A. Creates an array filled with ones
B. Creates an array filled with zeros
C. Creates an array filled with random numbers
D. Creates an empty array

Solution

  1. Step 1: Understand the function purpose

    np.ones() is designed to create arrays filled with the number one.
  2. Step 2: Compare with other array creation functions

    Functions like np.zeros() create arrays filled with zeros, but np.ones() specifically fills with ones.
  3. Final Answer:

    Creates an array filled with ones -> Option A
  4. Quick Check:

    np.ones() = array of ones [OK]
Hint: Remember: ones means all elements are 1 [OK]
Common Mistakes:
  • Confusing np.ones() with np.zeros()
  • Thinking it creates random values
  • Assuming it creates an empty array
2. Which of the following is the correct syntax to create a 2x3 array of ones with integer type using np.ones()?
easy
A. np.ones((2, 3), dtype=int)
B. np.ones([2, 3], type=int)
C. np.ones(2, 3, dtype='int')
D. np.ones(2, 3, int)

Solution

  1. Step 1: Check the shape argument format

    The shape must be given as a tuple, so (2, 3) is correct.
  2. Step 2: Check the dtype argument syntax

    The correct keyword is dtype, and the value can be int or 'int'.
  3. Final Answer:

    np.ones((2, 3), dtype=int) -> Option A
  4. Quick Check:

    Shape as tuple and dtype keyword correct [OK]
Hint: Use tuple for shape and 'dtype' keyword [OK]
Common Mistakes:
  • Using list instead of tuple for shape
  • Using 'type' instead of 'dtype'
  • Passing shape as separate arguments
3. What is the output of the following code?
import numpy as np
arr = np.ones((2, 2), dtype=float)
print(arr)
medium
A. [[1 1] [1 1]]
B. [[1. 1.] [1. 1.]]
C. [[1 1 1] [1 1 1]]
D. [[0. 0.] [0. 0.]]

Solution

  1. Step 1: Understand the shape and dtype

    The shape is (2, 2), so the array has 2 rows and 2 columns. The dtype is float, so elements are floats.
  2. Step 2: Predict the printed output

    Each element is 1.0, printed as 1. with a decimal point. So the output is a 2x2 array of 1.0 values.
  3. Final Answer:

    [[1. 1.] [1. 1.]] -> Option B
  4. Quick Check:

    Shape (2,2) and float type means 1.0 values [OK]
Hint: Float dtype shows decimal points in output [OK]
Common Mistakes:
  • Confusing integer and float output formats
  • Misreading shape as (2,3)
  • Expecting zeros instead of ones
4. Identify the error in this code snippet:
import numpy as np
arr = np.ones(3, 4)
print(arr)
medium
A. print() cannot display numpy arrays
B. Missing dtype argument
C. np.ones() cannot create 2D arrays
D. Shape must be a tuple, so use np.ones((3, 4))

Solution

  1. Step 1: Check the shape argument

    The code passes two separate arguments 3 and 4, but np.ones() expects a single shape argument as a tuple.
  2. Step 2: Correct the shape argument

    Use np.ones((3, 4)) to create a 3 by 4 array of ones.
  3. Final Answer:

    Shape must be a tuple, so use np.ones((3, 4)) -> Option D
  4. Quick Check:

    Shape argument must be tuple [OK]
Hint: Shape is always a tuple in np.ones() [OK]
Common Mistakes:
  • Passing shape as separate arguments
  • Forgetting parentheses around shape tuple
  • Thinking dtype is mandatory
5. You want to create a 3D mask array of shape (2, 3, 4) filled with ones of boolean type using np.ones(). Which code snippet correctly does this?
hard
A. np.ones((2, 3, 4), type=bool)
B. np.ones([2, 3, 4], type=bool)
C. np.ones((2, 3, 4), dtype=bool)
D. np.ones(2, 3, 4, dtype=bool)

Solution

  1. Step 1: Confirm the shape argument format

    The shape must be a tuple (2, 3, 4) to create a 3D array.
  2. Step 2: Use correct dtype keyword and value

    The dtype keyword must be dtype and the value bool to create a boolean array.
  3. Final Answer:

    np.ones((2, 3, 4), dtype=bool) -> Option C
  4. Quick Check:

    Tuple shape and dtype=bool correct [OK]
Hint: Use tuple shape and dtype=bool for boolean arrays [OK]
Common Mistakes:
  • Using list instead of tuple for shape
  • Using 'type' instead of 'dtype'
  • Passing shape as separate arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard