Essential NumPy Interview Quiz Quiz

Test your basic knowledge of NumPy with these easy interview questions. This quiz covers core NumPy concepts such as arrays, broadcasting, data types, numerical computation, memory optimization, and handling missing values.

  1. NumPy Basics

    What is the primary purpose of NumPy in scientific computing with Python?

    1. It provides efficient multi-dimensional array operations and mathematical functions.
    2. It creates visual plots and charts.
    3. It manages database records.
    4. It helps write HTML web pages.

    Explanation: NumPy is designed for fast numerical computations and array processing in Python, making it highly suitable for scientific computing. While NumPy does not create visualizations, manage databases, or work with web pages directly, its core strength lies in array manipulation and computation. Options mentioning HTML or databases are unrelated to NumPy's functionality. Visualization is handled by other libraries.

  2. Arrays vs. Lists

    Which advantage do NumPy arrays have over regular Python lists when performing mathematical operations?

    1. Python lists automatically support all mathematical operations.
    2. Python lists save more memory than NumPy arrays.
    3. NumPy arrays execute mathematical operations faster and more efficiently.
    4. NumPy arrays can hold mixed data types easily.

    Explanation: NumPy arrays are optimized for numerical computation, making them both faster and more memory-efficient for math operations compared to Python lists. Regular Python lists do not support vectorized operations or automatic broadcasting. NumPy arrays are homogeneous, so they do not hold mixed types as easily as lists. Memory usage is typically more optimal in NumPy arrays than in lists.

  3. Data Types in NumPy

    Why is selecting the appropriate data type (dtype) important when creating NumPy arrays?

    1. The dtype is not significant in NumPy arrays.
    2. The dtype affects both memory usage and computational performance.
    3. Only integer dtypes are available in NumPy.
    4. The dtype enables use of HTML tags in arrays.

    Explanation: Choosing the correct dtype can save memory and improve performance, especially with large datasets. NumPy supports many data types, not just integers. The dtype has no connection to HTML tags, and disregarding dtype in scientific computing can lead to unnecessary resource consumption or loss of precision.

  4. Understanding Broadcasting

    What does broadcasting allow you to do in NumPy arrays?

    1. Save arrays directly as audio files.
    2. Perform operations on arrays of different shapes by aligning dimensions automatically.
    3. Display figures and charts in web browsers.
    4. Encrypt array data for security.

    Explanation: Broadcasting is a core concept in NumPy, enabling operations on arrays with different shapes without explicit replication of data. It does not relate to web displays, encryption, or audio file saving. Those options are unrelated to NumPy's computational focus.

  5. Masked Arrays

    Which NumPy function is used to handle missing values by masking specific elements in an array?

    1. np.ma.masked_array()
    2. np.lossy_array()
    3. np.array_missing()
    4. np.null_mask()

    Explanation: The np.ma.masked_array() method creates masked arrays, allowing missing values to be excluded from computations. The other choices are not actual NumPy functions; np.array_missing(), np.lossy_array(), and np.null_mask() do not exist. Only np.ma.masked_array() serves this specific purpose.

  6. Feature Scaling

    What does standardization do to the features in a NumPy array for machine learning?

    1. It sorts all features in ascending order.
    2. It replaces missing values with zeros automatically.
    3. It converts values to their hexadecimal forms.
    4. It transforms data to have mean zero and standard deviation one.

    Explanation: Standardization adjusts features to have a mean of zero and a standard deviation of one, making them comparable for machine learning. Converting to hexadecimal or sorting are different operations, and standardization does not fill missing values with zeros. This helps ensure balanced feature contributions to ML models.

  7. Array Concatenation

    Which function would you use to join two NumPy arrays along an existing axis?

    1. np.connect()
    2. np.merge()
    3. np.concatenate()
    4. np.append_array()

    Explanation: np.concatenate() is the correct NumPy function for joining multiple arrays along a specific axis. np.connect(), np.append_array(), and np.merge() are not valid NumPy functions for this task. Using concatenate keeps array shapes consistent and efficient.

  8. Computing Inverse

    What does the NumPy function np.linalg.inv() compute when given a square matrix?

    1. It counts the number of nonzero elements in the matrix.
    2. It computes the inverse of the matrix.
    3. It transposes the matrix.
    4. It finds the largest value in the matrix.

    Explanation: np.linalg.inv() returns the inverse of a square matrix if it exists, which is important for solving linear equations. Transposing (option 3) or finding maxima (option 2) are different operations performed with other functions. Counting nonzero elements is unrelated to matrix inversion.

  9. Numerical Stability

    Why is np.float64 generally preferred over np.float32 in NumPy for numerical stability?

    1. np.float32 is not supported in NumPy.
    2. np.float64 automatically sorts array elements.
    3. np.float64 saves more memory than np.float32.
    4. np.float64 provides higher precision and reduces rounding errors.

    Explanation: The np.float64 data type has higher precision than np.float32, making it less prone to rounding errors in calculations. It does require more memory, not less. np.float32 is supported in NumPy, and neither data type influences the automatic sorting of elements.

  10. Applying Functions Efficiently

    Which tool lets you apply a custom function element-wise to each value in a NumPy array?

    1. np.parallelize()
    2. np.vectorize()
    3. np.optimize()
    4. np.gridder()

    Explanation: np.vectorize() wraps a Python function so it can be applied efficiently to each element of a NumPy array. np.parallelize(), np.optimize(), and np.gridder() are not valid NumPy functions for element-wise application. This method helps speed up custom computations without explicit loops.