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.
AI Skills
Before you start
- Questions
- 15
- Time
- 15 minutes
- Format
- Single section
- The timer starts when you press Start. When it reaches zero, your answers are submitted automatically.
- Use the numbered navigator to move between questions, and flag any you want to come back to.
- For typed answers, keep within the word limit. Answers over the limit are marked wrong, as in the real test.
- You can submit early. Your score, the correct answers and explanations appear straight away.
Python for AI Practice Test: all 15 questions with answers
Prefer to work through the test on paper? Answer the questions, then open “Show answer” to check.
Choose the correct answer, A, B, C or D.
Q1.Which library is the standard choice for fast numerical arrays in Python?
- A.Flask
- B.Requests
- C.NumPy
- D.Tkinter
Show answer
Answer: C. 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 neural network layer
- C.A web server
- D.A table-like structure with labelled rows and columns
Show answer
Answer: D. 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.[0, 2, 4]
- B.[2, 4, 6]
- C.[0, 1, 2]
- D.[1, 2, 3]
Show answer
Answer: A. [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
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.Making code run faster
- C.Keeping each project's package versions isolated
- D.Hiding code from other users
Show answer
Answer: C. 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.3
- C.4
- D.2
Show answer
Answer: D. 2
len of a dictionary counts its keys.
Q7.What does scikit-learn's train_test_split do?
- A.Splits data into training and test sets
- B.Trains two models
- C.Splits a string into words
- D.Tests the internet connection
Show answer
Answer: A. 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
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 writing comments
- C.For running many I/O-bound calls, such as API requests, concurrently
- D.For defining classes
Show answer
Answer: C. 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 name
- C.Hello Ana
- D.An error
Show answer
Answer: C. Hello Ana
f-strings insert the value of expressions inside braces.
Q11.Pydantic is commonly used in AI apps to:
- A.Draw charts
- B.Train neural networks
- C.Compress files
- D.Validate data against typed models, for example structured LLM output
Show answer
Answer: D. 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.Wrap it in try/except and retry or report the error
- B.Ignore errors
- C.Restart the computer
- D.Use print statements only
Show answer
Answer: A. 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
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.Raises an error
- C.Multiplies every element by 2 in one vectorised step
- D.Adds 2 to the length
Show answer
Answer: C. 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 speed up Python
- C.To hold training data
- D.To record dependencies so others can install the same versions
Show answer
Answer: D. To record dependencies so others can install the same versions
Pinned dependencies make the project reproducible on another machine or server.