0
0
NumPydata~20 mins

Boolean type in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Boolean Array Comparison
What is the output of this code snippet using NumPy Boolean arrays?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = arr > 3
print(result)
A[True True True False False]
B[False False False True True]
C[False False False False False]
D[True True True True True]
Attempts:
2 left
💡 Hint
Think about which numbers in the array are greater than 3.
data_output
intermediate
2:00remaining
Count True Values in Boolean Array
What is the output of this code that counts True values in a NumPy Boolean array?
NumPy
import numpy as np
bool_arr = np.array([True, False, True, True, False])
count = np.sum(bool_arr)
print(count)
A5
B2
C3
D0
Attempts:
2 left
💡 Hint
Sum of Boolean True values counts how many Trues are in the array.
🔧 Debug
advanced
2:00remaining
Identify the Error in Boolean Indexing
What error does this code raise when trying to index a NumPy array with a Boolean list?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40])
index = [True, False, True]
print(arr[index])
AValueError: operands could not be broadcast together with shapes (4,) (3,)
BTypeError: list indices must be integers or slices, not bool
CNo error, prints [10 30]
DIndexError: boolean index did not match indexed array along dimension 0; dimension is 4 but corresponding boolean dimension is 3
Attempts:
2 left
💡 Hint
Check if the Boolean list length matches the array length.
visualization
advanced
3:00remaining
Visualize Boolean Mask on Data
Which option shows the correct Boolean mask applied to filter values greater than 50 from this NumPy array?
NumPy
import numpy as np
import matplotlib.pyplot as plt
values = np.array([10, 55, 30, 70, 45, 90])
mask = values > 50
filtered = values[mask]
plt.bar(range(len(filtered)), filtered)
plt.show()
ABar chart with bars at heights 55, 70, 90
BBar chart with bars at heights 10, 30, 45
CBar chart with bars at heights 10, 55, 30, 70, 45, 90
DEmpty bar chart with no bars
Attempts:
2 left
💡 Hint
The mask selects values greater than 50 only.
🧠 Conceptual
expert
2:30remaining
Boolean Type Memory Usage in NumPy
Which statement correctly describes the memory usage of a NumPy array with dtype=bool compared to dtype=int8?
AA bool array uses 1 byte per element, same as int8, because NumPy stores bools as bytes.
BA bool array uses 1 bit per element, so it uses 8 times less memory than int8.
CA bool array uses variable memory depending on the values stored.
DA bool array uses 4 bytes per element, more than int8 which uses 1 byte.
Attempts:
2 left
💡 Hint
Consider how NumPy stores Boolean values internally.