python quiz

1. What is the output of the following Python code?

Python code :

x = 5 y = 2 result = x // y print(result)

Options:

  1. 2.5
  2. 2.0
  3. 2
  4. 3

Correct Answer: 3. 2

Explanation: The // operator performs floor division, which returns the largest integer less than or equal to the division of the operands. In this case, 5 divided by 2 is 2 with a remainder, so the output is 2.*


2. Which of the following statements is true regarding Python’s list?

Options:

  1. Lists are immutable.
  2. Lists can contain elements of different data types.
  3. Lists can only be accessed using numeric indices.
  4. Lists can have a fixed size.

Correct Answer: 2. Lists can contain elements of different data types.

Explanation: Python lists can hold elements of different data types, making them versatile. Unlike some other languages, Python lists are mutable, meaning you can modify them after creation.*


3. What will be the output of the following code?

python code:

numbers = [1, 2, 3, 4, 5] squared = [num**2 for num in numbers if num % 2 == 0] print(squared)

Options:

  1. [1, 4, 9, 16, 25]
  2. [4, 16]
  3. [1, 9, 25]
  4. [1, 4, 16]

Correct Answer: 2. [4, 16]

Explanation: The list comprehension generates a new list containing the squares of even numbers from the original list. Thus, the output is [4, 16].*


4. What does the pass statement do in Python?

Options:

  1. Exits the program.
  2. Skips the current iteration in a loop.
  3. Declares a variable without assigning a value.
  4. Raises an exception.

Correct Answer: 2. Skips the current iteration in a loop.

Explanation: The pass statement in Python is a no-operation statement. It is used when a statement is syntactically required, but you don’t want to execute any code. It is often used as a placeholder and does nothing when executed.*


5. How can you open a file named “example.txt” in Python for reading and writing?

Options:

  1. file = open("example.txt", "r")
  2. file = open("example.txt", "w")
  3. file = open("example.txt", "rw")
  4. file = open("example.txt", "a")

Correct Answer: 4. file = open("example.txt", "a")

Explanation: To open a file in Python for both reading and writing, you can use the “a” mode (append). This allows you to read and write to the file without truncating it.*


6. What is the purpose of the __init__ method in a Python class?

Options:

  1. It is used to initialize the class object.
  2. It is a reserved method for private initialization.
  3. It is used to destroy the class object.
  4. It is a special method for defining class attributes.

Correct Answer:

  1. It is used to initialize the class object.

Explanation: The __init__ method is a special method in Python classes that is automatically called when an object is created. It is used to initialize the attributes of the class.*


7. In Python, what is the purpose of the else clause in a try-except block?

Options:

  1. It is executed if an exception occurs.
  2. It is executed if no exception occurs.
  3. It is used to handle specific exceptions.
  4. It is used to raise a custom exception.

Correct Answer: 2. It is executed if no exception occurs.

Explanation: The else clause in a try-except block is executed if no exceptions are raised in the corresponding try block.*


8. What is the purpose of the super() function in Python?

Options:

  1. It calls the parent class’s constructor.
  2. It returns the current instance of the class.
  3. It is used to create a new instance of a class.
  4. It is a keyword for inheritance.

Correct Answer:

  1. It calls the parent class’s constructor.

Explanation: The super() function is used to call a method from the parent class. It is commonly used in the __init__ method of a subclass to invoke the constructor of the parent class.*

9. What is the purpose of the finally clause in a try-except block in Python?

Options:

  1. It is executed if an exception occurs.
  2. It is executed if no exception occurs.
  3. It is always executed, regardless of whether an exception occurs or not.
  4. It is used to handle specific exceptions.

Correct Answer: 3. It is always executed, regardless of whether an exception occurs or not.

Explanation: The finally clause in a try-except block is used to define cleanup actions that must be executed, regardless of whether an exception is raised or not.*


10. How can you concatenate two lists in Python?

Options:

  1. list1.join(list2)
  2. list1 + list2
  3. concat(list1, list2)
  4. merge(list1, list2)

Correct Answer: 2. list1 + list2

Explanation: The + operator is used for list concatenation in Python. It combines the elements of two lists to create a new list.*


11. What is the purpose of the break statement in a loop in Python?

Options:

  1. It ends the entire program.
  2. It skips the current iteration and continues with the next one.
  3. It terminates the loop and transfers control to the next statement after the loop.
  4. It raises an exception.

Correct Answer: 3. It terminates the loop and transfers control to the next statement after the loop.

Explanation: The break statement is used to exit a loop prematurely. It terminates the loop and transfers control to the next statement after the loop.*


12. What does the __str__ method do in Python?

Options:

  1. It converts an object to a string representation.
  2. It creates a new string object.
  3. It is used for string formatting.
  4. It is a reserved method for system strings.

Correct Answer:

  1. It converts an object to a string representation.

Explanation: The __str__ method is a special method in Python that is called when the str() function is used on an object. It should return a string representation of the object.*


13. In Python, what is the purpose of the enumerate function?

Options:

  1. It counts the number of elements in a list.
  2. It returns the index and value of each element in an iterable.
  3. It filters elements based on a given condition.
  4. It reverses the order of elements in a list.

Correct Answer: 2. It returns the index and value of each element in an iterable.

Explanation: The enumerate function in Python is used to iterate over a sequence (list, tuple, etc.) along with its index, providing both the index and the value of each element.*


14. What is the purpose of the lambda function in Python?

Options:

  1. It is used for declaring global variables.
  2. It is a reserved keyword for anonymous functions.
  3. It defines a new class in Python.
  4. It is used to handle exceptions.

Correct Answer: 2. It is a reserved keyword for anonymous functions.

Explanation: A lambda function is a concise way to create anonymous functions in Python. It is often used for short, one-time operations where a full function definition is unnecessary.*


15. How can you check if a key is present in a dictionary in Python?

Options:

  1. key in dict
  2. dict.contains(key)
  3. dict[key] != None
  4. key.exists(dict)

Correct Answer:

  1. key in dict

Explanation: The in keyword is used to check if a key is present in a dictionary in Python. It returns True if the key is found, and False otherwise.*


16. What is the purpose of the zip function in Python?

Options:

  1. It compresses files into a zip archive.
  2. It creates an iterator that aggregates elements from multiple iterables.
  3. It extracts files from a zip archive.
  4. It calculates the absolute value of a number.

Correct Answer: 2. It creates an iterator that aggregates elements from multiple iterables.

Explanation: The zip function in Python is used to combine elements from multiple iterables (e.g., lists, tuples) into tuples. It creates an iterator that generates tuples containing elements from the input iterables, allowing for parallel iteration.*

Explanation: The zip function in Python is used to combine elements from multiple iterables (e.g., lists, tuples) into tuples. It creates an iterator that generates tuples containing elements from the input iterables, allowing for parallel iteration.*

26. How can you check if a variable is of a certain data type in Python?

Options:

  1. variable.is_type()
  2. type(variable) == "desired_type"
  3. isinstance(variable, desired_type)
  4. variable.check_type(desired_type)

Correct Answer: 3. isinstance(variable, desired_type)

