Essential Python Interview Practice: Backend Basics Quiz

Sharpen your knowledge of Python fundamentals with this quick and focused quiz designed for backend development interview preparation. Ideal for beginners strengthening core skills.

  1. Understanding Python Variables

    Which of the following is a valid way to assign the integer 5 to a variable in Python?

    1. int x = 5
    2. let x = 5
    3. x = 5

    Explanation: In Python, variables are assigned using the equals sign without specifying the type, so 'x = 5' is correct. 'int x = 5' is valid in languages like C, and 'let x = 5' is used in JavaScript, but not in Python.

  2. Defining Functions

    What is the correct way to define a simple function in Python that takes no arguments and returns 'Hello'?

    1. func greet() -> 'Hello'
    2. function greet() { return 'Hello' }
    3. def greet(): return 'Hello'

    Explanation: Python defines functions using 'def', followed by the function name and parentheses. The other options use incorrect syntax: 'function' and curly braces are not Python keywords or syntax, and 'func' is not used this way.

  3. Indentation Rules

    Why is indentation important in Python code blocks, such as for loops or function bodies?

    1. It visually separates code only
    2. It defines block structure
    3. It makes code run faster

    Explanation: Python uses indentation to define code blocks, which is essential for correct program logic. Visual separation is a secondary benefit, and indentation does not affect code execution speed.

  4. Dictionaries and Key Access

    What would be the output of the following code: d = {'a': 2}; print(d['a'])?

    1. Error
    2. 2
    3. 'a'

    Explanation: Accessing a Python dictionary value by its key returns the associated value, so 'd['a']' gives 2. It does not print the key itself or raise an error, as the key exists.

  5. Working with Lists

    How do you append the value 9 to a Python list named items?

    1. items.add(9)
    2. items.append(9)
    3. append(items, 9)

    Explanation: 'items.append(9)' correctly appends 9 to the list. 'append(items, 9)' is not a valid Python function call, and 'items.add(9)' is used for sets, not lists.

  6. String Concatenation

    Given s1 = 'Hello' and s2 = 'World', which expression creates 'HelloWorld'?

    1. concat(s1, s2)
    2. s1 & s2
    3. s1 + s2

    Explanation: The plus operator '+' concatenates strings in Python. The '&' operator is used for bitwise operations, and 'concat' is not a built-in Python function for strings.

  7. If Statement Syntax

    Which is a valid Python if-statement checking if x equals 10?

    1. if x === 10:
    2. if (x = 10)
    3. if x == 10:

    Explanation: 'if x == 10:' is the correct syntax in Python. '==' compares values; single '=' is assignment, and '===' is not valid in Python (it's a JavaScript operator).

  8. List Indexing

    If numbers = [10, 20, 30], what is numbers[1]?

    1. 30
    2. 20
    3. 10

    Explanation: Lists in Python are zero-indexed, so numbers[1] returns the second item, which is 20. 10 is at index 0, and 30 is at index 2.

  9. Loops Over Ranges

    Which for loop prints numbers from 0 to 4 in Python?

    1. for i in range(5): print(i)
    2. for i = 1 to 5: print(i)
    3. for i in 0..4: print(i)

    Explanation: 'for i in range(5): print(i)' is valid Python and produces 0 to 4. The other options do not use Python syntax; 'to' and '..' are not recognized for loops.

  10. Importing Modules

    How do you correctly import the math module in Python?

    1. using math
    2. import math
    3. include math

    Explanation: 'import math' is the correct statement to bring a module into your Python code. 'include' and 'using' are keywords from other languages, not Python.