Essential Python Basics: Quick Interview Prep Quiz Quiz

Sharpen your Python fundamentals with this beginner-friendly quiz covering lists, tuples, swapping variables, data types, style guides, and core syntax. Designed for Python learners and job-seekers, this quiz helps reinforce key interview concepts for efficient preparation.

  1. Key Features of Python

    Which of the following is a key feature of Python that makes it popular for beginners and professionals alike?

    1. It requires manual memory management.
    2. It avoids object-oriented programming.
    3. It is interpreted and dynamically typed.
    4. It only runs on one operating system.

    Explanation: Python is known for being interpreted and dynamically typed, which simplifies code writing and improves flexibility. It does not require manual memory management—memory is managed automatically. Python actually supports object-oriented programming alongside procedural and functional styles. Since Python is cross-platform, it does not run only on one operating system.

  2. Lists vs. Tuples

    What is a primary difference between a Python list and a Python tuple?

    1. Lists are immutable, tuples are mutable.
    2. Lists use brackets [], tuples use parentheses ().
    3. Lists can only hold integers, tuples can hold strings.
    4. Lists require less memory than tuples.

    Explanation: Lists in Python are defined with square brackets while tuples use parentheses. The correct mutability is that lists are mutable and tuples are immutable, so the first option is incorrect. Both lists and tuples can store any data type, not just integers or strings. Memory usage can vary, but that is not a primary, defining difference.

  3. Swapping Variables

    How can you swap the values of variables a and b in Python without using a third variable?

    1. a, b = b, a
    2. swap(a, b)
    3. a = b + a; b = a - b
    4. switch(a, b)

    Explanation: Python allows swapping variables directly using a, b = b, a. There is no built-in 'swap' or 'switch' function in core Python. The a = b + a; b = a - b syntax works in some languages for numbers, but it's not a Pythonic or general approach.

  4. Identity vs. Equality

    In Python, what is the main difference between the operators 'is' and '=='?

    1. 'is' compares values, '==' compares types.
    2. 'is' checks identity, '==' checks value equality.
    3. 'is' is used only with numbers, '==' only with strings.
    4. 'is' compares memory size, '==' compares length.

    Explanation: 'is' checks whether two variables point to the exact same object in memory (identity), while '==' checks if their values are equal. The other options either incorrectly swap their functions, limit usage by type, or confuse 'is' with comparison of memory size or length.

  5. PEP 8 and Python Style

    What is PEP 8 in the Python community?

    1. A built-in function for finding errors.
    2. A data type for handling numbers.
    3. The official style guide for Python code.
    4. A framework for web development.

    Explanation: PEP 8 is the official style guide outlining best practices for writing Python code. It is not a function, data type, or web framework. While it's not required for code execution, following PEP 8 helps keep code readable and consistent.

  6. Mutable vs. Immutable Types

    Which Python types are considered immutable and cannot be changed after creation?

    1. Dictionaries and sets
    2. Strings and tuples
    3. Lists and sets
    4. Dictionaries and lists

    Explanation: Strings and tuples are both immutable in Python, meaning their contents cannot be modified after creation. Dictionaries, lists, and sets are all mutable and can be changed. Immutable types help ensure data remains constant when needed.

  7. Python Memory Management

    How does Python automatically manage memory for objects?

    1. By using manual allocations only
    2. Through reference counting and garbage collection
    3. By deleting all objects after each function call
    4. Through explicit free() calls like in C

    Explanation: Python manages memory by keeping track of reference counts and using garbage collection to remove objects with no references. Explicit free() calls are used in languages like C, not Python. Memory is not managed solely by deleting objects after functions, nor is it manual in basic Python.

  8. Loop Control Statements

    Which Python statement is used as a placeholder and does nothing when executed, for example inside a loop or function?

    1. break
    2. continue
    3. stop
    4. pass

    Explanation: The 'pass' statement does nothing and serves as a placeholder where code is syntactically required but no action is needed. 'break' exits loops, 'continue' skips to the next iteration, and 'stop' is not a valid Python statement.

  9. Flexible Function Arguments

    What is the purpose of *args and **kwargs in a Python function definition?

    1. They sort list elements automatically.
    2. They return the maximum and minimum values.
    3. They allow functions to accept any number of arguments.
    4. They create private variables inside the function.

    Explanation: *args and **kwargs let a function accept a variable number of positional and keyword arguments, respectively. They do not sort, return maximum/minimum, nor do they create private variables. This flexibility is useful for many generic functions.

  10. Lambda Functions

    Which statement best describes a lambda function in Python?

    1. It is a loop keyword for iteration.
    2. It is an anonymous, single-line function.
    3. It creates a new list automatically.
    4. It reverses the contents of a string.

    Explanation: A lambda function is a small, anonymous function written in a single line, often used for short operations. It is not a loop keyword, does not inherently create lists, and does not reverse strings (though it could define such logic).