Explanation: The isinstance() function in Python is used to check if a variable is of a certain data type. It returns True if the variable is an instance of the specified type, otherwise False.*


27. What is the purpose of the __iter__ method in Python?

Options:

  1. It initializes an iterator object.
  2. It is used to iterate over elements in a list.
  3. It returns an iterator for the object.
  4. It is a reserved method for system iteration.

Correct Answer: 3. It returns an iterator for the object.

Explanation: The __iter__ method is used to define how an object should create an iterator. It returns an iterator object, which can be used to iterate over the elements of the object.*


28. How do you remove the last element from a list in Python?

Options:

  1. list.pop()
  2. list.remove(-1)
  3. list.delete(-1)
  4. list[-1] = None

Correct Answer:

  1. list.pop()

Explanation: The pop() method in Python, without specifying an index, removes and returns the last element from a list.*


29. What is the purpose of the ord() function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It rounds a floating-point number to the nearest integer.
  4. It is used for bitwise operations.

Correct Answer: 2. It converts a character to its Unicode code point.

Explanation: The ord() function in Python returns the Unicode code point of a given character. It is often used in conjunction with the chr() function.*


30. How can you check if a file exists in Python before attempting to open it?

Options:

  1. if file.exists()
  2. if os.file_exists()
  3. if os.path.exists(file)
  4. if file.check()

Correct Answer: 3. if os.path.exists(file)

Explanation: The os.path.exists() function in Python is used to check if a file or directory exists at the specified path. It returns True if the path exists, otherwise False.*


31. What is the purpose of the random.choice() function in Python?

Options:

  1. It generates a random integer.
  2. It selects a random element from a list.
  3. It shuffles the elements of a list.
  4. It generates a random floating-point number.

Correct Answer: 2. It selects a random element from a list.

Explanation: The random.choice() function in Python is used to select a random element from a sequence, such as a list.*


32. How can you convert a list to a tuple in Python?

Options:

  1. tuple(list)
  2. list.to_tuple()
  3. convert_to_tuple(list)
  4. list(tuple)

Correct Answer:

  1. tuple(list)

Explanation: The tuple() constructor in Python can be used to convert a list to a tuple.*


33. What does the __str__ method do in a Python class?

Options:

  1. It converts an object to a string representation.
  2. It creates a new string object.
  3. It is used for string formatting.
  4. It is a reserved method for system strings.

Correct Answer:

  1. It converts an object to a string representation.

Explanation: The __str__ method in a Python class is called when the str() function is used on an object. It should return a string representation of the object.*


34. How can you reverse the order of elements in a list in Python?

Options:

  1. list.reverse()
  2. reversed(list)
  3. list.sort(reverse=True)
  4. list.flip()

Correct Answer: 2. reversed(list)

Explanation: The reversed() function in Python returns a reverse iterator, which can be used to iterate over the elements of a sequence in reverse order. To convert it back to a list, you can use list(reversed(my_list)) or use slicing, my_list[::-1].*


35. What is the purpose of the break statement in a loop in Python?

Options:

  1. It ends the entire program.
  2. It skips the current iteration and continues with the next one.
  3. It terminates the loop and transfers control to the next statement after the loop.
  4. It raises an exception.

Correct Answer: 3. It terminates the loop and transfers control to the next statement after the loop.

Explanation: The break statement in Python is used to exit a loop prematurely. It terminates the loop and transfers control to the next statement after the loop.*

36. What is the purpose of the __len__ method in Python?

Options:

  1. It returns the length of a list or tuple.
  2. It is a reserved method for system length calculations.
  3. It is used to define the length of a class.
  4. It is a special method to access the length of an object.

Correct Answer:

  1. It returns the length of a list or tuple.

Explanation: The __len__ method in Python is a special method that is called when the built-in len() function is used on an object. It should return the length of the object.*


37. How can you concatenate two dictionaries in Python?

Options:

  1. dict1.append(dict2)
  2. dict1 + dict2
  3. dict1.extend(dict2)
  4. dict1.update(dict2)

Correct Answer: 4. dict1.update(dict2)

Explanation: To concatenate two dictionaries in Python, you can use the update() method, which adds the key-value pairs from one dictionary to another.*


38. What is the purpose of the try and except blocks in Python?

Options:

  1. They define a new function.
  2. They handle errors and exceptions.
  3. They create a loop.
  4. They are reserved keywords for system-level operations.

Correct Answer: 2. They handle errors and exceptions.

Explanation: The try and except blocks in Python are used for exception handling. Code that may raise an exception is placed inside the try block, and the handling of the exception is done in the except block.*


39. How can you convert a string to an integer in Python?

Options:

  1. int(string)
  2. string.to_int()
  3. convert_to_int(string)
  4. integer(string)

Correct Answer:

  1. int(string)

Explanation: The int() function in Python is used to convert a string to an integer.*


40. What is the purpose of the pass statement in Python?

Options:

  1. It is used to end the program.
  2. It skips the current iteration in a loop.
  3. It is a placeholder for future code.
  4. It raises an exception.

Correct Answer: 3. It is a placeholder for future code.

Explanation: The pass statement in Python is often used as a placeholder where syntactically some code is required but no action is desired.*


41. How can you round a floating-point number to a specified number of decimal places in Python?

Options:

  1. round(number, decimals)
  2. number.round(decimals)
  3. round(number, places)
  4. number.toFixed(decimals)

Correct Answer:

  1. round(number, decimals)

Explanation: The round() function in Python is used to round a floating-point number to the specified number of decimal places.*


42. What is the purpose of the with statement in Python?

Options:

  1. It defines a new context manager.
  2. It creates a loop.
  3. It handles exceptions.
  4. It is a reserved keyword for file operations.

Correct Answer:

  1. It defines a new context manager.

Explanation: The with statement in Python is used to create a context manager, which simplifies resource management, such as file handling, by automatically taking care of setup and teardown operations.*


43. What is the purpose of the filter() function in Python?

Options:

  1. It filters elements based on a given condition.
  2. It creates a new list containing the unique elements of an existing list.
  3. It applies a function to all the elements of an iterable.
  4. It is used for filtering files in a directory.

Correct Answer:

  1. It filters elements based on a given condition.

Explanation: The filter() function in Python is used to filter elements from an iterable based on a specified function or condition.*


44. How can you convert a tuple to a list in Python?

Options:

  1. list.convert(tuple)
  2. tuple.to_list()
  3. list(tuple)
  4. convert_to_list(tuple)

Correct Answer: 3. list(tuple)

Explanation: The list() constructor in Python can be used to convert a tuple to a list.*


45. What is the purpose of the __eq__ method in Python?

Options:

  1. It is a reserved method for equality comparisons.
  2. It defines a new class attribute.
  3. It is used for exception handling.
  4. It creates a new instance of a class.

Correct Answer:

  1. It is a reserved method for equality comparisons.

Explanation: The __eq__ method in Python is a special method used for defining the behavior of the equality operator (==) for instances of a class.*


46. How can you check if a number is an integer in Python?

