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.

Given a scenario where a system should notify a user if the storage space goes below 5% or above 95%. Which of the following combinations of operators will be suitable for the condition?

  • if storage < 5 or storage > 95:
  • if storage <= 5 or storage >= 95:
  • if storage > 5 and storage < 95:
  • if 5 <= storage <= 95:
To check if the storage space goes below 5% or above 95%, you should use the condition if storage <= 5 or storage >= 95:. This condition covers both cases, ensuring that the system notifies the user when the storage falls below 5% or exceeds 95%. The other options do not provide the correct combination of operators for this scenario.

Which Python method is executed when the context of a with statement is entered?

  • __enter__()
  • __exit__()
  • __init__()
  • __open__()
The __enter__() method is executed when the context of a 'with' statement is entered. It typically sets up the required resources or context for the code block within the 'with' statement.

Packages in Python provide a way of organizing related modules into a single _______ directory hierarchy.

  • File System
  • Folder
  • Namespace
  • Package
Packages in Python help organize related modules into a single folder hierarchy, making it easier to manage and access them in a structured way.

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.

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.

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.