All subjectsPython & AI · 15 questions · 5 junior / 7 mid / 3 senior

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 quiz15 questions in the bank · 10 per round
Level
Question type
Mode

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.

  1. juniorPredict the output

    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).

  2. juniorPredict the output

    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.

  3. midPredict the output

    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.

  4. juniorFind the bug

    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.

  5. juniorPredict the output

    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.

  6. midPredict the output

    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.

  7. juniorConcepts

    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.

  8. midPredict the output

    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.

  9. midFind the bug

    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).

  10. midPredict the output

    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.

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp