Job skill test · free, with answers

Python Basics Test

This Python test has 25 multiple-choice questions, most of which show a short Python 3 snippet and ask what it prints. They cover slicing, lists and dictionaries, mutability and copying, comprehensions, functions and exceptions: the basics checked in coding rounds for developers, data analysts and campus hires. Every snippet has been run to confirm its output. Allow 20 minutes and try not to run the code yourself.

  • 25 questions
  • 20 minutes
  • 10 easy · 10 medium · 5 hard

Topics: Operators · Strings and slicing · Lists · Dictionaries · Mutability · Comprehensions · Functions · Exceptions

Answer key: all 25 questions with answers and explanations
  1. 1. What does this Python 3 code print? print(len([1, [2, 3], 4]))

    1. A. 4
    2. B. 3
    3. C. 2
    4. D. An error

    Answer: B. 3

    The list has three elements: 1, the inner list [2, 3], and 4. len counts only top-level elements, so the inner list counts as one.

  2. 2. What does this Python 3 code print? s = 'interview' print(s[1:4])

    1. A. nte
    2. B. int
    3. C. nter
    4. D. ter

    Answer: A. nte

    Slicing s[1:4] starts at index 1 and stops before index 4. The characters at indexes 1, 2 and 3 of "interview" are n, t and e.

  3. 3. What does this Python 3 code print? word = 'python' print(word[::-1])

    1. A. python
    2. B. pytho
    3. C. nohtyp
    4. D. n

    Answer: C. nohtyp

    A slice with step -1 walks the string from the end to the start, so [::-1] returns the string reversed: "nohtyp".

  4. 4. What does this Python 3 code print? print(7 // 2, 7 % 2, 7 / 2)

    1. A. 3.5 1 3.5
    2. B. 3 1 3
    3. C. 4 1 3.5
    4. D. 3 1 3.5

    Answer: D. 3 1 3.5

    // is floor division (3), % is the remainder (1), and / is true division, which always returns a float in Python 3 (3.5).

  5. 5. What does this Python 3 code print? print(2 ** 3 ** 2)

    1. A. 64
    2. B. 36
    3. C. 512
    4. D. 12

    Answer: C. 512

    The power operator is right-associative, so 2 ** 3 ** 2 means 2 ** (3 ** 2) = 2 ** 9 = 512, not (2 ** 3) ** 2 = 64.

  6. 6. What does this Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)

    1. A. [1, 2, 3, 4]
    2. B. [1, 2, 3]
    3. C. [4]
    4. D. [1, 2, 3, [4]]

    Answer: A. [1, 2, 3, 4]

    b = a does not copy the list; both names refer to the same list object. Appending through b changes the one list, so printing a shows [1, 2, 3, 4].

  7. 7. What does this Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(len(a))

    1. A. 4
    2. B. 3
    3. C. 1
    4. D. An error

    Answer: B. 3

    a[:] creates a new (shallow) copy of the list. Appending to the copy b does not affect a, which still has 3 elements.

  8. 8. What does this Python 3 code print? def add(x, items=[]): items.append(x) return items add(1) print(add(2))

    1. A. [2]
    2. B. [1]
    3. C. [1, 2]
    4. D. [2, 1]

    Answer: C. [1, 2]

    A default argument is created once, when the function is defined, not on each call. The same list is reused, so the first call leaves [1] in it and the second call returns [1, 2]. Use items=None and create the list inside instead.

  9. 9. What does this Python 3 code print? print([x * x for x in range(5) if x % 2 == 0])

    1. A. [0, 1, 4, 9, 16]
    2. B. [1, 9]
    3. C. [0, 4, 16]
    4. D. [4, 16]

    Answer: C. [0, 4, 16]

    range(5) gives 0 to 4. The condition keeps even numbers 0, 2 and 4, and each is squared: [0, 4, 16].

  10. 10. What does this Python 3 code print? d = {'a': 1, 'b': 2} d['c'] = 3 print(list(d.keys()))

    1. A. ['a', 'b', 'c']
    2. B. ['c', 'a', 'b']
    3. C. ['a', 'b']
    4. D. [1, 2, 3]

    Answer: A. ['a', 'b', 'c']

    Assigning to a new key adds it to the dictionary. Since Python 3.7, dictionaries keep insertion order, so the keys come out as a, b, c.

  11. 11. What does this Python 3 code print? d = {'a': 1, 'b': 2} print(d.get('z', 0))

    1. A. None
    2. B. KeyError
    3. C. z
    4. D. 0

    Answer: D. 0

    get returns the value for the key if it exists, otherwise the default given as the second argument. "z" is missing, so it returns 0 instead of raising KeyError.

  12. 12. What happens when this Python 3 code runs? t = (1, 2, 3) t[0] = 9 print(t)

    1. A. It prints (9, 2, 3)
    2. B. It prints (1, 2, 3)
    3. C. It raises IndexError
    4. D. It raises TypeError

    Answer: D. It raises TypeError

    Tuples are immutable, so assigning to t[0] raises TypeError ("tuple object does not support item assignment") before print runs.

  13. 13. What does this Python 3 code print? print(bool([]), bool('0'))

    1. A. False False
    2. B. True True
    3. C. True False
    4. D. False True

    Answer: D. False True

    An empty list is falsy, so bool([]) is False. The string "0" is not empty, and any non-empty string is truthy, so bool("0") is True.

  14. 14. What does this Python 3 code print (each value on its own line)? try: x = int('5') except ValueError: print('A') else: print('B') finally: print('C')

    1. A. A, then C
    2. B. B, then C
    3. C. C only
    4. D. A, then B, then C

    Answer: B. B, then C

    int("5") succeeds, so the except block is skipped. The else block runs only when no exception occurred, printing B. The finally block always runs, printing C.

  15. 15. Which exception does this Python 3 code raise? print(int('abc'))

    1. A. TypeError
    2. B. ValueError
    3. C. 0
    4. D. NameError

    Answer: B. ValueError

    int() receives a string, which is the right type, but its content is not a valid number. A value of the right type with a bad value raises ValueError.

  16. 16. What does this Python 3 code print? print(len({1, 2, 2, 3, 3, 3}))

    1. A. 6
    2. B. 4
    3. C. 3
    4. D. 1

    Answer: C. 3

    A set stores each value only once. The duplicates collapse, leaving {1, 2, 3}, so the length is 3.

  17. 17. What does this Python 3 code print? print(sorted(['banana', 'Apple', 'cherry']))

    1. A. ['Apple', 'banana', 'cherry']
    2. B. ['banana', 'cherry', 'Apple']
    3. C. ['cherry', 'banana', 'Apple']
    4. D. ['Apple', 'cherry', 'banana']

    Answer: A. ['Apple', 'banana', 'cherry']

    Strings sort by character code. Uppercase letters come before lowercase ones, so "Apple" is first, then "banana" and "cherry". Here that also matches alphabetical order.

  18. 18. What does this Python 3 code print? x = [1, 2, 3, 4, 5] print(x[-2:])

    1. A. [5]
    2. B. [1, 2]
    3. C. [4]
    4. D. [4, 5]

    Answer: D. [4, 5]

    Negative indexes count from the end: -2 is the second-last element. x[-2:] takes from there to the end, giving [4, 5].

  19. 19. What does this Python 3 code print? print({k: v for k, v in zip('abc', range(3))})

    1. A. {'a': 0, 'b': 1, 'c': 2}
    2. B. {'a': 1, 'b': 2, 'c': 3}
    3. C. {0: 'a', 1: 'b', 2: 'c'}
    4. D. [('a', 0), ('b', 1), ('c', 2)]

    Answer: A. {'a': 0, 'b': 1, 'c': 2}

    zip pairs the characters of "abc" with 0, 1 and 2. The dictionary comprehension turns each pair into key: value, giving {"a": 0, "b": 1, "c": 2}.

  20. 20. What does this Python 3 code print? print(0.1 + 0.2 == 0.3)

    1. A. True
    2. B. None
    3. C. 0.3
    4. D. False

    Answer: D. False

    Floats are stored in binary, and 0.1 and 0.2 cannot be represented exactly. Their sum is 0.30000000000000004, which is not equal to 0.3. Use math.isclose for such comparisons.

  21. 21. What does this Python 3 code print? a = [1] b = [1] print(a == b, a is b)

    1. A. True True
    2. B. True False
    3. C. False False
    4. D. False True

    Answer: B. True False

    == compares values, and both lists contain [1], so it is True. "is" checks whether both names point to the same object; a and b are two separate lists, so it is False.

  22. 22. What does this Python 3 code print? fs = [lambda: i for i in range(3)] print([f() for f in fs])

    1. A. [0, 1, 2]
    2. B. [0, 0, 0]
    3. C. [2, 2, 2]
    4. D. [1, 2, 3]

    Answer: C. [2, 2, 2]

    Each lambda looks up i when it is called, not when it is created. By the time the functions are called, the loop has finished and i is 2, so all three return 2. Writing lambda i=i: i would capture each value.

  23. 23. What does this Python 3 code print? print(list(map(lambda n: n * 2, filter(lambda n: n > 1, [1, 2, 3]))))

    1. A. [2, 4, 6]
    2. B. [4, 6]
    3. C. [2, 3]
    4. D. [4]

    Answer: B. [4, 6]

    filter keeps the numbers greater than 1, which are 2 and 3. map then doubles each of them, giving [4, 6].

  24. 24. What does this Python 3 code print? grid = [[0] * 2] * 2 grid[0][0] = 1 print(grid)

    1. A. [[1, 0], [0, 0]]
    2. B. [[1, 0], [1, 0]]
    3. C. [[1, 1], [0, 0]]
    4. D. [[1, 1], [1, 1]]

    Answer: B. [[1, 0], [1, 0]]

    Multiplying the outer list copies the reference, not the inner list, so both rows are the same list object. Changing grid[0][0] shows up in both rows. Use [[0] * 2 for _ in range(2)] to get independent rows.

  25. 25. What does this Python 3 code print? g = (x for x in range(3)) print(sum(g), sum(g))

    1. A. 3 0
    2. B. 3 3
    3. C. 0 3
    4. D. 3 None

    Answer: A. 3 0

    A generator can be consumed only once. The first sum reads 0, 1 and 2 and returns 3. The generator is then exhausted, so the second sum gets no values and returns 0.

