Which of the following syntaxes is used to define an empty dictionary in Python?

  • empty_dict = ()
  • empty_dict = []
  • empty_dict = {}
  • empty_dict = {}{}
The correct syntax to define an empty dictionary in Python is 'empty_dict = {}'. An empty dictionary consists of a pair of curly braces with nothing inside.

What is the primary difference between a class attribute and an instance attribute in Python?

  • Class attributes are private, while instance attributes are public.
  • Class attributes are shared among all instances of a class, while instance attributes are specific to each instance.
  • Class attributes are specific to each instance, while instance attributes are shared among all instances of a class.
  • Class attributes can only be accessed within the class itself.
The primary difference is that class attributes are shared among all instances of a class, whereas instance attributes are specific to each instance. This affects how data is stored and accessed in Python classes.

When working with large files, why might the readlines() method not be the most memory-efficient?

  • It doesn't work with large files
  • It lacks support for binary file formats
  • It reads the entire file into memory
  • It's not available in Python
The 'readlines()' method reads the entire file into memory, which can be inefficient for large files, as it can lead to memory consumption issues. It's better to use methods like 'readline()' or iterate over the file to process it line by line.

Which keyword is used in Python to check a condition?

  • check
  • if
  • then
  • verify
In Python, the 'if' keyword is used to check a condition. It allows you to execute a block of code if the condition is true.

What do you call a function defined inside another function in Python?

  • Child function
  • Inner function
  • Nested function
  • Sub function
A function defined inside another function in Python is called an 'Inner function' or 'Nested function.' They have access to the variables of the enclosing function.

Which Python data type is used to represent whole numbers?

  • bool
  • float
  • int
  • str
The 'int' data type in Python is used to represent whole numbers, both positive and negative, without any fractional part.

You can use the 'try...finally' block in Python to ensure that the database connection is always closed.

  • Implement a custom function to manage connections
  • Manually close the connection in each place
  • Rely on Python's automatic garbage collection
  • Use a context manager like 'with' statements
The 'try...finally' block ensures that the database connection is closed even if an exception occurs. It's a robust way to manage resource cleanup.

You have a function that needs to return multiple values, which data structure is more commonly used for such a purpose?

  • Dictionary
  • List
  • Set
  • Tuple
Tuples are commonly used to return multiple values from a function because they are immutable (cannot be changed), ensuring data integrity. This makes them a safer choice than lists, which are mutable. Using a tuple also communicates that the returned values should not be modified.

When importing a module, what is the primary purpose of using the as keyword?

  • To create an alias for the module
  • To hide the module's functions
  • To prevent the module from being imported again
  • To specify the module's version
The 'as' keyword is used to create an alias for a module when importing it. This alias can make it easier to reference the module in your code, especially if the module name is long or conflicts with other names in your script.

In a project where memory efficiency and speed are of utmost priority and the data doesn't need any modification once set, which data structure would be a better choice?

  • Dictionary
  • List
  • Set
  • Tuple
Tuples are an excellent choice when memory efficiency and speed are crucial because they are immutable and consume less memory compared to lists. Their immutability also ensures that data integrity is maintained, making them suitable for scenarios where data should not be modified after initialization.

What keyword is used to stop the execution of the current iteration and proceed to the next in a loop?

  • break
  • continue
  • skip
  • stop
The 'continue' keyword is used to stop the execution of the current iteration in a loop and proceed to the next iteration. It allows you to skip specific iterations.

For which of the following OS is Python NOT available by default?

  • Android
  • Linux
  • Windows
  • macOS
Python is available by default on Windows, macOS, and Linux. However, it is not available by default on Android operating systems.