Bird
Raised Fist0
NumPydata~20 mins

NumPy and scientific computing ecosystem - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of NumPy broadcasting with different shapes
What is the output of the following code snippet?
NumPy
import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + arr2
print(result)
A
[[11 22 33]
 [14 25 36]]
BRaises ValueError due to shape mismatch
C
[[10 20 30]
 [10 20 30]]
D
[[11 22 33]
 [14 25 36]
 [11 22 33]]
Attempts:
2 left
💡 Hint
Recall how NumPy broadcasts arrays with compatible shapes.
❓ data_output
intermediate
2:00remaining
Shape and size of a reshaped NumPy array
Given the code below, what are the shape and size of the array 'reshaped'?
NumPy
import numpy as np

arr = np.arange(24)
reshaped = arr.reshape(2, 3, 4)
print(reshaped.shape, reshaped.size)
A(2, 3, 4) 24
B(3, 4, 2) 24
C(2, 4, 3) 12
D(24,) 24
Attempts:
2 left
💡 Hint
The reshape method changes the shape but does not change the total number of elements.
🔧 Debug
advanced
2:00remaining
Identify the error in NumPy matrix multiplication
What error will the following code raise when executed?
NumPy
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([5, 6, 7])
C = np.dot(A, B)
print(C)
ATypeError: unsupported operand type(s) for *: 'int' and 'list'
BNo error, output: [17 39]
CIndexError: index out of bounds
DValueError: shapes (2,2) and (3,) not aligned: 2 (dim 1) != 3 (dim 0)
Attempts:
2 left
💡 Hint
Check the shapes of the arrays before matrix multiplication.
❓ visualization
advanced
3:00remaining
Plotting a 2D Gaussian distribution using NumPy and Matplotlib
Which option produces a correct 2D heatmap of a Gaussian distribution centered at (0,0)?
NumPy
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2))
plt.imshow(Z, extent=[-3,3,-3,3], origin='lower')
plt.colorbar()
plt.show()
ARaises NameError due to undefined variable Z
BDisplays a checkerboard pattern with random colors
CDisplays a smooth circular heatmap with highest intensity at center
DDisplays a flat image with uniform color
Attempts:
2 left
💡 Hint
The Gaussian function decreases smoothly from the center.
🧠 Conceptual
expert
2:30remaining
Understanding memory layout in NumPy arrays
Which statement about NumPy array memory layout is TRUE?
AFortran-contiguous arrays store rows sequentially, while C-contiguous arrays store columns sequentially.
BC-contiguous arrays store rows sequentially in memory, while Fortran-contiguous arrays store columns sequentially.
CNumPy arrays always store data in column-major order regardless of flags.
DMemory layout does not affect performance in NumPy operations.
Attempts:
2 left
💡 Hint
Think about how C and Fortran languages store multi-dimensional arrays.

Practice

(1/5)
1. What is the main purpose of NumPy in scientific computing?
easy
A. To create graphical user interfaces
B. To write web applications
C. To manage databases
D. To provide fast and efficient array operations for numerical data

Solution

  1. Step 1: Understand NumPy's role

    NumPy is designed to handle numerical data efficiently using arrays.
  2. Step 2: Compare with other options

    Creating graphical user interfaces, managing databases, and writing web applications are unrelated to NumPy's purpose in scientific computing.
  3. Final Answer:

    To provide fast and efficient array operations for numerical data -> Option D
  4. Quick Check:

    NumPy = fast numerical arrays [OK]
Hint: NumPy is all about fast number arrays [OK]
Common Mistakes:
  • Confusing NumPy with GUI or web tools
  • Thinking NumPy manages databases
  • Assuming NumPy is for general programming
2. Which of the following is the correct way to import NumPy with its common alias?
easy
A. import np as numpy
B. import numpy as np
C. from numpy import *
D. import numpy as numpy

Solution

  1. Step 1: Recall standard import syntax

    The common and recommended way is 'import numpy as np' for short alias.
  2. Step 2: Check other options

    'import np as numpy' has invalid syntax, 'import numpy as numpy' uses a redundant alias, 'from numpy import *' imports all without the np alias.
  3. Final Answer:

    import numpy as np -> Option B
  4. Quick Check:

    Standard alias = np [OK]
Hint: Use 'import numpy as np' for short and clear code [OK]
Common Mistakes:
  • Using wrong alias syntax
  • Importing all without alias
  • Confusing alias and module names
3. What will be the output of this code?
import numpy as np
arr = np.array([1, 2, 3])
print(arr * 2)
medium
A. [2 4 6]
B. [1 2 3 1 2 3]
C. [3 4 5]
D. Error: unsupported operand

Solution

  1. Step 1: Understand array multiplication

    Multiplying a NumPy array by a number multiplies each element by that number.
  2. Step 2: Calculate element-wise multiplication

    Each element in [1, 2, 3] multiplied by 2 gives [2, 4, 6].
  3. Final Answer:

    [2 4 6] -> Option A
  4. Quick Check:

    Array * scalar = element-wise multiply [OK]
Hint: Array * number multiplies each element [OK]
Common Mistakes:
  • Thinking it concatenates arrays
  • Expecting addition instead of multiplication
  • Confusing with Python list behavior
4. Identify the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3])
print(arr + '2')
medium
A. SyntaxError due to wrong print statement
B. It will add 2 to each element
C. TypeError because you cannot add string to array
D. It will concatenate '2' to each element

Solution

  1. Step 1: Check operation types

    Adding a string '2' to a numeric NumPy array causes a type error.
  2. Step 2: Understand error cause

    NumPy cannot add string to integer array elements directly.
  3. Final Answer:

    TypeError because you cannot add string to array -> Option C
  4. Quick Check:

    Array + string = TypeError [OK]
Hint: NumPy arrays need matching types for operations [OK]
Common Mistakes:
  • Expecting automatic conversion
  • Thinking it concatenates strings
  • Ignoring type mismatch errors
5. You have two NumPy arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

How do you create a new array that contains the element-wise sum of a and b?
hard
A. np.add(a, b)
B. a + b + 1
C. np.concatenate((a, b))
D. a * b

Solution

  1. Step 1: Understand element-wise addition

    NumPy's np.add function adds arrays element-wise.
  2. Step 2: Check other options

    'a + b + 1' adds an extra 1, np.concatenate((a, b)) joins arrays end-to-end, 'a * b' multiplies elements pairwise.
  3. Final Answer:

    np.add(a, b) -> Option A
  4. Quick Check:

    np.add = element-wise sum [OK]
Hint: Use np.add for clear element-wise addition [OK]
Common Mistakes:
  • Using + 1 mistakenly
  • Confusing concatenate with add
  • Using multiplication instead of addition
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