Python MCQs Test 3 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 How do you remove an item from a set named my_set in Python? my_set.remove(item) my_set.discard(item) my_set.pop() my_set.delete(item) 3 / 20 What does the max() function return in Python? The largest element in an iterable The smallest element in an iterable The sum of all elements in an iterable The average of all elements in an iterable 4 / 20 Which of the following is NOT a valid data type in Python? Float Double Boolean Complex 5 / 20 What is the output of the following code? \npython\nx = "hello"\nprint(x[::-1]) hello elloh olleh h 6 / 20 What does the is keyword do in Python? Checks if two variables refer to the same object Checks if two variables have the same value Checks if a variable is empty Checks if a variable is of a certain type 7 / 20 What is the output of the following code? \npython\nx = [1, 2, 3]\nx.append(4)\nprint(x) [1, 2, 3, 4] [4, 3, 2, 1] [1, 2, 3] [1, 2, 3, [4]] 8 / 20 Which of the following is NOT a valid way to concatenate strings in Python? "hello" + "world" concat("hello", "world") "".join(["hello", "world"]) f"hello{'world'}" 9 / 20 What does the split() method do in Python? Splits a string into a list of substrings Joins a list of strings into one string Removes leading and trailing whitespaces Reverses the characters in a string 10 / 20 What is the output of the following code? \npython\nprint(2 ** 3) 8 6 16 23 11 / 20 What is the purpose of the else statement in Python loops? It is executed if the loop terminates normally without encountering a break statement It is executed if the loop encounters a continue statement It is executed if the loop encounters an error It is executed if the loop terminates due to an exception 12 / 20 How do you check if a key exists in a dictionary named my_dict in Python? key in my_dict key.exists(my_dict) exists(key, my_dict) my_dict.hasKey(key) 13 / 20 What is the result of the expression 4 + 5 * 2 / 2 in Python? 4 9 13 14 14 / 20 What is the output of the following code? \npython\nx = "hello"\nprint(x[1:3]) hel el lo hell 15 / 20 Which of the following is the correct way to open a file named example.txt in Python for writing? open("example.txt", "r") open("example.txt", "write") open("example.txt", "w") write("example.txt") 16 / 20 What is the output of the following code? \npython\nprint("Python".find("t")) 2 1 0 -1 17 / 20 What is the output of the following code? \npython\nprint("Python".find("t")) 2 1 0 -1 18 / 20 What does the isdigit() method do in Python? Checks if all characters in a string are digits Converts a string to lowercase Checks if all characters in a string are alphabetic Checks if a string is empty 19 / 20 Which of the following is NOT a valid way to define a tuple in Python? my_tuple = (1, 2, 3) my_tuple = 1, 2, 3 my_tuple = [1, 2, 3] my_tuple = tuple([1, 2, 3]) 20 / 20 What does the remove() method do in Python? Removes the first occurrence of a specified element from a list Adds an element to the end of a list Removes the last element of a list Returns the index of a specified element in a list Your score isThe average score is 65% 0% Restart Your Test