Options:

  1. number.is_integer()
  2. integer(number)
  3. isinteger(number)
  4. type(number) == int

Correct Answer:

  1. number.is_integer()

Explanation: The is_integer() method in Python can be used to check if a floating-point number represents an integer.*


47. What is the purpose of the del statement in Python?

Options:

  1. It is used to delete a file.
  2. It removes an element from a list.
  3. It deletes a variable or object.
  4. It is a reserved keyword for declaring classes.

Correct Answer: 3. It deletes a variable or object.

Explanation: The del statement in Python is used to delete a variable or object. It can also be used to delete elements from a list or a dictionary.*


48. How can you convert a list of strings to a single string in Python?

Options:

  1. str(list)
  2. "".join(list)
  3. list.convert_to_string()
  4. string(list)

Correct Answer: 2. "".join(list)

Explanation: The "".join(list) expression in Python can be used to concatenate the elements of a list of strings into a single string.*


49. What is the purpose of the zip function in Python?

Options:

  1. It compresses files into a zip archive.
  2. It creates an iterator that aggregates elements from multiple iterables.
  3. It extracts files from a zip archive.
  4. It calculates the absolute value of a number.

Correct Answer: 2. It creates an iterator that aggregates elements from multiple iterables.

Explanation: The zip function in Python is used to combine elements from multiple iterables (e.g., lists, tuples) into tuples. It creates an iterator that generates tuples containing elements from the input iterables, allowing for parallel iteration.*


50. What is the purpose of the os module in Python?

Options:

  1. It is used for mathematical calculations.
  2. It provides a way to interact with the operating system.
  3. It defines basic data types in Python.
  4. It is a reserved module for string operations.

Correct Answer: 2. It provides a way to interact with the operating system.

Explanation: The os module in Python provides a way to interact with the operating system, allowing you to perform various system-related tasks, such as file and directory operations.*

Explanation: The os module in Python provides a way to interact with the operating system, allowing you to perform various system-related tasks, such as file and directory operations.*

61. What is the purpose of the chr function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 3. It generates a character from a Unicode code point.

Explanation: The chr function in Python is used to generate a character from a Unicode code point.*


62. How can you open a file in Python in binary mode for both reading and writing?

Options:

  1. file = open("example.txt", "rb+")
  2. file = open("example.txt", "rwb")
  3. file = open("example.txt", "bw")
  4. file = open("example.txt", "br+")

Correct Answer:

  1. file = open("example.txt", "rb+")

Explanation: To open a file in binary mode for both reading and writing, you can use the “rb+” mode.*


63. What is the purpose of the sorted function in Python?

Options:

  1. It creates a sorted copy of a list.
  2. It sorts a list in place.
  3. It is used for sorting characters in a string.
  4. It returns the maximum value in an iterable.

Correct Answer:

  1. It creates a sorted copy of a list.

Explanation: The sorted function in Python is used to create a new sorted list from the elements of an iterable.*


64. How can you convert a number to a string in Python?

Options:

  1. number.to_string()
  2. str(number)
  3. string(number)
  4. convert_to_string(number)

Correct Answer: 2. str(number)

Explanation: The str() function in Python is used to convert a number to a string.*


65. What is the purpose of the enumerate function in a loop in Python?

Options:

  1. It counts the number of elements in a list.
  2. It creates a new list.
  3. It returns the index and value of each element in an iterable.
  4. It is used for enumerating characters in a string.

Correct Answer: 3. It returns the index and value of each element in an iterable.

Explanation: The enumerate function in Python is used in loops to iterate over both the index and the value of an iterable.*


66. How can you check if a string starts with a specific substring in Python?

Options:

  1. string.startswith(substring)
  2. substring.check_start(string)
  3. start(string, substring)
  4. string[0] == substring

Correct Answer:

  1. string.startswith(substring)

Explanation: The startswith() method in Python is used to check if a string starts with a specified prefix.*


67. What is the purpose of the next function in Python?

Options:

  1. It generates the next number in a sequence.
  2. It is a reserved keyword for system-level operations.
  3. It returns the next item from an iterator.
  4. It creates a new instance of a class.

Correct Answer: 3. It returns the next item from an iterator.

Explanation: The next function in Python is used to retrieve the next item from an iterator.*


68. How can you check if a variable is of a specific type in Python?

Options:

  1. isinstance(variable, type)
  2. variable.check_type(type)
  3. variable.typeof(type)
  4. type(variable) == type

Correct Answer:

  1. isinstance(variable, type)

Explanation: The isinstance() function in Python is used to check if a variable is an instance of a specific type.*


69. What is the purpose of the sum function in Python?

Options:

  1. It calculates the sum of all elements in an iterable.
  2. It is used for bitwise sum operations.
  3. It concatenates strings in a list.
  4. It returns the maximum value in an iterable.

Correct Answer:

  1. It calculates the sum of all elements in an iterable.

Explanation: The sum function in Python is used to calculate the sum of all elements in an iterable.*


70. How can you remove all occurrences of a specific element from a list in Python?

Options:

  1. list.remove_all(element)
  2. list.discard(element)
  3. list.remove(element)
  4. list.filter(element)

Correct Answer: 3. list.remove(element)

Explanation: The remove method in Python is used to remove the first occurrence of a specified value from a list. To remove all occurrences, you may need to use a loop or list comprehension

71. What is the purpose of the isalnum method in Python?

Options:

  1. It checks if a string contains only alphanumeric characters.
  2. It converts a string to lowercase.
  3. It checks if a string is empty.
  4. It removes all whitespace from a string.

Correct Answer:

  1. It checks if a string contains only alphanumeric characters.

Explanation: The isalnum method in Python is used to check if a string contains only alphanumeric characters.*


72. How can you find the index of the first occurrence of a value in a list in Python?

Options:

  1. list.index(value)
  2. list.find(value)
  3. find(list, value)
  4. list.search(value)

Correct Answer:

  1. list.index(value)

Explanation: The index method in Python is used to find the index of the first occurrence of a specified value in a list.*


73. What is the purpose of the round function in Python?

Options:

  1. It rounds a floating-point number to the nearest integer.
  2. It is used for rounding up a number.
  3. It is used for mathematical calculations.
  4. It rounds a number to a specified number of decimal places.

Correct Answer: 4. It rounds a number to a specified number of decimal places.

Explanation: The round function in Python is used to round a number to a specified number of decimal places.*


74. How can you convert a list of strings to a single string with a delimiter in Python?

Options:

  1. delimiter.join(list)
  2. list.combine(delimiter)
  3. str.concat(list, delimiter)
  4. list + delimiter

Correct Answer:

  1. delimiter.join(list)

Explanation: The join method in Python is used to concatenate a list of strings into a single string, using a specified delimiter.*


75. What is the purpose of the strip method in Python?

Options:

  1. It removes all whitespace characters from the beginning and end of a string.
  2. It splits a string into a list of substrings.
  3. It extracts a substring from a string.
  4. It is used for stripping comments from code.

Correct Answer:

  1. It removes all whitespace characters from the beginning and end of a string.

