0% 0 votes, 0 avg 10 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 All The Best Python Quiz 1 / 52 Which built-in function is used to get input from the user? read() read_line() get_input() input() Explanation: The input() function reads a line from input, converts it to a string (stripping the trailing newline), and returns that string. 2 / 52 What is the purpose of the else block with a for loop? It defines an alternative loop It executes if the loop is interrupted It executes after the loop completes normally It executes if the loop does not run Explanation: The else block associated with a for loop executes only if the loop completes without encountering a break statement. 3 / 52 What is the output of print(10 % 3)? 10 3 1 0 Explanation: The % operator is the modulo operator, which returns the remainder of the division. 10 divided by 3 is 3 with a remainder of 1. 4 / 52 Which of the following is the correct way to import a module named math? import math use math include math require math Explanation: The import keyword is used to bring modules into the current namespace. 5 / 52 What does the dir() function do without arguments? Returns a list of attributes of the current scope Returns a list of built-in functions Returns a list of available modules Returns a list of local variables Explanation: When dir() is called without arguments, it returns the list of names in the current local scope. 6 / 52 How do you comment out a single line in Python? // This is a comment # This is a comment /* This is a comment */ -- This is a comment Explanation: In Python, the # symbol is used to denote a single-line comment. Everything after # on that line is ignored by the interpreter. 7 / 52 Which of the following is used to iterate over a sequence (e.g., a list)? for loop if statement while loop do-while loop Explanation: The for loop is primarily used to iterate over elements of a sequence (like a list, tuple, string, or range). 8 / 52 What is the output of print("Python"[::-1])? Error nohtyP Python PythoN Explanation: [::-1] is a slicing technique that reverses a sequence (like a string or list). It creates a reversed copy of the string. 9 / 52 Which of the following is used for multiline comments in Python? // This is a multiline comment // ## This is a multiline comment ## /* This is a multiline comment */ ''' This is a multiline comment ''' Explanation: Python does not have specific syntax for multiline comments like /* ... */ in other languages. Instead, triple quotes (''' or """) are often used. If they are not assigned to a variable, they act as string literals and are effectively ignored by the interpreter, serving as multiline comments. 10 / 52 What is the scope of a variable defined inside a function? Global scope Module scope Local scope Class scope Explanation: Variables defined inside a function have local scope, meaning they are only accessible within that function. 11 / 52 Which method is used to remove an item from a dictionary by its key? dict.discard(key) dict.delete(key) dict.pop(key) dict.remove(key) Explanation: The pop() method is used to remove a key-value pair from a dictionary by specifying the key. It also returns the value associated with the removed key. 12 / 52 Which of the following is NOT a valid variable name in Python? 2my_var my_var_2 _my_var myVar Explanation: Variable names in Python cannot start with a number. They can contain letters, numbers, and underscores, but must start with a letter or an underscore. 13 / 52 How do you add an element to the end of a list in Python? my_list.append(element) my_list.add(element) my_list.insert(element) my_list.push(element) Explanation: The append() method is used to add a single element to the end of a list. 14 / 52 What is the output of print(len("Hello"))? 5 6 4 Error Explanation: The len() function returns the number of items in an object. For a string, it returns the number of characters. "Hello" has 5 characters. 15 / 52 What is the name of the built-in function that returns the absolute value of a number? fabs() mod() abs() absolute() Explanation: The abs() built-in function returns the absolute value of a number. 16 / 52 How do you get the length of a list named my_list? my_list.size() my_list.length() length(my_list) len(my_list) Explanation: The built-in len() function is used to get the number of items in an object, including lists, strings, and tuples. 17 / 52 What is the purpose of pip in Python? A Python integrated development environment A code debugger A package installer for Python A text editor Explanation: pip is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not part of the standard Python library. 18 / 52 What is the output of print("Python".upper())? Python python PYTHON PyThOn Explanation: The upper() string method returns a new string where all characters in the original string are converted to uppercase. 19 / 52 Which operator is used for exponentiation in Python? * ^ ** // Explanation: The ** operator is used for exponentiation (raising to a power) in Python. For example, 2 ** 3 would be 8. 20 / 52 What is the output of print(type(lambda: None))? Object Error <class 'function'> None Explanation: lambda creates an anonymous function. The type() of a lambda function is <class 'function'>. 21 / 52 Which of the following is a valid way to create an empty set in Python? my_set = () my_set = set() my_set = {} my_set = [] Explanation: To create an empty set, you must use the set() constructor. {} creates an empty dictionary, not an empty set. 22 / 52 How do you check if an item exists in a list? exists(item, my_list) my_list.contains(item) item in my_list my_list.has(item) Explanation: The in operator is used to check for membership in sequences like lists, strings, and tuples. It returns True if the item is found, False otherwise. 23 / 52 How do you concatenate two strings s1 and s2? concat(s1, s2) s1.append(s2) s1.concat(s2) s1 + s2 Explanation: The + operator is commonly used for string concatenation in Python. 24 / 52 What does the pass statement do in Python? It raises an error It exits the program It skips the current iteration It does nothing Explanation: The pass statement is a null operation; nothing happens when it is executed. It's often used as a placeholder where a statement is syntactically required but you don't want any code to execute. 25 / 52 What is the purpose of the break statement in loops? To skip the current iteration To restart the loop To continue to the next iteration To terminate the loop entirely Explanation: The break statement immediately terminates the loop (either for or while) and transfers control to the statement immediately following the loop. 26 / 52 What is the purpose of the if statement in Python? To handle exceptions To declare a variable To perform conditional execution To define a loop Explanation: The if statement is a conditional statement that executes a block of code only if a specified condition is true. 27 / 52 Which of the following is a mutable data type in Python? Integer Tuple List String Explanation: Mutable data types can be changed after they are created. Lists are mutable, meaning you can add, remove, or modify elements. Tuples and Strings are immutable. 28 / 52 What is the output of print("Python"[2])? h t y o Explanation: String indexing in Python starts from 0. The character at index 2 in "Python" is 't'. 29 / 52 Which of the following is used to define a block of code in Python? Indentation Curly braces {} Square brackets [] Parentheses () Explanation: Python uses indentation (whitespace) to define blocks of code (e.g., within if statements, loops, functions, and classes), unlike other languages that use curly braces. 30 / 52 How do you remove duplicate elements from a list while preserving order? Using a for loop and if condition to check for duplicates list.unique() list.remove_duplicates() Using set() and then converting back to list Explanation: While converting to a set() and back to a list can remove duplicates, it doesn't guarantee order preservation. The most common way to remove duplicates while preserving order is to iterate through the list and add elements to a new list only if they haven't been seen before. 31 / 52 What will be the output of the following Python code? 5 15 1 10 32 / 52 What is the output of the following Python code ? 10 False 5 True Step by step: x > y = False, x and y = 10 (truthy value), so False or 10 = 10. The trick: Python's 'or' returns the first truthy value, not just True/False! This catches many beginners off-guard. 33 / 52 What does __init__ method do in a Python class? It defines a method It is a constructor for the class It cleans up resources It initializes a variable Explanation: The __init__ method is a special method in Python classes that acts as a constructor. It is automatically called when a new instance of the class is created. 34 / 52 What is the correct way to create a list in Python? my_list = [1, 2, 3] my_list = {1, 2, 3} my_list = (1, 2, 3) my_list = Explanation: Lists in Python are created using square brackets []. Tuples use parentheses (), and sets/dictionaries use curly braces {}. 35 / 52 How do you open a file in Python for writing? open("file.txt", "x") open("file.txt", "w") open("file.txt", "r") open("file.txt", "a") Explanation: The open() function is used to open files. The "w" mode opens a file for writing. If the file exists, its content is truncated (emptied); if it doesn't exist, a new file is created. 36 / 52 Which keyword is used to define a function in Python? define func function def Explanation: The def keyword is used to define a new function in Python. 37 / 52 Which module in Python is commonly used for working with regular expressions? re sys math os Explanation: The re module provides regular expression operations. 38 / 52 Which of the following is an immutable data type? Tuple List Set Dictionary Explanation: Immutable data types cannot be changed after they are created. Tuples are immutable; lists, dictionaries, and sets are mutable. 39 / 52 What is the output of 2 + 3 * 4? 10 14 24 20 Explanation: Python follows the order of operations (PEMDAS/BODMAS). Multiplication is performed before addition: 3∗4=12, then 2+12=14. 40 / 52 What is the purpose of the elif keyword in Python? To handle errors To define an else block To check multiple conditions sequentially To start a new if condition Explanation: elif (short for "else if") is used in if...elif...else statements to check multiple conditions sequentially after an initial if condition. 41 / 52 How do you convert an integer to a string in Python? convert_str(integer_var) to_string(integer_var) int_to_str(integer_var) str(integer_var) Explanation: The str() built-in function is used to convert values to their string representation. 42 / 52 How do you create a dictionary in Python? my_dict = {key: value} my_dict = my_dict = [key: value] my_dict = (key: value) Explanation: Dictionaries in Python are created using curly braces {} with key-value pairs separated by colons. 43 / 52 What is the output of print("Hello" + " " + "World")? Hello+World Hello world HelloWorld Hello World Explanation: The + operator performs string concatenation in Python. It joins the strings together. 44 / 52 How do you find the maximum value in a list named numbers? max(numbers) numbers.get_max() maximum(numbers) numbers.max() Explanation: The built-in max() function returns the largest item in an iterable or the largest of two or more arguments. 45 / 52 Which of the following is not a valid Python data type? String Integer Character Float Explanation: Python has Integer, Float, and String as built-in data types. "Character" is not a distinct data type; single characters are represented as strings of length 1. 46 / 52 How do you remove an element from a list by its index? del my_list[index] my_list.pop_at(index) my_list.delete(index) my_list.remove_at(index) Explanation: The del statement can be used to remove an item from a list at a specified index. my_list.pop(index) also works and returns the removed item. 47 / 52 What is the output of print(3 // 2)? 1.5 0 1 2 Explanation: The // operator performs floor division (integer division), which rounds the result down to the nearest whole number. 3/2=1.5, and floor division results in 1. 48 / 52 What is the output of print(type(10.5))? 10.5 <class 'float'> Error Explanation: 10.5 is a floating-point number, so its type is <class 'float'> 49 / 52 What is the output of print(bool(0))? True False None 0 Explanation: In Python, 0 (integer zero) is considered "falsy." When converted to a boolean, it evaluates to False. Other falsy values include None, empty strings, empty lists, empty tuples, and empty dictionaries. 50 / 52 Which statement is used to handle exceptions in Python? catch...throw try...except check...error handle...fault Explanation: The try...except block is used for exception handling in Python. Code that might raise an exception is placed in the try block, and the except block handles specific exceptions. 51 / 52 What is the default value of arguments in a Python function if not specified? None False "" 0 Explanation: If a function argument is not provided a value when the function is called, and no default value is specified in the function definition, Python assigns None to that argument. 52 / 52 What is the output of [1, 2, 3] * 2? [1, 2, 3] [2, 4, 6] Error [1, 2, 3, 1, 2, 3] Explanation: When a list is multiplied by an integer, it creates a new list by concatenating the original list with itself that many times. Your score isThe average score is 25% 0% Restart quiz