Python & AI quiz and interview questions
Python is the language of AI and automation, and interviews now mix classic Python questions with AI basics. This quiz covers slicing, comprehensions, mutable defaults and common errors, then the LLM ideas every developer is asked about: tokens, RAG, embeddings and temperature.
Python & AI: key concepts
- Lists, tuples and dicts
- Lists are mutable, tuples are immutable, dicts map keys to values. dict.get(key, default) avoids a KeyError.
- Division
- / always returns a float (7 / 2 = 3.5), // is floor division (3) and % is the remainder (1).
- References
- b = a doesn't copy a list — both names point to the same list. Use a.copy() or list(a) for a copy.
- Mutable default arguments
- Default values are created once, when the function is defined. Use None as the default and create the list inside the function.
- LLMs and tokens
- Language models read and write tokens (word pieces). Context limits and API pricing are counted in tokens.
- RAG and embeddings
- Embeddings turn text into vectors so similar meanings are close together. RAG retrieves the most relevant chunks of your data and adds them to the prompt, so answers come from your documents.
All 15 Python & AI questions with answers
Try each one first, then open the answer. Making a class test? Use the test maker.
What does this print?
print(7 // 2, 7 % 2, 7 / 2)- A. 3 1 3.5
- B. 3.5 1 3
- C. 3 1 3
- D. 4 1 3.5
Show answer
A. 3 1 3.5 — // is floor division (3), % is the remainder (1) and / always returns a float (3.5).
What does this print?
nums = [10, 20, 30, 40, 50] print(nums[1:4])- A. [10, 20, 30]
- B. [20, 30, 40]
- C. [20, 30, 40, 50]
- D. [10, 20, 30, 40]
Show answer
B. [20, 30, 40] — Slicing [1:4] starts at index 1 and stops before index 4: 20, 30, 40.
What does this print?
def add(item, items=[]): items.append(item) return items print(add(1)) print(add(2))- A. [1] then [2]
- B. [1] then [1, 2]
- C. [1, 2] then [1, 2]
- D. Error
Show answer
B. [1] then [1, 2] — The default list is created once and shared between calls, so the second call appends to the same list. Use items=None and create a new list inside.
What error does this raise?
def greet(name): print("Hi", name)- A. NameError
- B. IndentationError
- C. TypeError
- D. No error
Show answer
B. IndentationError — Python uses indentation to define blocks. The function body must be indented under def.
What does this print?
print(len("Hello World"))- A. 10
- B. 11
- C. 12
- D. 2
Show answer
B. 11 — len counts every character, including the space: 5 + 1 + 5 = 11.
What does this print?
print([x * x for x in range(5) if x % 2 == 0])- A. [0, 4, 16]
- B. [1, 9]
- C. [0, 1, 4, 9, 16]
- D. [4, 16]
Show answer
A. [0, 4, 16] — range(5) gives 0–4. The even ones are 0, 2, 4, and their squares are 0, 4, 16.
What is the main difference between a list and a tuple?
- A. Tuples can only hold numbers
- B. Lists are mutable; tuples are immutable
- C. Tuples are always faster to sort
- D. There is no difference
Show answer
B. Lists are mutable; tuples are immutable — You can append to or change a list. A tuple can't be changed after it's created, so it can be used as a dict key.
What does this print?
d = {"a": 1} print(d.get("b", 0))- A. None
- B. KeyError
- C. 0
- D. 1
Show answer
C. 0 — get returns the default (0) when the key is missing. d["b"] would raise a KeyError.
What happens when this runs?
age = 25 print("Age: " + age)- A. Prints Age: 25
- B. TypeError: can only concatenate str to str
- C. SyntaxError
- D. Prints Age:
Show answer
B. TypeError: can only concatenate str to str — Python won't add a str and an int. Use an f-string (f"Age: {age}") or str(age).
What does this print?
a = [1, 2, 3] b = a b.append(4) print(a)- A. [1, 2, 3]
- B. [1, 2, 3, 4]
- C. [4]
- D. Error
Show answer
B. [1, 2, 3, 4] — b = a makes b point to the same list, so appending through b changes a too.
In large language models, what is a token?
- A. A password for the API
- B. A chunk of text (often part of a word) that the model reads and writes
- C. A whole paragraph
- D. A GPU
Show answer
B. A chunk of text (often part of a word) that the model reads and writes — Models process text as tokens. Context-window limits and API pricing are both measured in tokens.
What is RAG (retrieval-augmented generation)?
- A. Training a new model from scratch
- B. Finding relevant chunks of your own data and adding them to the prompt so the model answers from them
- C. A type of GPU
- D. Compressing prompts
Show answer
B. Finding relevant chunks of your own data and adding them to the prompt so the model answers from them — RAG lets a chatbot answer from your documents or database without retraining — e.g. a support bot that quotes your policies.
What does lowering an LLM's temperature do?
- A. Makes responses faster
- B. Makes output more focused and predictable
- C. Makes output more creative and random
- D. Reduces cost
Show answer
B. Makes output more focused and predictable — Low temperature picks the most likely tokens — good for extraction and classification. Higher temperature adds variety for creative writing.
What are embeddings used for?
- A. Compressing images
- B. Representing text as vectors so you can search by meaning
- C. Encrypting prompts
- D. Training data labels
Show answer
B. Representing text as vectors so you can search by meaning — Texts with similar meanings get vectors that are close together, which powers semantic search and the retrieval step in RAG.
Because of CPython's GIL, which approach speeds up CPU-heavy work?
- A. More threads
- B. multiprocessing (separate processes)
- C. async/await
- D. Larger lists
Show answer
B. multiprocessing (separate processes) — The GIL lets only one thread run Python bytecode at a time. Threads and asyncio help with I/O; separate processes use multiple CPU cores.
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.