Python MCQs Test 5 Python MCQs Test 5 1 / 20 What is the purpose of the finally block in a try-except statement in Python? It is always executed regardless of whether an exception occurs or not It is executed if an exception occurs It is executed if no exception occurs It is executed before the try block 2 / 20 What does the clear() method do in Python lists? Removes all items from the list Returns a shallow copy of the list Returns the value associated with a specified key Updates the list with elements from another list 3 / 20 What is the output of the following code? \npython\nx = [1, 2, 3]\nprint(x[::-1]) [1, 2, 3] [3, 2, 1] [3, 1] [1, 3] 4 / 20 Which of the following is the correct way to open a file named example.txt in Python for reading and writing in binary mode? open("example.txt", "rb+") open("example.txt", "r+b") open("example.txt", "readwritebinary") open("example.txt", "r+b+") 5 / 20 What is the output of the following code? \npython\nprint("hello".replace("l", "L")) hello heLLo heLo heLoL 6 / 20 What does the enumerate() function do in Python? Returns an iterator that yields tuples of an index and an item Returns the length of an iterable Returns the sum of all elements in an iterable Returns a reversed version of the iterable 7 / 20 What is the output of the following code? \npython\nx = 10\ny = 3\nprint(x % y) 1 2 3 0 8 / 20 Which of the following is NOT a valid way to remove an item from a list in Python? my_list.remove(item) my_list.pop(index) del my_list[index] my_list.delete(item) 9 / 20 What does the values() method return in Python dictionaries? A list of all the values in the dictionary A list of all the keys in the dictionary A list of tuples containing key-value pairs A generator object of the values 10 / 20 What is the output of the following code? \npython\nx = [1, 2, 3]\nprint(len(x)) 1 2 3 4 11 / 20 What is the purpose of the continue statement in a loop? Skips the current iteration and continues with the next one Exits the loop entirely Pauses the loop temporarily Restarts the loop from the beginning 12 / 20 What does the min() function return in Python? The smallest element in an iterable The largest element in an iterable The sum of all elements in an iterable The average of all elements in an iterable 13 / 20 What is the output of the following code? \npython\nx = [1, 2, 3]\nprint(x[-1]) 1 2 3 None 14 / 20 Which of the following is the correct way to open a file named example.txt in Python for reading as binary? open("example.txt", "rb") open("example.txt", "r") open("example.txt", "readbinary") open("example.txt", "readb") 15 / 20 What is the output of the following code? \npython\nprint("python".count("th")) 0 1 2 3 16 / 20 What does the keys() method return in Python dictionaries? A list of all the keys in the dictionary A list of all the values in the dictionary A list of tuples containing key-value pairs A generator object of the keys 17 / 20 What is the output of the following code? \npython\nx = (1, 2, 3)\nprint(x[0]) 1 2 3 (1, 2, 3) 18 / 20 Which of the following is NOT a valid way to create a tuple in Python? my_tuple = (1, 2, 3) my_tuple = tuple(1, 2, 3) my_tuple = 1, 2, 3 my_tuple = (1,) 19 / 20 What does the del keyword do in Python? Deletes a variable or element from a list Declares a new variable Imports a module Converts a string to lowercase 20 / 20 What is the output of the following code? \npython\nx = "hello"\nprint(x[1]) e h I O Your score isThe average score is 85% 0% Restart Your Test