Explanation: The strip method in Python is used to remove leading and trailing whitespace characters from a string.*


76. How can you convert a string to a list of words in Python?

Options:

  1. list(string)
  2. split(string)
  3. string.to_list()
  4. words(string)

Correct Answer: 2. split(string)

Explanation: The split method in Python is used to split a string into a list of words, using whitespace as the default delimiter.*


77. What is the purpose of the bin function in Python?

Options:

  1. It converts a number to a binary string.
  2. It is used for bitwise operations.
  3. It creates a binary file.
  4. It converts a binary string to an integer.

Correct Answer:

  1. It converts a number to a binary string.

Explanation: The bin function in Python is used to convert an integer to a binary string.*


78. How can you check if a string ends with a specific suffix in Python?

Options:

  1. string.ends(suffix)
  2. string.endswith(suffix)
  3. endswith(string, suffix)
  4. suffix.check(string)

Correct Answer: 2. string.endswith(suffix)

Explanation: The endswith method in Python is used to check if a string ends with a specified suffix.*


79. What is the purpose of the reversed function in Python?

Options:

  1. It reverses the order of elements in a list.
  2. It creates a reversed copy of a string.
  3. It is used for reversing files.
  4. It generates a reverse iterator.

Correct Answer: 4. It generates a reverse iterator.

Explanation: The reversed function in Python is used to create a reverse iterator, which can be used to iterate over the elements of a sequence in reverse order.*


80. How can you remove all whitespace characters from a string in Python?

Options:

  1. string.trim()
  2. strip(string)
  3. string.remove_whitespace()
  4. "".join(string.split())

Correct Answer: 4. "".join(string.split())

Explanation: One way to remove all whitespace characters from a string in Python is to use the split method to create a list of words and then use "".join() to concatenate the words without spaces.*

81. What is the purpose of the all function in Python?

Options:

  1. It checks if any element in an iterable is true.
  2. It checks if all elements in an iterable are true.
  3. It returns the logical OR of all elements in an iterable.
  4. It is used for checking file permissions.

Correct Answer: 2. It checks if all elements in an iterable are true.

Explanation: The all function in Python returns True if all elements of an iterable are true, otherwise it returns False.*


82. How can you check if a number is a prime number in Python?

Options:

  1. isprime(number)
  2. number.check_prime()
  3. prime(number)
  4. number % 2 == 0

Correct Answer:

  1. isprime(number)

Explanation: Checking if a number is prime in Python often involves using a function like isprime() that tests for divisibility by all numbers less than the square root of the given number.*


83. What is the purpose of the staticmethod decorator in Python?

Options:

  1. It is used to define a static method in a class.
  2. It is a reserved keyword for system-level operations.
  3. It creates a new instance of a class.
  4. It is used for string formatting.

Correct Answer:

  1. It is used to define a static method in a class.

Explanation: The staticmethod decorator in Python is used to define a static method in a class. A static method is a method that belongs to the class rather than an instance of the class.*


84. How can you concatenate two sets in Python?

Options:

  1. set1.concat(set2)
  2. set1 + set2
  3. set1.extend(set2)
  4. set1.update(set2)

Correct Answer: 4. set1.update(set2)

Explanation: The update method in Python is used to add elements from another set (or any iterable) to an existing set, effectively concatenating them.*


85. What is the purpose of the min function in Python?

Options:

  1. It finds the minimum value in a list.
  2. It is used for bitwise minimum operations.
  3. It creates a new minimum value.
  4. It returns the index of the minimum element in a list.

Correct Answer:

  1. It finds the minimum value in a list.

Explanation: The min function in Python returns the smallest item in an iterable or the smallest of two or more arguments.*


86. How can you check if a string contains only numeric characters in Python?

Options:

  1. string.isdigit()
  2. string.isnumeric()
  3. numeric(string)
  4. check_numeric(string)

Correct Answer: 2. string.isnumeric()

Explanation: The isnumeric() method in Python is used to check if all the characters in a string are numeric.*


87. What is the purpose of the divmod function in Python?

Options:

  1. It divides two numbers and returns the quotient and remainder.
  2. It is used for bitwise division operations.
  3. It creates a new modulus value.
  4. It is used for dividing strings.

Correct Answer:

  1. It divides two numbers and returns the quotient and remainder.

Explanation: The divmod function in Python takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder when using integer division.*


88. How can you convert a dictionary to a list of key-value pairs in Python?

Options:

  1. list(dictionary)
  2. dictionary.to_list()
  3. list(dictionary.items())
  4. convert_to_list(dictionary)

Correct Answer: 3. list(dictionary.items())

Explanation: The items() method in Python is used to return a list of key-value pairs as tuples. Using list() on this result converts the dictionary to a list of tuples.*


89. What is the purpose of the filter function in Python?

Options:

  1. It filters elements based on a given condition.
  2. It creates a new list containing the unique elements of an existing list.
  3. It applies a function to all the elements of an iterable.
  4. It is used for filtering files in a directory.

Correct Answer:

  1. It filters elements based on a given condition.

Explanation: The filter function in Python is used to filter elements from an iterable based on a specified function or condition.*


90. How can you check if a list is empty in Python?

Options:

  1. if list.is_empty():
  2. if list == []:
  3. if len(list) == 0:
  4. if not list:

Correct Answer: 4. if not list:

Explanation: Checking if a list is empty in Python can be done using the not keyword, as an empty list evaluates to False in a boolean context.*

91. What is the purpose of the format method in Python?

Options:

  1. It formats a string by replacing placeholders with values.
  2. It is used for formatting numbers.
  3. It creates a new format object.
  4. It formats a date and time.

Correct Answer:

  1. It formats a string by replacing placeholders with values.

Explanation: The format method in Python is used to format a string by replacing placeholders with specified values.*


92. How can you convert a list of integers to a string in Python?

Options:

  1. str(list)
  2. "".join(map(str, list))
  3. convert_to_string(list)
  4. list.to_string()

Correct Answer: 2. "".join(map(str, list))

Explanation: Using map(str, list) converts each integer in the list to a string, and then "".join() concatenates them into a single string.*


93. What is the purpose of the callable function in Python?

Options:

  1. It checks if an object is callable.
  2. It calls a function.
  3. It creates a callable object.
  4. It is used for making calls to external APIs.

Correct Answer:

  1. It checks if an object is callable.

Explanation: The callable function in Python is used to check if an object appears to be callable (i.e., can be called as a function).*


94. How can you find the length of the longest word in a string in Python?

Options:

  1. max(len(word) for word in string)
  2. string.max_word_length()
  3. len(max(string.split(), key=len))
  4. longest_word_length(string)

Correct Answer: 3. len(max(string.split(), key=len))

Explanation: This expression splits the string into words using string.split(), finds the word with the maximum length using max(), and then calculates the length of that word.*


95. What is the purpose of the chr function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 3. It generates a character from a Unicode code point.

Explanation: The chr function in Python is used to generate a character from a Unicode code point.*


96. How can you check if a string contains only alphabetic characters in Python?

Options:

  1. string.isalpha()
  2. string.isalphabetic()
  3. is_alpha(string)
  4. check_alphabetic(string)

