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.

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.

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.

To loop through two lists simultaneously, one can use the _______ function in conjunction with a for loop.

  • concat()
  • enumerate()
  • itertools.product()
  • zip()
To iterate through two lists simultaneously, the 'zip()' function is used in conjunction with a for loop. 'zip()' combines elements from two or more iterables into tuples, allowing you to iterate over pairs of elements. This is a common technique for parallel iteration through multiple lists.

What is the purpose of the import keyword in Python?

  • To create custom functions
  • To declare class names
  • To define variables
  • To include external modules
The 'import' keyword in Python is used to include external modules or libraries, making their functionality available in your script.

How can you achieve the symmetric difference of two sets in Python?

  • Using the difference() method
  • Using the intersection() method
  • Using the symmetric_difference() method
  • Using the union() method
To find the symmetric difference of two sets in Python, you can use the symmetric_difference() method. It returns a new set containing elements that are unique to each of the two sets, excluding the elements they have in common.

How can you implement a switch-case like behavior in Python?

  • By importing the 'switch' module
  • By using if-elif-else statements
  • By using the 'switch' keyword
  • Python does not support switch-case
Python does not have a built-in 'switch' statement, but you can achieve similar behavior using if-elif-else statements to evaluate multiple conditions.

To make a module's functions directly accessible, you can use the _______ statement.

  • From
  • Import
  • Include
  • Require
To make a module's functions directly accessible, you can use the 'from' statement. It allows you to import specific functions or objects from a module into your current namespace.

What happens if you try to raise an exception using the raise keyword without specifying an exception?

  • It raises a 'NameError'
  • It raises a 'SyntaxError'
  • It raises a 'TypeError'
  • It raises a generic 'Exception'
If you raise an exception using the 'raise' keyword without specifying an exception type, it raises a generic 'Exception' with no message.

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.

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.

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.