0
0
NumPydata~20 mins

Complex number type in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Complex Number Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy complex array addition
What is the output of this code that adds two numpy arrays of complex numbers?
NumPy
import numpy as np
arr1 = np.array([1+2j, 3+4j])
arr2 = np.array([5+6j, 7+8j])
result = arr1 + arr2
print(result)
Aarray([6.+6.j, 10.+12.j])
Barray([6.+8.j, 10.+10.j])
Carray([6.+8.j, 10.+12.j])
Darray([6.+8.j, 10.+14.j])
Attempts:
2 left
💡 Hint
Remember how complex numbers add: real parts add, imaginary parts add.
data_output
intermediate
1:30remaining
Shape and dtype of complex numpy array
What is the shape and data type of this numpy array?
NumPy
import numpy as np
arr = np.array([[1+1j, 2+2j], [3+3j, 4+4j]])
print(arr.shape, arr.dtype)
A(2, 2) complex128
B(4,) complex128
C(2, 2) complex64
D(2, 2) float64
Attempts:
2 left
💡 Hint
Check the shape of the nested list and default complex dtype in numpy.
🔧 Debug
advanced
1:30remaining
Identify the error in complex number array creation
What error does this code raise when trying to create a numpy array of complex numbers?
NumPy
import numpy as np
arr = np.array([1+2, 3+4j])
ANo error, creates array with dtype complex128
BNo error, creates array with mixed types
CValueError: setting an array element with a sequence
DTypeError: unsupported operand type(s) for +: 'int' and 'int'
Attempts:
2 left
💡 Hint
Check how numpy handles mixed numeric types including complex and int.
visualization
advanced
2:30remaining
Visualizing real and imaginary parts of complex array
Which option correctly plots the real and imaginary parts of this complex numpy array?
NumPy
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([1+2j, 3+4j, 5+6j])
plt.plot(arr.real, label='Real')
plt.plot(arr.imag, label='Imaginary')
plt.legend()
plt.show()
AScatter plot with points at (1,2), (3,4), (5,6)
BPie chart showing ratio of real to imaginary parts
CBar chart with bars for magnitude of each complex number
DLine plot with two lines: real parts [1,3,5] and imaginary parts [2,4,6]
Attempts:
2 left
💡 Hint
Look at what plt.plot(arr.real) and plt.plot(arr.imag) produce.
🧠 Conceptual
expert
2:00remaining
Effect of numpy complex dtype on arithmetic precision
Which statement about numpy complex dtypes is correct?
Acomplex128 uses 64 bits total, 32 bits each for real and imaginary parts
Bcomplex64 uses 32 bits for real and 32 bits for imaginary parts, offering less precision than complex128
Ccomplex64 and complex128 have the same precision but differ in memory layout
Dcomplex128 uses 128 bits for the real part only, imaginary part is stored separately
Attempts:
2 left
💡 Hint
Recall how floating point precision relates to complex number storage.