0
0
NumPydata~20 mins

Scalar operations on arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scalar Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of scalar multiplication on a NumPy array
What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 4
print(result)
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 4
print(result)
A[4 8 12]
B[1 2 3 4]
C[5 6 7]
DTypeError
Attempts:
2 left
💡 Hint
Multiplying an array by a number multiplies each element by that number.
data_output
intermediate
2:00remaining
Result of adding a scalar to a 2D NumPy array
What is the resulting array after adding 10 to each element?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = arr + 10
print(result)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = arr + 10
print(result)
A
[[1 2]
 [3 4]
 10]
B
[[10 10]
 [10 10]]
C
[[11 12]
 [13 14]]
DValueError
Attempts:
2 left
💡 Hint
Adding a scalar adds it to every element in the array.
🔧 Debug
advanced
2:00remaining
Identify the error in scalar division of a NumPy array
What error does this code produce?
import numpy as np
arr = np.array([10, 20, 30])
result = arr / 0
print(result)
NumPy
import numpy as np
arr = np.array([10, 20, 30])
result = arr / 0
print(result)
ATypeError
BValueError
CZeroDivisionError
DRuntimeWarning: divide by zero encountered in true_divide
Attempts:
2 left
💡 Hint
Dividing by zero in NumPy arrays triggers a warning, not an exception.
🧠 Conceptual
advanced
2:00remaining
Effect of scalar subtraction on a NumPy array
If you subtract 5 from each element of the array np.array([7, 8, 9]), what is the resulting array?
A[12 13 14]
B[2 3 4]
C[7 8 9 5]
DTypeError
Attempts:
2 left
💡 Hint
Subtracting a scalar subtracts it from every element.
🚀 Application
expert
2:00remaining
Calculate the mean after scalar multiplication
Given the array np.array([2, 4, 6, 8]), what is the mean of the array after multiplying each element by 3?
NumPy
import numpy as np
arr = np.array([2, 4, 6, 8])
result = arr * 3
mean_val = result.mean()
print(mean_val)
A15.0
B10.0
C30.0
D20.0
Attempts:
2 left
💡 Hint
Multiply first, then find the average of the new array.