Correct Answer:

  1. string.isalpha()

Explanation: The isalpha method in Python is used to check if all characters in a string are alphabetic.*


97. What is the purpose of the locals function in Python?

Options:

  1. It is used for local variable declaration.
  2. It returns a dictionary of the current local symbol table.
  3. It creates local functions.
  4. It is used for localization of strings.

Correct Answer: 2. It returns a dictionary of the current local symbol table.

Explanation: The locals function in Python returns a dictionary of the current local symbol table, which contains all local variables.*


98. How can you reverse the order of elements in a list in Python?

Options:

  1. reversed(list)
  2. list.reverse()
  3. list.sort(reverse=True)
  4. reverse(list)

Correct Answer:

  1. reversed(list)

Explanation: The reversed function in Python is used to reverse the order of elements in an iterable.*


99. What is the purpose of the len function in Python?

Options:

  1. It calculates the length of a string.
  2. It is used for bitwise length operations.
  3. It returns the index of the maximum element in a list.
  4. It calculates the length of an iterable.

Correct Answer: 4. It calculates the length of an iterable.

Explanation: The len function in Python is used to calculate the number of items in an iterable or the length of a string.*


100. How can you remove duplicate elements from a list in Python?

Options:

  1. list.unique()
  2. unique(list)
  3. list.remove_duplicates()
  4. list(set(list))

Correct Answer: 4. list(set(list))

Explanation: Converting the list to a set and then back to a list eliminates duplicate elements, as sets cannot contain duplicate items.*

Explanation: Converting the list to a set and then back to a list eliminates duplicate elements, as sets cannot contain duplicate items.*

101. What is the purpose of the eval function in Python?

Options:

  1. It evaluates mathematical expressions.
  2. It evaluates logical expressions.
  3. It executes external commands.
  4. It evaluates the length of an iterable.

Correct Answer:

  1. It evaluates mathematical expressions.

Explanation: The eval function in Python is used to evaluate mathematical expressions or other valid Python expressions from a string.*


102. How can you convert a list of strings to lowercase in Python?

Options:

  1. list.lower()
  2. lower(list)
  3. [word.lower() for word in list]
  4. convert_to_lowercase(list)

Correct Answer: 3. [word.lower() for word in list]

Explanation: List comprehension can be used to create a new list where each string is converted to lowercase using word.lower().*


103. What is the purpose of the bytearray type in Python?

Options:

  1. It is used for representing boolean values.
  2. It represents a mutable sequence of bytes.
  3. It is used for bitwise operations.
  4. It is used for formatting strings.

Correct Answer: 2. It represents a mutable sequence of bytes.

Explanation: The bytearray type in Python represents a mutable sequence of bytes, and it can be modified after creation.*


104. How can you get the current date and time in Python?

Options:

  1. current_datetime()
  2. datetime.now()
  3. get_datetime()
  4. now(datetime)

Correct Answer: 2. datetime.now()

Explanation: The datetime.now() function in Python is used to get the current local date and time.*


105. What is the purpose of the sum function in Python?

Options:

  1. It calculates the sum of all elements in an iterable.
  2. It is used for bitwise sum operations.
  3. It concatenates strings in a list.
  4. It returns the maximum value in an iterable.

Correct Answer:

  1. It calculates the sum of all elements in an iterable.

Explanation: The sum function in Python is used to calculate the sum of all elements in an iterable.*


106. How can you find the square root of a number in Python?

Options:

  1. number.sqrt()
  2. sqrt(number)
  3. math.square_root(number)
  4. number ** 0.5

Correct Answer: 4. number ** 0.5

Explanation: Calculating the square root of a number in Python can be done using the exponentiation operator ** with the value 0.5.*


107. What is the purpose of the ord function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 2. It converts a character to its Unicode code point.

Explanation: The ord function in Python is used to get the Unicode code point of a character.*


108. How can you check if a number is a power of two in Python?

Options:

  1. number.power_of_two()
  2. power_of_two(number)
  3. number % 2 == 0
  4. (number & (number - 1)) == 0

Correct Answer: 4. (number & (number - 1)) == 0

Explanation: Checking if a number is a power of two in Python can be done using bitwise operations.*


109. What is the purpose of the bin function in Python?

Options:

  1. It converts a number to a binary string.
  2. It is used for bitwise operations.
  3. It creates a binary file.
  4. It converts a binary string to an integer.

Correct Answer:

  1. It converts a number to a binary string.

Explanation: The bin function in Python is used to convert an integer to a binary string.*


110. How can you concatenate two dictionaries in Python?

Options:

  1. dict1.concat(dict2)
  2. dict1 + dict2
  3. dict1.extend(dict2)
  4. dict1.update(dict2)

Correct Answer: 4. dict1.update(dict2)

Explanation: The update method in Python is used to add key-value pairs from another dictionary to an existing dictionary, effectively concatenating them.*

111. What is the purpose of the locals function in Python?

Options:

  1. It is used for local variable declaration.
  2. It returns a dictionary of the current local symbol table.
  3. It creates local functions.
  4. It is used for localization of strings.

Correct Answer: 2. It returns a dictionary of the current local symbol table.

Explanation: The locals function in Python returns a dictionary of the current local symbol table, which contains all local variables.*


112. How can you check if a string contains only alphabetic characters in Python?

Options:

  1. string.isalpha()
  2. string.isalphabetic()
  3. is_alpha(string)
  4. check_alphabetic(string)

Correct Answer:

  1. string.isalpha()

Explanation: The isalpha method in Python is used to check if all characters in a string are alphabetic.*


113. What is the purpose of the reversed function in Python?

Options:

  1. It reverses the order of elements in a list.
  2. It creates a reversed copy of a string.
  3. It is used for reversing files.
  4. It generates a reverse iterator.

Correct Answer: 4. It generates a reverse iterator.

Explanation: The reversed function in Python is used to create a reverse iterator, which can be used to iterate over the elements of a sequence in reverse order.*


114. How can you check if a variable is of a specific type in Python?

Options:

  1. isinstance(variable, type)
  2. variable.check_type(type)
  3. variable.typeof(type)
  4. type(variable) == type

Correct Answer:

  1. isinstance(variable, type)

Explanation: The isinstance() function in Python is used to check if a variable is an instance of a specific type.*


115. What is the purpose of the filter function in Python?

Options:

  1. It filters elements based on a given condition.
  2. It creates a new list containing the unique elements of an existing list.
  3. It applies a function to all the elements of an iterable.
  4. It is used for filtering files in a directory.

Correct Answer:

  1. It filters elements based on a given condition.

Explanation: The filter function in Python is used to filter elements from an iterable based on a specified function or condition.*


116. How can you convert a number to a string in Python?

Options:

  1. number.to_string()
  2. str(number)
  3. string(number)
  4. convert_to_string(number)

Correct Answer: 2. str(number)

Explanation: The str() function in Python is used to convert a number to a string.*


117. What is the purpose of the bin function in Python?

Options:

  1. It converts a number to a binary string.
  2. It is used for bitwise operations.
  3. It creates a binary file.
  4. It converts a binary string to an integer.

