HomeSubjectsUniversityBlogAbout
210 total MCQsShowing 30 with explanations10 Easy10 Medium10 Hard

About This Topic

Python for AI refers to the language features and libraries, chiefly NumPy, Pandas, Matplotlib and scikit-learn, used to load data, compute and train models. Questions check core Python such as lists, dictionaries, comprehensions, lambda functions and the difference between append() and extend(). NumPy items test array shape, reshaping, broadcasting and vectorized operations, while Pandas questions cover DataFrames, loc versus iloc, handling missing values, merge and groupby() aggregation. You will also meet the scikit-learn fit, predict and transform API, pipelines and train_test_split. Advanced questions discuss the Global Interpreter Lock (GIL), memory efficiency and pickling custom estimators.

Below are 30 practice questions from a pool of 210 Python for AI MCQs, one of 17 topics in AI / Machine Learning & Data Analytics. Each shows the correct answer with an explanation; when you are ready, take a timed quiz to test recall under exam conditions.

Practice Questions

Each question below shows the correct answer with a full explanation. Use these to build conceptual understanding before attempting a timed quiz.

Python for AIEasy

Q1. Which Python library is most commonly used for numerical computations in AI?

  1. A.Tkinter
  2. B.Flask
  3. C.NumPy✓ Correct
  4. D.Django

Explanation

NumPy is the fundamental library for numerical computing in Python, providing support for arrays and mathematical operations.

Report an error in this question

Python for AIEasy

Q2. What does pandas primarily provide?

  1. A.Network socket programming APIs
  2. B.Data structures for data analysis✓ Correct
  3. C.Web development framework tools
  4. D.Game development engine support

Explanation

Pandas provides DataFrame and Series data structures for efficient data manipulation and analysis.

Report an error in this question

Python for AIEasy

Q3. Which library is used for plotting graphs in Python?

  1. A.Pandas
  2. B.NumPy
  3. C.Matplotlib✓ Correct
  4. D.Scikit-learn

Explanation

Matplotlib is the standard Python library for creating static, animated, and interactive visualizations.

Report an error in this question

Python for AIEasy

Q4. How do you import NumPy with an alias?

  1. A.include numpy
  2. B.import np
  3. C.import numpy as num
  4. D.import numpy as np✓ Correct

Explanation

The convention is to import NumPy as np: import numpy as np.

Report an error in this question

Python for AIEasy

Q5. What is a Jupyter Notebook?

  1. A.An interactive computing environment for code and visualizations✓ Correct
  2. B.A production web server for hosting static site content
  3. C.A relational database for storing structured table records
  4. D.A lightweight plain text editor without any execution support

Explanation

Jupyter Notebook is an interactive web-based environment for writing and executing code, visualizing data, and documenting analysis.

Report an error in this question

Python for AIEasy

Q6. Which function creates a NumPy array?

  1. A.np.make()
  2. B.np.list()
  3. C.np.array()✓ Correct
  4. D.np.create()

Explanation

np.array() is used to create a NumPy array from a Python list or tuple.

Report an error in this question

Python for AIEasy

Q7. What does df.head() do in pandas?

  1. A.Sorts the DataFrame by column values
  2. B.Returns the last 5 rows of the frame
  3. C.Deletes the first row of the DataFrame
  4. D.Returns the first 5 rows of the DataFrame✓ Correct

Explanation

df.head() returns the first 5 rows of a pandas DataFrame by default.

Report an error in this question

Python for AIEasy

Q8. Which library provides machine learning algorithms in Python?

  1. A.Pillow
  2. B.Scikit-learn✓ Correct
  3. C.Seaborn
  4. D.Matplotlib

Explanation

Scikit-learn provides simple and efficient tools for data mining and machine learning in Python.

Report an error in this question

Python for AIEasy

Q9. What data type does pandas use for tabular data?

  1. A.List
  2. B.Dictionary
  3. C.DataFrame✓ Correct
  4. D.Array

Explanation

A pandas DataFrame is a 2-dimensional labeled data structure, similar to a spreadsheet or SQL table.

Report an error in this question

Python for AIEasy

Q10. Which operator is used for element-wise multiplication in NumPy?

  1. A.*✓ Correct
  2. B.//
  3. C.&
  4. D.@

