Sharpen your knowledge of Python fundamentals with this quick and focused quiz designed for backend development interview preparation. Ideal for beginners strengthening core skills.
Which of the following is a valid way to assign the integer 5 to a variable in Python?
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.
What is the correct way to define a simple function in Python that takes no arguments and returns '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.
Why is indentation important in Python code blocks, such as for loops or function bodies?
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.
What would be the output of the following code: d = {'a': 2}; print(d['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.
How do you append the value 9 to a Python list named items?
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.
Given s1 = 'Hello' and s2 = 'World', which expression creates 'HelloWorld'?
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.
Which is a valid Python if-statement checking if x equals 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).
If numbers = [10, 20, 30], what is numbers[1]?
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.
Which for loop prints numbers from 0 to 4 in Python?
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.
How do you correctly import the math module in Python?
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.