Python Basics Quiz for Backend Development Interviews Quiz

Assess your understanding of fundamental Python concepts and coding skills for backend development interview preparation. This quiz covers syntax, data types, control structures, functions, and essential language features relevant to backend programming in Python.

  1. Variable Assignment

    Which of the following statements correctly assigns the integer value 10 to a variable named 'x' in Python?

    1. x = 10
    2. int x = 10
    3. x == 10
    4. x := 10

    Explanation: The correct way to assign the integer 10 to the variable x in Python is using x = 10. The statement int x = 10 is incorrect because variable declaration with a type is not required in Python. The option x == 10 is a comparison, not an assignment. The walrus operator x := 10 is used for assignment expressions in Python 3.8+, but generally assignment should be done with =.

  2. Data Types

    Which built-in data type in Python is best suited to store a collection of unique items?

    1. set
    2. list
    3. dict
    4. tuple

    Explanation: The set data type in Python is designed to store a collection of unique and unordered items. While lists and tuples can contain duplicates, sets automatically remove any repeated elements. Dictionaries (dict) store key-value pairs rather than single values. Tuples, like lists, can also contain duplicates.

  3. Conditional Statements

    How would you write an if statement in Python to check if the variable 'score' is greater than 50?

    1. if score > 50:
    2. if (score > 50)
    3. if score > 50
    4. if score => 50:

    Explanation: The syntax if score > 50: is correct in Python for writing an if statement. Python requires a colon at the end of the condition. If (score > 50) is not valid syntax due to the parentheses and missing colon. The third option misses the colon, and the fourth option uses '=>' which is not a valid operator in Python; it should be '>=' for greater than or equal.

  4. Loop Structures

    What is the correct Python statement to iterate over a list named 'items' and print each element?

    1. for item in items: print(item)
    2. for (item : items) print item
    3. foreach item in items: print(item)
    4. for item in items print(item)

    Explanation: for item in items: print(item) is the correct way in Python to loop through each element in a list. Python uses 'for ... in ... :' for iteration. The foreach keyword does not exist in Python. Options without a colon or with incorrect syntax, such as parentheses or missing indentation, will not execute.

  5. Function Definition

    Which of these is the correct way to define a function that accepts one argument and prints it?

    1. def show(arg): print(arg)
    2. function show(arg): print arg
    3. def show[arg] print(arg)
    4. function show[arg] { print(arg) }

    Explanation: In Python, functions are defined with the 'def' keyword, followed by the function name, arguments in parentheses, a colon, and the function body. The correct option is def show(arg): print(arg). The other options use incorrect syntax or keywords not valid in Python, such as 'function' and [ ] instead of ( ).

  6. String Manipulation

    Given s = 'hello', what is the correct way to change the value of s to uppercase in Python?

    1. s = s.upper()
    2. s = s.upcase()
    3. s = uppercase(s)
    4. s = toUpper(s)

    Explanation: The upper() method transforms a string to uppercase in Python, so s = s.upper() is correct. The upcase() and toUpper() methods do not exist in Python, and uppercase(s) is not a built-in function. Using the correct method name is essential for manipulating strings as intended.

  7. List Indexing

    If my_list = [1, 2, 3, 4], which statement retrieves the last element of the list?

    1. my_list[-1]
    2. my_list[4]
    3. my_list[last]
    4. my_list.(-1)

    Explanation: Using negative indexing, my_list[-1] correctly retrieves the last element of the list in Python. my_list[4] raises an IndexError because indexing starts at 0. There is no valid use of the keyword 'last' or the syntax my_list.(-1) in Python indexing.

  8. Dictionary Access

    Given d = {'key': 5}, how should you access the value associated with 'key'?

    1. d['key']
    2. d(key)
    3. d.key
    4. d->'key'

    Explanation: The correct way to access a value by key in a Python dictionary is d['key']. d(key) tries to call the dictionary as a function, which is not correct. d.key is not valid unless using special objects, and d->'key' uses syntax from other programming languages.

  9. Slicing

    What will be the result of the expression data[1:3] if data = [10, 20, 30, 40]?

    1. [20, 30]
    2. [10, 20]
    3. [20, 30, 40]
    4. [30, 40]

    Explanation: data[1:3] returns a new list containing items at index 1 and 2, meaning [20, 30]. Slicing in Python does not include the element at the stop index (3), so 40 is excluded. The other options include incorrect items or slice ranges.

  10. Boolean Values

    How is the logical 'and' operator written in Python to combine two conditions?

    1. and
    2. &
    3. &&
    4. plus

    Explanation: Python uses the word 'and' to combine two boolean expressions logically. The '&&' operator is used in other languages but not in Python. The '&' symbol performs a bitwise 'and', not logical, and 'plus' is unrelated to logical operations.

  11. Type Conversion

    What is the correct way to convert the string '123' to an integer in Python?

    1. int('123')
    2. string_to_int('123')
    3. parseInt('123')
    4. Integer('123')

    Explanation: The int() function is the built-in way to convert a string representing an integer into an actual integer in Python. Functions like string_to_int() and parseInt() are not standard in Python, and Integer is not a type constructor in Python.

  12. Comments

    Which symbol is used for writing a single-line comment in Python?

    1. #
    2. //
    3. /*
    4. --

    Explanation: Python uses the '#' symbol to begin a single-line comment. The double slash (//) and slash-asterisk (/*) are used in other programming languages. The double hyphen (--) is not used for comments in Python.

  13. Indentation

    Why is indentation used in Python?

    1. To define code blocks
    2. To align code for readability only
    3. To add comments in the code
    4. To separate statements like semicolons

    Explanation: Indentation in Python is required to define code blocks, such as for functions, loops, and conditionals. While indentation can improve readability in other languages, in Python it is a necessary part of the syntax. It is not used for comments or to separate statements, which are separated by newlines.

  14. Import Statements

    Which statement correctly imports the 'math' module in Python?

    1. import math
    2. #import math
    3. include math
    4. import_math

    Explanation: The statement import math is correct for importing the math module in Python. Using a hash (#) makes the line a comment, include math is incorrect as Python does not use 'include', and import_math is not a Python statement.

  15. Exception Handling

    How do you start exception handling in Python to catch all exceptions?

    1. try:
    2. catch:
    3. except:
    4. handle:

    Explanation: In Python, exception handling begins with the 'try:' keyword, followed by the block of code that might raise an error. The 'catch:' and 'handle:' keywords are not recognized in Python; handling exceptions is done with 'except:'. However, you must use 'try:' to start the block.

  16. Return Statement

    In Python, which keyword is used to send a value back from a function to the caller?

    1. return
    2. yield
    3. send
    4. output

    Explanation: The 'return' keyword is used in Python to pass a value back to the function caller. The 'yield' keyword is used in generators for producing a sequence of values, not an immediate return. The words 'send' and 'output' are not used for returning values from functions.