Explanation

The * operator performs element-wise multiplication on NumPy arrays. The @ operator is for matrix multiplication.

Report an error in this question

Python for AIMedium

Q11. What is broadcasting in NumPy?

  1. A.A networking feature for sending data to multiple receivers
  2. B.Automatic expansion of arrays with different shapes for arithmetic✓ Correct
  3. C.A type of sorting algorithm for ordering array elements
  4. D.A logging mechanism for recording runtime error messages

Explanation

Broadcasting allows NumPy to perform arithmetic operations on arrays with different shapes by automatically expanding smaller arrays.

Report an error in this question

Python for AIMedium

Q12. Which scikit-learn class is used for train-test splitting?

  1. A.SplitData
  2. B.DataSplitter
  3. C.train_test_split✓ Correct
  4. D.TestTrainDivide

Explanation

sklearn.model_selection.train_test_split is used to split data into training and testing subsets.

Report an error in this question

Python for AIMedium

Q13. What does df.groupby() do in pandas?

  1. A.Renames columns using a provided mapping dict
  2. B.Sorts the DataFrame by index values in order
  3. C.Filters rows based on a boolean condition mask
  4. D.Groups data by one or more columns for aggregation✓ Correct

Explanation

groupby() splits data into groups based on column values, allowing aggregate operations on each group.

Report an error in this question

Python for AIMedium

Q14. Which pandas method fills missing values?

  1. A.fillna()✓ Correct
  2. B.isna()
  3. C.notna()
  4. D.dropna()

Explanation

fillna() replaces NaN/missing values with a specified value or method like forward fill or backward fill.

Report an error in this question

Python for AIMedium

Q15. What is a Python generator?

  1. A.A built-in immutable data type like a list
  2. B.A function that yields values lazily using yield✓ Correct
  3. C.A special class constructor for initialization
  4. D.A comparison-based sorting algorithm method

Explanation

A generator is a function that uses yield to produce values one at a time, enabling memory-efficient iteration.

Report an error in this question

Python for AIMedium

Q16. How do you perform matrix multiplication in NumPy?

  1. A.np.multiply(A, B) function✓ Correct
  2. B.A + B element-wise addition
  3. C.A * B element-wise product
  4. D.np.dot(A, B) or A @ B operator

Explanation

Matrix multiplication uses np.dot() or the @ operator. A * B performs element-wise multiplication.

Report an error in this question

Python for AIMedium

Q17. What does the pandas method .apply() do?

  1. A.Deletes specified rows from the DataFrame index
  2. B.Merges two DataFrames on a common column key
  3. C.Adds a new column to the end of a DataFrame
  4. D.Applies a function along an axis of a DataFrame✓ Correct

Explanation

apply() applies a function to each element, row, or column of a DataFrame or Series.

Report an error in this question

Python for AIMedium

Q18. Which visualization library is built on top of Matplotlib?

  1. A.Pygal
  2. B.Bokeh
  3. C.Seaborn✓ Correct
  4. D.Plotly

Explanation

Seaborn is built on Matplotlib and provides a high-level interface for drawing statistical graphics.

Report an error in this question

Python for AIMedium

Q19. What is the purpose of np.reshape()?

  1. A.To sort the elements of an array in ascending order
  2. B.To change the shape of an array without changing its data✓ Correct
  3. C.To filter elements of an array by a condition mask
  4. D.To delete selected elements from an existing array

Explanation

np.reshape() changes the dimensions of an array while preserving the same data and total number of elements.

Report an error in this question

Python for AIMedium

Q20. What does pickle do in Python?

  1. A.Manages relational database connections
  2. B.Performs numerical matrix calculations
  3. C.Creates interactive charts and plots
  4. D.Serializes and deserializes Python objects✓ Correct

Explanation

Pickle serializes Python objects to byte streams for saving/loading models and data structures.

Report an error in this question

Python for AIHard

Q21. What is the Global Interpreter Lock (GIL) in Python?

  1. A.A file system locking mechanism for preventing concurrent write access
  2. B.A garbage collection tool for automated memory resource management
  3. C.A cryptographic security feature for encrypting sensitive runtime data
  4. D.A mutex preventing multiple threads from executing Python bytecode simultaneously✓ Correct