Correct Answer:

  1. It converts a number to a binary string.

Explanation: The bin function in Python is used to convert an integer to a binary string.*


118. How can you check if a string ends with a specific suffix in Python?

Options:

  1. string.ends(suffix)
  2. string.endswith(suffix)
  3. endswith(string, suffix)
  4. suffix.check(string)

Correct Answer: 2. string.endswith(suffix)

Explanation: The endswith method in Python is used to check if a string ends with a specified suffix.*


119. What is the purpose of the any function in Python?

Options:

  1. It checks if all elements in an iterable are true.
  2. It checks if any element in an iterable is true.
  3. It returns the logical AND of all elements in an iterable.
  4. It is used for logical OR operations.

Correct Answer: 2. It checks if any element in an iterable is true.

Explanation: The any function in Python returns True if at least one element of an iterable is true, otherwise it returns False.*


120. How can you remove an element from a set in Python?

Options:

  1. set.remove(element)
  2. set.delete(element)
  3. set.discard(element)
  4. remove(set, element)

Correct Answer: 3. set.discard(element)

Explanation: The discard() method in Python is used to remove a specified element from a set, if it is present.*

121. What is the purpose of the round function in Python?

Options:

  1. It rounds a floating-point number to the nearest integer.
  2. It is used for rounding up a number.
  3. It is used for mathematical calculations.
  4. It rounds a number to a specified number of decimal places.

Correct Answer: 4. It rounds a number to a specified number of decimal places.

Explanation: The round function in Python is used to round a number to a specified number of decimal places.*


122. How can you check if a string is a palindrome in Python?

Options:

  1. string.is_palindrome()
  2. is_palindrome(string)
  3. string == reverse(string)
  4. string == string[::-1]

Correct Answer: 4. string == string[::-1]

Explanation: Checking if a string is a palindrome in Python can be done by comparing the original string with its reverse.*


123. What is the purpose of the enumerate function in Python?

Options:

  1. It counts the number of elements in a list.
  2. It creates a new list.
  3. It returns the index and value of each element in an iterable.
  4. It is used for enumerating characters in a string.

Correct Answer: 3. It returns the index and value of each element in an iterable.

Explanation: The enumerate function in Python is used in loops to iterate over both the index and the value of an iterable.*


124. How can you convert a list of integers to a list of strings in Python?

Options:

  1. [str(list)]
  2. convert_to_strings(list)
  3. [str(x) for x in list]
  4. list.to_strings()

Correct Answer: 3. [str(x) for x in list]

Explanation: List comprehension can be used to create a new list where each integer is converted to a string using str(x).*


125. What is the purpose of the map function in Python?

Options:

  1. It is used for creating maps.
  2. It applies a function to all the elements of an iterable.
  3. It generates a map object.
  4. It creates a dictionary.

Correct Answer: 2. It applies a function to all the elements of an iterable.

Explanation: The map function in Python applies a specified function to all items in an input iterable (list, tuple, etc.) and returns an iterator that produces the results.*


126. How can you check if a number is even in Python?

Options:

  1. number.is_even()
  2. is_even(number)
  3. number % 2 == 0
  4. even(number)

Correct Answer: 3. number % 2 == 0

Explanation: Checking if a number is even in Python can be done using the modulus operator %. If the result is 0, the number is even.*


127. What is the purpose of the chr function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 3. It generates a character from a Unicode code point.

Explanation: The chr function in Python is used to generate a character from a Unicode code point.*


128. How can you find the index of the last occurrence of a value in a list in Python?

Options:

  1. list.find_last(value)
  2. list.rfind(value)
  3. list.index_last(value)
  4. list.search_last(value)

Correct Answer: 2. list.rfind(value)

Explanation: The rfind method in Python is used to find the index of the last occurrence of a specified value in a list.*


129. What is the purpose of the len function in Python?

Options:

  1. It calculates the length of a string.
  2. It is used for bitwise length operations.
  3. It returns the index of the maximum element in a list.
  4. It calculates the length of an iterable.

Correct Answer: 4. It calculates the length of an iterable.

Explanation: The len function in Python is used to calculate the number of items in an iterable or the length of a string.*


130. How can you check if a list is a subset of another list in Python?

Options:

  1. list.is_subset(other_list)
  2. subset(list, other_list)
  3. set(list).issubset(set(other_list))
  4. list.check_subset(other_list)

Correct Answer: 3. set(list).issubset(set(other_list))

Explanation: One way to check if a list is a subset of another list in Python is to convert both lists to sets and then use the issubset method.*

Explanation: One way to check if a list is a subset of another list in Python is to convert both lists to sets and then use the issubset method.*

141. What is the purpose of the isinstance function in Python?

Options:

  1. It checks if an object is an instance of a specific class or type.
  2. It creates a new instance of a class.
  3. It checks if an object is empty.
  4. It is used for instantiating classes.

Correct Answer:

  1. It checks if an object is an instance of a specific class or type.

Explanation: The isinstance function in Python is used to check if an object is an instance of a specified class or type.*


142. How can you reverse the order of characters in a string in Python?

Options:

  1. string.reverse()
  2. reverse(string)
  3. string[::-1]
  4. string.invert()

Correct Answer: 3. string[::-1]

Explanation: Using the slicing syntax [::-1] in Python reverses the order of characters in a string.*


143. What is the purpose of the list function in Python?

Options:

  1. It creates a new list.
  2. It converts an iterable to a list.
  3. It checks if an object is a list.
  4. It is used for list manipulation.

Correct Answer: 2. It converts an iterable to a list.

Explanation: The list function in Python is used to convert an iterable (e.g., tuple, string) to a list.*


144. How can you check if a list is not empty in Python?

Options:

  1. if not list:
  2. if list.not_empty():
  3. if list != []:
  4. if len(list) > 0:

Correct Answer:

  1. if not list:

Explanation: Checking if a list is not empty in Python can be done using the not keyword, as an empty list evaluates to False in a boolean context.*


145. What is the purpose of the ord function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 2. It converts a character to its Unicode code point.

Explanation: The ord function in Python is used to get the Unicode code point of a character.*


146. How can you check if a number is positive in Python?

Options:

  1. number.positive()
  2. positive(number)
  3. number > 0
  4. is_positive(number)

Correct Answer: 3. number > 0

Explanation: Checking if a number is positive in Python can be done by comparing it to zero using the > operator.*


147. What is the purpose of the set function in Python?

Options:

  1. It creates a new set.
  2. It converts an iterable to a set.
  3. It checks if an object is a set.
  4. It is used for set operations.

Correct Answer: 2. It converts an iterable to a set.

Explanation: The set function in Python is used to convert an iterable (e.g., list, tuple) to a set.*


148. How can you check if a string starts with a specific prefix in Python?

Options:

  1. string.starts(prefix)
  2. string.startswith(prefix)
  3. startswith(string, prefix)
  4. prefix.check(string)

Correct Answer: 2. string.startswith(prefix)

Explanation: The startswith method in Python is used to check if a string starts with a specified prefix.*