How this test is scored

Each correct answer earns one mark with no negative marking, for a maximum of 25. Scoring 20 or more means your Python basics are interview-ready; if you scored lower, run the snippets you missed and change them until the behaviour makes sense.

On the result screen, below 50% means the topics need more practice, 50–75% is a good base to build on, and above 75% means you are ready for questions like these. These bands are for your own practice only.

How to prepare

  • Read each snippet line by line and track every variable on paper before choosing an answer; this is exactly what output questions test.
  • Learn which types are mutable (list, dict, set) and which are not (int, str, tuple), and what assignment, slicing and copy each do.
  • Practise slicing with negative indexes and steps on a short string until [::-1], [-2:] and [1:4] feel automatic.
  • Know the common exceptions by name (ValueError, TypeError, KeyError, IndexError) and when try, except, else and finally each run.
  • Rewrite a few loops as list and dictionary comprehensions, and learn why a generator can only be used once.

Tests are one part of the process. Read the interview preparation guide and practise answering out loud with the free interview practice tool.

FAQ

Questions about the Python Basics Test

Freshers are usually asked to predict the output of short code, explain the difference between a list and a tuple, reverse a string, remove duplicates from a list, count words with a dictionary, and handle an exception. Mutable default arguments, shallow copies and list comprehensions are favourite follow-ups because they show whether you understand how Python really works.

Python 3 only. Every snippet was run with Python 3 to confirm the answer. Some answers differ in Python 2; for example 7 / 2 gives 3 in Python 2 but 3.5 in Python 3, and dictionaries only keep insertion order from Python 3.7 onwards. Python 2 is no longer supported, so interviews use Python 3.

Take it once without running any code. Then, for every question you got wrong, paste the snippet into a Python shell, change one thing at a time and see how the output changes. This builds a mental model you can use for new questions, which matters more than remembering these particular answers.

Show your skills with a personal website

Put your resume, projects and certificates on one link recruiters can open on any phone — live in about five minutes.

● Live in 5 minutes · free to start · no auto-renew

Recruiters Google you before the interview

Get a page that shows up: your experience, projects and contact details at your own link. Live in minutes.

Start free
Chat on WhatsApp