Explanation

The GIL is a mutex in CPython that prevents multiple threads from executing Python bytecodes at the same time, limiting true parallelism.

Report an error in this question

Python for AIHard

Q22. Which tool is best for parallel processing of large datasets in Python?

  1. A.Flask
  2. B.Django
  3. C.Tkinter
  4. D.Dask✓ Correct

Explanation

Dask provides parallel computing capabilities and can handle datasets larger than memory by breaking them into smaller chunks.

Report an error in this question

Python for AIHard

Q23. What is the difference between deepcopy and copy in Python?

  1. A.deepcopy creates copies of nested objects recursively, copy does not✓ Correct
  2. B.copy is faster and creates a more thorough deep duplication
  3. C.deepcopy only works on list objects and not dictionaries
  4. D.They are completely identical in behavior and performance

Explanation

copy creates a shallow copy (references to nested objects are shared), while deepcopy recursively copies all nested objects.

Report an error in this question

Python for AIHard

Q24. In NumPy, what does np.einsum() do?

  1. A.Performs only element-wise operations on flat one-D arrays
  2. B.Performs Einstein summation for multi-dimensional array operations✓ Correct
  3. C.Creates identity matrices of a specified dimension size
  4. D.Calculates eigenvalues of a square matrix decomposition

Explanation

np.einsum() provides a concise way to express multi-dimensional array operations using Einstein summation convention.

Report an error in this question

Python for AIHard

Q25. What is vectorization in the context of NumPy?

  1. A.Converting natural language text into numeric vectors
  2. B.Performing operations on entire arrays instead of loops✓ Correct
  3. C.Creating scalable vector graphics for visualization
  4. D.A lossless data compression encoding technique

Explanation

Vectorization replaces explicit loops with array operations, leveraging optimized C implementations for significantly faster execution.

Report an error in this question

Python for AIHard

Q26. What does the __slots__ attribute do in Python classes?

  1. A.Defines abstract methods for interface contracts
  2. B.Enables multiple inheritance across class chains
  3. C.Restricts instance attributes and reduces memory usage✓ Correct
  4. D.Creates new class-level methods from descriptors

Explanation

__slots__ restricts which attributes instances can have and avoids the __dict__ overhead, reducing memory usage significantly.

Report an error in this question

Python for AIHard

Q27. Which library provides GPU-accelerated computing for Python ML?

  1. A.CuPy✓ Correct
  2. B.Pandas
  3. C.Matplotlib
  4. D.SciPy

Explanation

CuPy provides GPU-accelerated computing with a NumPy-compatible interface, enabling faster numerical computations on NVIDIA GPUs.

Report an error in this question

Python for AIHard

Q28. What is a context manager in Python and why is it useful in ML?

  1. A.It creates persistent database connections for data loading
  2. B.It manages resources using with statements ensuring proper cleanup✓ Correct
  3. C.It handles asynchronous network requests for API access
  4. D.It manages the training context of machine learning models

Explanation

Context managers (with statements) ensure proper resource acquisition and release, critical for managing file handles, GPU memory, and database connections in ML pipelines.

Report an error in this question

Python for AIHard

Q29. What is the purpose of __call__ in a Python class?

  1. A.Copies the object into a new reference
  2. B.Initializes a new class from a template
  3. C.Destroys the instance and frees memory
  4. D.Makes an instance callable like a function✓ Correct

Explanation

The __call__ method allows class instances to be called like functions, commonly used in PyTorch for defining model forward passes.

Report an error in this question

Python for AIHard

Q30. How does memory mapping (np.memmap) help in handling large datasets?

  1. A.It maps files to memory allowing access without loading the entire file✓ Correct
  2. B.It encrypts data at rest to ensure privacy and compliance
  3. C.It compresses data into smaller archive files for disk savings
  4. D.It duplicates data across servers for redundancy and backups

Explanation

Memory mapping allows accessing large files as if they were in memory without loading everything, enabling processing of datasets larger than available RAM.

Report an error in this question

Ready to test yourself on Python for AI?

Take a timed quiz drawn from 210+ questions on this topic. No signup required — your progress saves in your browser.

Start Python for AI Quiz