149. What is the purpose of the max function in Python?

Options:

  1. It finds the maximum value in a list.
  2. It is used for bitwise maximum operations.
  3. It creates a new maximum value.
  4. It returns the index of the maximum element in a list.

Correct Answer:

  1. It finds the maximum value in a list.

Explanation: The max function in Python returns the largest item in an iterable or the largest of two or more arguments.*


150. How can you check if a number is a perfect square in Python?

Options:

  1. number.perfect_square()
  2. perfect_square(number)
  3. number == int(number**0.5)**2
  4. (number & (number - 1)) == 0

Correct Answer: 3. number == int(number**0.5)**2

Explanation: Checking if a number is a perfect square in Python can be done by comparing it to the square of its integer square root.*

151. What is the purpose of the abs function in Python?

Options:

  1. It converts a value to an absolute value.
  2. It is used for abstract mathematical operations.
  3. It calculates the absolute sum of elements in an iterable.
  4. It is used for abstract data structures.

Correct Answer:

  1. It converts a value to an absolute value.

Explanation: The abs function in Python is used to return the absolute value of a number.*


152. How can you convert a string to an integer in Python?

Options:

  1. int(string)
  2. string.to_int()
  3. integer(string)
  4. convert_to_int(string)

Correct Answer:

  1. int(string)

Explanation: The int function in Python is used to convert a string or a number to an integer.*


153. What is the purpose of the any function in Python?

Options:

  1. It checks if all elements in an iterable are true.
  2. It checks if any element in an iterable is true.
  3. It returns the logical AND of all elements in an iterable.
  4. It is used for logical OR operations.

Correct Answer: 2. It checks if any element in an iterable is true.

Explanation: The any function in Python returns True if at least one element of an iterable is true, otherwise it returns False.*


154. How can you find the index of the maximum element in a list in Python?

Options:

  1. list.find_max()
  2. list.index(max(list))
  3. max_index(list)
  4. list.max_index()

Correct Answer: 2. list.index(max(list))

Explanation: The index method in Python is used to find the index of the first occurrence of a specified value in a list.*


155. What is the purpose of the chr function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 3. It generates a character from a Unicode code point.

Explanation: The chr function in Python is used to generate a character from a Unicode code point.*


156. How can you check if a string is empty in Python?

Options:

  1. if not string:
  2. string.is_empty()
  3. if string == ''
  4. string.check_empty()

Correct Answer:

  1. if not string:

Explanation: Checking if a string is empty in Python can be done using the not keyword, as an empty string evaluates to False in a boolean context.*


157. What is the purpose of the filter function in Python?

Options:

  1. It filters elements based on a given condition.
  2. It creates a new list containing the unique elements of an existing list.
  3. It applies a function to all the elements of an iterable.
  4. It is used for filtering files in a directory.

Correct Answer:

  1. It filters elements based on a given condition.

Explanation: The filter function in Python is used to filter elements from an iterable based on a specified function or condition.*


158. How can you find the index of the minimum element in a list in Python?

Options:

  1. min_index(list)
  2. list.index(min(list))
  3. list.find_min()
  4. list.min_index()

Correct Answer: 2. list.index(min(list))

Explanation: The index method in Python is used to find the index of the first occurrence of a specified value in a list.*


159. What is the purpose of the sum function in Python?

Options:

  1. It calculates the sum of all elements in an iterable.
  2. It is used for bitwise sum operations.
  3. It concatenates strings in a list.
  4. It returns the maximum value in an iterable.

Correct Answer:

  1. It calculates the sum of all elements in an iterable.

Explanation: The sum function in Python is used to calculate the sum of all elements in an iterable.*


160. How can you check if a string contains only numeric characters in Python?

Options:

  1. string.isnumeric()
  2. string.isnumericchars()
  3. is_numeric(string)
  4. check_numeric(string)

Correct Answer:

  1. string.isnumeric()

Explanation: The isnumeric method in Python is used to check if all characters in a string are numeric.*

161. What is the purpose of the round function in Python?

Options:

  1. It rounds a floating-point number to the nearest integer.
  2. It is used for rounding up a number.
  3. It is used for mathematical calculations.
  4. It rounds a number to a specified number of decimal places.

Correct Answer: 4. It rounds a number to a specified number of decimal places.

Explanation: The round function in Python is used to round a number to a specified number of decimal places.*


162. How can you check if a list contains duplicates in Python?

Options:

  1. list.has_duplicates()
  2. has_duplicates(list)
  3. len(set(list)) != len(list)
  4. list.contains_duplicates()

Correct Answer: 3. len(set(list)) != len(list)

Explanation: Checking for duplicates in a list in Python can be done by converting the list to a set and comparing the lengths.*


163. What is the purpose of the sorted function in Python?

Options:

  1. It sorts elements in a list in ascending order.
  2. It creates a sorted copy of a set.
  3. It is used for sorting files in a directory.
  4. It sorts characters in a string.

Correct Answer:

  1. It sorts elements in a list in ascending order.

Explanation: The sorted function in Python is used to sort the elements of an iterable in ascending order.*


164. How can you check if a list is a palindrome in Python?

Options:

  1. list.is_palindrome()
  2. is_palindrome(list)
  3. list == list[::-1]
  4. list.check_palindrome()

Correct Answer: 3. list == list[::-1]

Explanation: Checking if a list is a palindrome in Python can be done by comparing the original list with its reverse.*


165. What is the purpose of the tuple function in Python?

Options:

  1. It creates a new tuple.
  2. It converts an iterable to a tuple.
  3. It checks if an object is a tuple.
  4. It is used for tuple operations.

Correct Answer: 2. It converts an iterable to a tuple.

Explanation: The tuple function in Python is used to convert an iterable (e.g., list, string) to a tuple.*


166. How can you check if a string is a valid identifier in Python?

Options:

  1. string.isidentifier()
  2. is_identifier(string)
  3. check_identifier(string)
  4. identifier(string)

Correct Answer:

  1. string.isidentifier()

Explanation: The isidentifier method in Python is used to check if a string is a valid identifier.*


167. What is the purpose of the min function in Python?

Options:

  1. It finds the minimum value in a list.
  2. It is used for bitwise minimum operations.
  3. It creates a new minimum value.
  4. It returns the index of the minimum element in a list.

Correct Answer:

  1. It finds the minimum value in a list.

Explanation: The min function in Python returns the smallest item in an iterable or the smallest of two or more arguments.*


168. How can you check if a string contains only whitespace characters in Python?

Options:

  1. string.iswhitespace()
  2. string.isspace()
  3. is_whitespace(string)
  4. check_whitespace(string)

Correct Answer: 2. string.isspace()

Explanation: The isspace method in Python is used to check if all characters in a string are whitespace characters.*


169. What is the purpose of the len function in Python?

Options:

  1. It calculates the length of a string.
  2. It is used for bitwise length operations.
  3. It returns the index of the maximum element in a list.
  4. It calculates the length of an iterable.

Correct Answer: 4. It calculates the length of an iterable.

