Python for AI Practice Test
Practise the Python you need for AI work: core syntax, data handling with NumPy and pandas, JSON, environments, async calls and safe handling of API keys.
Your progress
Answer all questions, then submit to see your score and explanations.
Question 1
Which library is the standard choice for fast numerical arrays in Python?
Question 2
What is a pandas DataFrame?
Question 3
What does [x * 2 for x in range(3)] produce?
Question 4
Which function turns a JSON string into a Python dictionary?
Question 5
What is a virtual environment for?
Question 6
What does len({"a": 1, "b": 2}) return?
Question 7
What does scikit-learn's train_test_split do?
Question 8
Where should an API key for an LLM service be kept?
Question 9
When are async and await most useful in AI apps?
Question 10
name = "Ana". What does f"Hello {name}" give?
Question 11
Pydantic is commonly used in AI apps to:
Question 12
How should code handle an API call that may fail?
Question 13
What does df.head() show by default?
Question 14
What does a NumPy operation like array * 2 do?
Question 15
Why use a requirements.txt or pyproject.toml file?
Please answer all questions before submitting.
Python for AI Practice Test: all 15 questions with answers
Try each question first, then open “Show answer” to check the correct option and explanation.
Q1.Which library is the standard choice for fast numerical arrays in Python?
- A.NumPy
- B.Flask
- C.Requests
- D.Tkinter
Show answer
Correct answer: A. NumPy
NumPy provides n-dimensional arrays and vectorised maths used across the AI ecosystem.
Q2.What is a pandas DataFrame?
- A.An image format
- B.A table-like structure with labelled rows and columns
- C.A neural network layer
- D.A web server
Show answer
Correct answer: B. A table-like structure with labelled rows and columns
DataFrames are the everyday tool for loading, cleaning and exploring tabular data.
Q3.What does [x * 2 for x in range(3)] produce?
- A.[2, 4, 6]
- B.[0, 2, 4]
- C.[0, 1, 2]
- D.[1, 2, 3]
Show answer
Correct answer: B. [0, 2, 4]
range(3) gives 0, 1, 2, and each is doubled.
Q4.Which function turns a JSON string into a Python dictionary?
- A.json.dumps
- B.json.loads
- C.str.split
- D.dict.keys
Show answer
Correct answer: B. json.loads
json.loads parses a string. json.dumps does the reverse.
Q5.What is a virtual environment for?
- A.Running Python in the cloud only
- B.Keeping each project's package versions isolated
- C.Making code run faster
- D.Hiding code from other users
Show answer
Correct answer: B. Keeping each project's package versions isolated
venv (or tools like uv and conda) stops one project's dependencies from breaking another's.
Q6.What does len({"a": 1, "b": 2}) return?
- A.1
- B.2
- C.3
- D.4
Show answer
Correct answer: B. 2
len of a dictionary counts its keys.
Q7.What does scikit-learn's train_test_split do?
- A.Trains two models
- B.Splits data into training and test sets
- C.Splits a string into words
- D.Tests the internet connection
Show answer
Correct answer: B. Splits data into training and test sets
It randomly divides data so the model can be evaluated on examples it did not train on.
Q8.Where should an API key for an LLM service be kept?
- A.Hard-coded in the source file
- B.In an environment variable or secrets manager
- C.In a public GitHub README
- D.In the prompt text
Show answer
Correct answer: B. In an environment variable or secrets manager
Keys in code get leaked through version control. Load them from the environment instead.
Q9.When are async and await most useful in AI apps?
- A.For heavy maths on one CPU core
- B.For running many I/O-bound calls, such as API requests, concurrently
- C.For writing comments
- D.For defining classes
Show answer
Correct answer: B. For running many I/O-bound calls, such as API requests, concurrently
While one request waits on the network, others can proceed.
Q10.name = "Ana". What does f"Hello {name}" give?
- A.Hello {name}
- B.Hello Ana
- C.Hello name
- D.An error
Show answer
Correct answer: B. Hello Ana
f-strings insert the value of expressions inside braces.
Q11.Pydantic is commonly used in AI apps to:
- A.Draw charts
- B.Validate data against typed models, for example structured LLM output
- C.Train neural networks
- D.Compress files
Show answer
Correct answer: B. Validate data against typed models, for example structured LLM output
Defining a Pydantic model and validating the LLM's JSON catches missing or wrong fields early.
Q12.How should code handle an API call that may fail?
- A.Ignore errors
- B.Wrap it in try/except and retry or report the error
- C.Restart the computer
- D.Use print statements only
Show answer
Correct answer: B. Wrap it in try/except and retry or report the error
Network calls fail. Catch the error, retry with backoff where it makes sense, and log what happened.
Q13.What does df.head() show by default?
- A.The last row
- B.The first 5 rows
- C.The column names only
- D.The whole DataFrame
Show answer
Correct answer: B. The first 5 rows
head() previews the first five rows; head(10) shows ten.
Q14.What does a NumPy operation like array * 2 do?
- A.Repeats the array twice
- B.Multiplies every element by 2 in one vectorised step
- C.Raises an error
- D.Adds 2 to the length
Show answer
Correct answer: B. Multiplies every element by 2 in one vectorised step
Vectorised operations apply to every element without a Python loop, which is much faster.
Q15.Why use a requirements.txt or pyproject.toml file?
- A.To store passwords
- B.To record dependencies so others can install the same versions
- C.To speed up Python
- D.To hold training data
Show answer
Correct answer: B. To record dependencies so others can install the same versions
Pinned dependencies make the project reproducible on another machine or server.