A colleague is facing an ImportError when trying to import a package. Upon checking, you notice the package directory lacks a certain file, making Python not recognize it as a package. Which file is missing?
- init.py
- main.py
- package.py
- setup.py
The 'init.py' file is missing. This file is required in Python package directories for package recognition and proper importing.
Which operator will return True if both the operands are true?
- and
- not
- or
- xor
The 'and' operator in Python returns True if both of its operands are true. It performs a logical AND operation. For example, True and True evaluates to True, while True and False evaluates to False.
When dealing with file I/O, the _______ statement is often used to ensure that the file is properly closed after its suite finishes.
- close
- open
- try-finally
- with
When dealing with file I/O, the try-finally statement is often used to ensure that the file is properly closed after its suite finishes, even if an exception is raised.
In OOP, what is the term for the class from which the derived class inherits?
- Base Class
- Parent Class
- Primary Class
- Superclass
The class from which a derived class inherits is known as the 'Base Class' or 'Parent Class.' In Python, it's also referred to as the superclass.
What does the @property decorator do in a Python class?
- It defines a class method
- It defines a getter method
- It defines a setter method
- It defines a static method
The @property decorator defines a getter method for a class attribute, allowing you to access it as if it were an instance variable.
In a for loop, what does the range(3, 8, 2) function return?
- [2, 4, 6, 8]
- [3, 4, 5, 6, 7]
- [3, 5, 7]
- [3, 5]
range(3, 8, 2) generates values starting from 3 (inclusive) up to 8 (exclusive) with a step size of 2, resulting in [3, 5, 7].
Which built-in function can be used to create a set in Python?
- create_set()
- make_set()
- new_set()
- set()
The built-in function set() is used to create a set in Python. You can use it by calling set(), and you can pass an iterable (like a list) as an argument to initialize the set with elements.
The json module provides two methods for JSON serialization: dump() and _______.
- dumps()
- save()
- serialize()
- write()
The json module in Python provides two methods for JSON serialization: dump() and dumps(). The dump() method is used to serialize a Python object to a JSON file, while dumps() is used to serialize a Python object to a JSON-formatted string. Both methods are essential for working with JSON data in Python.
What is the base class in Python from which all exceptions inherit?
- BaseError
- BaseException
- Error
- Exception
The base class from which all exceptions inherit in Python is 'BaseException'. It's the root of the exception hierarchy and provides common methods like str() and args for all exceptions.
Which keyword allows the creation of a block of code that always runs, regardless of exceptions?
- catch
- except
- finally
- try
In Python, the finally keyword is used to create a block of code that always runs, even if an exception is raised. It is commonly used for cleanup tasks.