Explanation: The len function in Python is used to calculate the number of items in an iterable or the length of a string.*


170. How can you convert a string to uppercase in Python?

Options:

  1. string.upper()
  2. uppercase(string)
  3. convert_to_upper(string)
  4. string.to_uppercase()

Correct Answer:

  1. string.upper()

Explanation: The upper method in Python is used to convert all characters in a string to uppercase.*

171. What is the purpose of the ord function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 2. It converts a character to its Unicode code point.

Explanation: The ord function in Python is used to get the Unicode code point of a character.*


172. How can you check if a string ends with a specific suffix in Python?

Options:

  1. string.ends(suffix)
  2. string.endswith(suffix)
  3. endswith(string, suffix)
  4. suffix.check(string)

Correct Answer: 2. string.endswith(suffix)

Explanation: The endswith method in Python is used to check if a string ends with a specified suffix.*


173. What is the purpose of the sum function in Python?

Options:

  1. It calculates the sum of all elements in an iterable.
  2. It is used for bitwise sum operations.
  3. It concatenates strings in a list.
  4. It returns the maximum value in an iterable.

Correct Answer:

  1. It calculates the sum of all elements in an iterable.

Explanation: The sum function in Python is used to calculate the sum of all elements in an iterable.*


174. How can you remove leading and trailing whitespace from a string in Python?

Options:

  1. trim(string)
  2. string.trim()
  3. string.strip()
  4. remove_whitespace(string)

Correct Answer: 3. string.strip()

Explanation: The strip method in Python is used to remove leading and trailing whitespace from a string.*


175. What is the purpose of the map function in Python?

Options:

  1. It is used for creating maps.
  2. It applies a function to all the elements of an iterable.
  3. It generates a map object.
  4. It creates a dictionary.

Correct Answer: 2. It applies a function to all the elements of an iterable.

Explanation: The map function in Python applies a specified function to all items in an input iterable (list, tuple, etc.) and returns an iterator that produces the results.*


176. How can you concatenate two lists in Python?

Options:

  1. list.concat(other_list)
  2. list + other_list
  3. concat(list, other_list)
  4. list.combine(other_list)

Correct Answer: 2. list + other_list

Explanation: The + operator in Python is used for concatenating two lists.*


177. What is the purpose of the float function in Python?

Options:

  1. It is used for floating-point arithmetic.
  2. It converts a number to a floating-point number.
  3. It creates a new float object.
  4. It is used for floating-point comparison.

Correct Answer: 2. It converts a number to a floating-point number.

Explanation: The float function in Python is used to convert a number or a string containing a number to a floating-point number.*


178. How can you check if a number is negative in Python?

Options:

  1. number < 0
  2. is_negative(number)
  3. number.is_negative()
  4. negative(number)

Correct Answer:

  1. number < 0

Explanation: Checking if a number is negative in Python can be done by comparing it to zero using the < operator.*


179. What is the purpose of the zip function in Python?

Options:

  1. It compresses files.
  2. It creates a ZIP archive.
  3. It combines two or more iterables element-wise.
  4. It is used for zipping folders.

Correct Answer: 3. It combines two or more iterables element-wise.

Explanation: The zip function in Python is used to combine elements from two or more iterables (e.g., lists, tuples) element-wise.*


180. How can you convert a list of strings to a single string in Python?

Options:

  1. ' '.join(list)
  2. list.to_string()
  3. convert_to_string(list)
  4. string(list)

Correct Answer:

  1. ' '.join(list)

Explanation: The join method in Python is used to concatenate a list of strings into a single string.*

181. What is the purpose of the isalpha method in Python?

Options:

  1. It checks if a string contains only alphabetic characters.
  2. It converts a string to lowercase.
  3. It checks if a string is empty.
  4. It removes all whitespace from a string.

Correct Answer:

  1. It checks if a string contains only alphabetic characters.

Explanation: The isalpha method in Python is used to check if all characters in a string are alphabetic.*


182. How can you find the index of the first occurrence of a substring in a string in Python?

Options:

  1. string.find(substring)
  2. string.index(substring)
  3. substring.first_index(string)
  4. find(string, substring)

Correct Answer:

  1. string.find(substring)

Explanation: The find method in Python is used to find the index of the first occurrence of a specified substring in a string.*


183. What is the purpose of the enumerate function in Python?

Options:

  1. It counts the number of elements in a list.
  2. It creates a new list.
  3. It returns the index and value of each element in an iterable.
  4. It is used for enumerating characters in a string.

Correct Answer: 3. It returns the index and value of each element in an iterable.

Explanation: The enumerate function in Python is used in loops to iterate over both the index and the value of an iterable.*


184. How can you check if a list is a subset of another list in Python?

Options:

  1. list.is_subset(other_list)
  2. subset(list, other_list)
  3. set(list).issubset(set(other_list))
  4. list.check_subset(other_list)

Correct Answer: 3. set(list).issubset(set(other_list))

Explanation: One way to check if a list is a subset of another list in Python is to convert both lists to sets and then use the issubset method.*


185. What is the purpose of the chr function in Python?

Options:

  1. It calculates the absolute value of a number.
  2. It converts a character to its Unicode code point.
  3. It generates a character from a Unicode code point.
  4. It is used for character mapping.

Correct Answer: 3. It generates a character from a Unicode code point.

Explanation: The chr function in Python is used to generate a character from a Unicode code point.*


186. How can you check if a value is present in a list in Python?

Options:

  1. value.check(list)
  2. list.contains(value)
  3. value in list
  4. list.check_value(value)

Correct Answer: 3. value in list

Explanation: The in keyword in Python is used to check if a value is present in a list.*


187. What is the purpose of the isdigit method in Python?

Options:

  1. It checks if a string is composed of digits only.
  2. It converts a string to an integer.
  3. It is used for string formatting.
  4. It checks if a string contains only alphanumeric characters.

Correct Answer:

  1. It checks if a string is composed of digits only.

Explanation: The isdigit method in Python is used to check if a string consists of digits only.*


188. How can you convert a list of integers to a list of strings in Python?

Options:

  1. [str(list)]
  2. convert_to_strings(list)
  3. [str(x) for x in list]
  4. list.to_strings()

Correct Answer: 3. [str(x) for x in list]

Explanation: List comprehension can be used to create a new list where each integer is converted to a string using str(x).*


189. What is the purpose of the map function in Python?

Options:

  1. It is used for creating maps.
  2. It applies a function to all the elements of an iterable.
  3. It generates a map object.
  4. It creates a dictionary.

Correct Answer: 2. It applies a function to all the elements of an iterable.

Explanation: The map function in Python applies a specified function to all items in an input iterable (list, tuple, etc.) and returns an iterator that produces the results.*


190. How can you check if a number is even in Python?

Options:

  1. number.is_even()
  2. is_even(number)
  3. number % 2 == 0
  4. even(number)

Correct Answer: 3. number % 2 == 0

Explanation: Checking if a number is even in Python can be done using the modulus operator %. If the result is 0, the number is even.*

Explanation: Checking if a number is even in Python can be done using the modulus operator %. If the result is 0, the number is even.*