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.
Which of the following is a key feature of Python that makes it popular for beginners and professionals alike?
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.
What is a primary difference between a Python list and a Python tuple?
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.
How can you swap the values of variables a and b in Python without using a third variable?
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.
In Python, what is the main difference between the operators 'is' and '=='?
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.
What is PEP 8 in the Python community?
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.
Which Python types are considered immutable and cannot be changed after creation?
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.
How does Python automatically manage memory for objects?
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.
Which Python statement is used as a placeholder and does nothing when executed, for example inside a loop or function?
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.
What is the purpose of *args and **kwargs in a Python function definition?
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.
Which statement best describes a lambda function in Python?
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).