Challenge your Python knowledge with this 15-question quiz covering everything from basic syntax, data types, and variables to advanced topics such as decorators, generators, and concurrency. Perfect for candidates preparing for Python interviews or anyone wanting a quick, effective review of essential Python programming concepts.
What does it mean that Python is a dynamically typed language?
Explanation: In Python, variable types are determined at runtime, making the language dynamically typed. Unlike statically typed languages, you do not have to explicitly declare a variable's type. Typing errors will not be caught at compile time because Python does not compile code in the same way as some other languages. Declaring variables as only integers is incorrect and restrictive.
Which statement is true about integers in Python?
Explanation: Integers in Python can represent numbers of any size, limited only by your system's memory, giving them effectively unlimited precision. Python does not restrict integers to 2^31-1, as is common in some older or lower-level languages. Integer division with // returns another integer, and all integers are immutable objects in Python.
What happens if you try to modify an individual character of a Python string?
Explanation: Strings in Python are immutable, so trying to change an individual character like my_str[0] = 'a' will raise a TypeError. The original string cannot change, and characters won't be shifted or appended in this way. To modify a string, you must create a new one.
Why does 0.1 + 0.2 == 0.3 evaluate to False in Python?
Explanation: Floating-point representation in computers cannot exactly express some decimal numbers, leading to precision errors like 0.1 + 0.2 not exactly equaling 0.3. Python does support decimal arithmetic, and the addition operator behaves normally for floats. Integer promotion is not a cause in this context.
Which built-in function would you use to convert a string '7.5' into a floating-point number?
Explanation: The float() function converts a string representing a decimal number to its float value, so float('7.5') results in 7.5. int() tries to convert to an integer but fails on decimals. str() and bool() do not perform numeric conversions from strings.
If x = 10, what does type(x) return in Python?
Explanation: type(x) returns <class 'int'> to indicate that x is an integer object in Python 3. The other answers are not valid outputs of the type() function in Python 3, as '<type 'integer'>' was used in older Python versions and '10' is simply the value.
Which of the following is a valid for loop in Python to print numbers 0 to 4?
Explanation: Python uses the syntax for i in range(5): print(i) to iterate from 0 to 4. The C-style loop and pseudo-code options are invalid in Python. Python also requires a colon and proper indentation in its loops.
How can you create a list of the squares of even numbers from 0 to 9 in Python?
Explanation: List comprehensions in Python can include conditions, so [x**2 for x in range(10) if x % 2 == 0] creates squares of even numbers between 0 and 9. [x^2 ...] uses bitwise xor instead of exponentiation, and {x*2 ...} creates a set, not a list. [x*x for x in range(0,10,2)] creates squares of only even numbers, but misses the clarity of the conditional form.
Why is it risky to use a mutable object like a list as a default value for a Python function argument?
Explanation: A mutable default argument like a list is shared between all calls to the function, which can lead to unintended behavior. It's not prohibited or a syntax error, but can introduce subtle bugs. While performance may be affected in rare cases, the main issue is unexpected sharing.
What is the primary use of a dictionary in Python?
Explanation: Dictionaries in Python are used to store data as key-value pairs, allowing efficient lookups. They do not enforce uniqueness of values, nor do they automatically sort items. Dictionaries themselves are mutable, so they do not enforce immutability.
What does inheritance allow in Python's object-oriented programming?
Explanation: Inheritance allows one class to use and override the methods and properties of another, supporting code reuse and logical structure. It does not offer multiple constructors or data hiding exclusively. Making a class immutable is unrelated to inheritance.
What is the primary purpose of a decorator in Python?
Explanation: Decorators are functions that modify or extend the behavior of the functions or methods they wrap. They do not introduce new types, check for syntax errors, or handle encryption. Decorators are frequently used for logging, access control, or other enhancements.
Which keyword is used in Python to define a generator?
Explanation: The yield keyword converts a function into a generator, which can be iterated over to produce values one at a time. The return keyword simply exits a function and gives back a value. The break and continue keywords are used within loops, not for defining generators.
Which Python library can be used for running code in parallel on multiple CPU cores?
Explanation: The multiprocessing library allows you to create processes that run in parallel, making use of multiple CPU cores. The math library provides mathematical functions, sys is for system-specific parameters, and logging is for creating log messages.
What is a main characteristic of a set in Python?
Explanation: Sets in Python are collections of unique, unordered items. They do not preserve the order of elements, allow duplicates, or store key-value pairs. Sets are commonly used to test membership and remove duplicates.
If you call range(1, 6), what sequence is generated?
Explanation: The range(1, 6) function generates numbers from 1 up to, but not including, 6: that is, 1, 2, 3, 4, 5. Including 6 is incorrect because the stop value is exclusive. Starting from 0 or ending at 4 also does not match the provided parameters.