







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








































1. What is the correct syntax to print "Hello, World!" in Python? A) echo "Hello, World!" B) print("Hello, World!") C) printf("Hello, World!") D) cout << "Hello, World!" Answer: B Explanation: Python uses the print() function to display output to the console. 2. Which of the following is a valid variable name in Python? A) 2ndVariable B) variable-name C) _variable D) variable name Answer: C Explanation: Variable names must begin with a letter or underscore and cannot contain spaces or hyphens. 3. How do you convert the string "123" into an integer in Python? A) int("123") B) str("123") C) float("123") D) to_int("123") Answer: A Explanation: The int() function converts a string representing an integer to an actual integer type. 4. Which data type in Python is immutable? A) List B) Dictionary C) Set D) Tuple Answer: D Explanation: Tuples are immutable, meaning once created, their elements cannot be modified. 5. What is the output of: print(3 + 4 * 2)? A) 14 B) 11 C) 10 D) 7 Answer: B Explanation: Python follows operator precedence; multiplication is done before addition (4*2=8; 3+8=11).
6. Which operator is used for exponentiation in Python? A) ^ B) ** C) exp() D) ^^ Answer: B Explanation: The ** operator is used for exponentiation in Python. 7. What will be the output of: print("Hello" + "World")? A) Hello World B) HelloWorld C) Hello+World D) Error Answer: B Explanation: The + operator concatenates strings without any space unless explicitly added. 8. Which keyword is used to create a function in Python? A) func B) define C) def D) function Answer: C Explanation: The def keyword is used to define a function in Python. 9. What is the purpose of the return statement in a function? A) To exit the loop B) To output a value from the function C) To display a message D) To start the function Answer: B Explanation: The return statement sends a value back to the caller of the function. 10. How do you start a single-line comment in Python? A) // comment B) # comment C) D) /* comment */ Answer: B Explanation: The hash symbol (#) is used to denote single-line comments in Python. 11. Which of the following is a correct if-statement syntax in Python? A) if x > 10 { print(x) } B) if (x > 10): print(x) C) if x > 10 then print(x) D) if x > 10; print(x)
17. How do you access the first element in a list called myList? A) myList[1] B) myList[0] C) myList.first() D) myList(-1) Answer: B Explanation: Python lists are zero-indexed; the first element is at index 0. 18. Which of these is a valid way to create an empty dictionary in Python? A) {} B) [] C) () D) <> Answer: A Explanation: Curly braces {} define an empty dictionary in Python. 19. What is the result of len("Python")? A) 5 B) 6 C) 7 D) Error Answer: B Explanation: The len() function returns the number of characters in a string, which is 6 for "Python". 20. How do you create a tuple with a single element 5? A) (5) B) (5,) C) [5] D) {5} Answer: B Explanation: A single-element tuple requires a trailing comma to distinguish it from a regular parenthesis. 21. Which of the following is used to iterate over the keys of a dictionary? A) dict.keys() B) dict.values() C) dict.items() D) for key in dict: Answer: D Explanation: Iterating directly over the dictionary yields its keys by default. 22. What is the purpose of the break statement in a loop? A) To skip to the next iteration B) To exit the loop entirely C) To pause the loop
D) To restart the loop Answer: B Explanation: The break statement immediately terminates the loop.
23. Which loop control statement is used to skip the current iteration and continue with the next one? A) break B) exit C) continue D) pass Answer: C Explanation: The continue statement skips the rest of the current loop iteration. 24. What does the pass statement do in Python? A) Stops the program B) Acts as a placeholder with no action C) Skips to the next loop iteration D) Exits a function Answer: B Explanation: The pass statement is a no-operation statement, useful as a placeholder. 25. How do you define a function with parameters in Python? A) function myFunc(param): B) def myFunc(param): C) func myFunc(param): D) define myFunc(param): Answer: B Explanation: The def keyword is used to define a function along with its parameters. 26. What is the scope of a variable defined inside a function? A) Global scope B) Local scope C) Module scope D) Class scope Answer: B Explanation: Variables defined inside a function are local to that function. 27. How do you import the math module in Python? A) import math B) include math C) using math D) require math Answer: A Explanation: The import statement is used to load modules such as math.
Answer: B Explanation: The raise statement is used to trigger an exception manually.
34. Which exception is raised when a division by zero occurs in Python? A) ZeroDivisionError B) ArithmeticError C) ValueError D) DivisionError Answer: A Explanation: ZeroDivisionError is the exception raised for division by zero. 35. What is the primary difference between a class and an object in Python? A) A class is an instance of an object B) An object is a blueprint for a class C) A class is a blueprint, and an object is an instance created from that blueprint D) There is no difference Answer: C Explanation: A class defines a template, and an object is an instantiation of that class. 36. Which method is automatically called when an object is created? A) init () B) start () C) create () D) new () Answer: A Explanation: The init () method initializes a new object once it has been created. 37. What is inheritance in object-oriented programming? A) A way to overload functions B) A method to create a new class based on an existing class C) A way to encapsulate data D) A method to hide variables Answer: B Explanation: Inheritance allows a new class to derive properties and methods from an existing class. 38. Which of the following supports multiple inheritance in Python? A) Only single inheritance is allowed B) Only hierarchical inheritance is allowed C) Python classes can inherit from multiple classes D) Inheritance is not supported Answer: C Explanation: Python supports multiple inheritance, allowing a class to inherit from more than one parent class.
39. What does encapsulation in OOP mean? A) Combining data and functions into a single unit B) Hiding internal details from the user C) Inheriting properties from another class D) Both A and B Answer: D Explanation: Encapsulation involves bundling data and methods together while hiding the internal state. 40. Which concept allows different classes to define methods with the same name? A) Inheritance B) Polymorphism C) Encapsulation D) Abstraction Answer: B Explanation: Polymorphism allows different classes to have methods with the same name, with behavior specific to each class. 41. What is an iterator in Python? A) A function that returns a list B) An object that can be iterated upon C) A loop structure D) A built-in function for looping Answer: B Explanation: An iterator is an object that implements the iter () and next () methods, enabling iteration. 42. Which keyword is used to create a generator in Python? A) yield B) return C) generate D) produce Answer: A Explanation: The yield keyword is used in a function to make it a generator that can yield a sequence of values. 43. What is a list comprehension in Python? A) A method to iterate over lists using loops B) A concise way to create lists C) A method to reverse a list D) A function to compare lists Answer: B Explanation: List comprehensions provide a compact syntax to generate lists from existing iterables.
B) os C) math D) datetime Answer: B Explanation: The os module provides a portable way of using operating system-dependent functionality.
50. How do you get the current date and time in Python using the datetime module? A) datetime.now() B) datetime.today() C) datetime.current() D) datetime.getTime() Answer: A Explanation: The now() method of the datetime module returns the current local date and time. 51. Which module in Python is used for calendar-related functions? A) time B) datetime C) calendar D) schedule Answer: C Explanation: The calendar module provides functions related to calendar operations, such as generating calendars. 52. What does the collections module provide in Python? A) Advanced mathematical operations B) Specialized container datatypes C) File handling utilities D) Network operations Answer: B Explanation: The collections module includes specialized container datatypes like deque, namedtuple, and Counter. 53. Which function is used to test code with the unittest framework? A) runTest() B) assertTest() C) unittest.main() D) test.run() Answer: C Explanation: The unittest.main() function is used to run all tests when executing a test script. 54. What does the pass statement do in an empty function? A) Causes an error B) Allows the function to be defined without any code C) Automatically returns None D) Skips the function call
Answer: B Explanation: The pass statement is a null operation; it allows a function or block to be syntactically correct without executing any code.
55. Which module in Python is commonly used for CSV file parsing? A) csv B) json C) xml D) fileparse Answer: A Explanation: The csv module provides functionality to read from and write to CSV files. 56. What method would you use to convert a Python object to a JSON string? A) json.loads() B) json.dumps() C) json.parse() D) json.encode() Answer: B Explanation: The json.dumps() method serializes a Python object into a JSON formatted string. 57. Which module is used to parse XML files in Python? A) xml.etree.ElementTree B) xml.parse C) json D) csv Answer: A Explanation: The xml.etree.ElementTree module is part of the Python standard library for parsing XML data. 58. Which library is popular for data manipulation in Python? A) matplotlib B) numpy C) pandas D) scipy Answer: C Explanation: pandas is widely used for data manipulation and analysis. 59. Which library is primarily used for data visualization in Python? A) pandas B) numpy C) matplotlib D) requests Answer: C Explanation: matplotlib is a comprehensive library for creating static, animated, and interactive visualizations.
Answer: B Explanation: The GET method requests data from a specified resource.
66. In Python, which module is commonly used for socket programming? A) os B) sys C) socket D) network Answer: C Explanation: The socket module provides access to the BSD socket interface for networking. 67. What does the client-server model refer to? A) A design pattern for graphical user interfaces B) A network architecture where clients request services and servers provide them C) A database structure D) A module in Python Answer: B Explanation: The client-server model describes how networked computers interact with clients requesting resources from servers. 68. How do you create a basic socket in Python? A) socket.create() B) socket.socket() C) new socket() D) Socket() Answer: B Explanation: The socket.socket() function is used to create a new socket object in Python. 69. Which of the following is not a valid data type in Python? A) List B) Tuple C) Array D) Dictionary Answer: C Explanation: Python does not have a built-in array type; lists are used instead, though arrays can be created using modules. 70. What does the modulus operator (%) do in Python? A) Divides two numbers B) Returns the remainder of a division C) Multiplies two numbers D) Returns the quotient of a division Answer: B Explanation: The modulus operator returns the remainder after division.
71. What is the output of: print("Hello".lower())? A) hello B) HELLO C) Hello D) Error Answer: A Explanation: The lower() method converts all characters in a string to lowercase. 72. Which function would you use to obtain the absolute value of a number in Python? A) abs() B) absolute() C) mod() D) floor() Answer: A Explanation: The abs() function returns the absolute value of a number. 73. What is the purpose of the sorted() function in Python? A) To sort a list in place B) To return a sorted list from an iterable C) To reverse a list D) To count items in a list Answer: B Explanation: The sorted() function returns a new sorted list without modifying the original iterable. 74. How do you reverse a list called myList in Python? A) myList.reverse() B) reverse(myList) C) myList = myList[::-1] D) Both A and C Answer: D Explanation: Both the reverse() method and slicing with [::-1] can reverse a list. 75. What will the expression: "Python"[1:4] return? A) "Pyt" B) "yth" C) "ytho" D) "tho" Answer: B Explanation: Slicing [1:4] returns characters from index 1 up to, but not including, index 4. 76. Which of the following statements correctly creates a set in Python? A) set = {1, 2, 3} B) set = [1, 2, 3] C) set = (1, 2, 3) D) set = {1:2, 3:4}
B) Removes leading and trailing whitespace C) Replaces whitespace with underscores D) Splits the string into words Answer: B Explanation: The strip() method removes any leading and trailing whitespace from a string.
83. Which operator is used to check if two values are equal? A) = B) == C) != D) <> Answer: B Explanation: The equality operator == checks if two values are equal. 84. How do you check for inequality in Python? A) <> B) != C) not= D) =! Answer: B Explanation: The != operator is used to determine if two values are not equal. 85. Which of the following is the correct way to define a list in Python? A) list = (1, 2, 3) B) list = [1, 2, 3] C) list = {1, 2, 3} D) list = <1, 2, 3> Answer: B Explanation: Square brackets [] denote a list in Python. 86. What does the index() method do when used with a list? A) Returns the index of the first occurrence of a value B) Sorts the list C) Returns the element at a given index D) Inserts an element at the given index Answer: A Explanation: The index() method returns the first index at which a specified value is found. 87. How do you insert an element at index 2 in a list called myList? A) myList.add(2, element) B) myList.insert(2, element) C) myList.append(2, element) D) myList.put(2, element) Answer: B Explanation: The insert() method adds an element at the specified index in the list.
88. What is the output of: print(2 ** 3)? A) 6 B) 8 C) 9 D) 5 Answer: B Explanation: 2 raised to the power of 3 is 8. 89. Which function returns the maximum value from a list? A) max() B) high() C) top() D) maximum() Answer: A Explanation: The max() function returns the largest item in an iterable. 90. What does the min() function do? A) Returns the smallest item in an iterable B) Returns the first item in an iterable C) Sorts the iterable in ascending order D) Calculates the average Answer: A Explanation: The min() function returns the smallest element from an iterable. 91. Which keyword is used to define an anonymous function in Python? A) def B) lambda C) anonymous D) func Answer: B Explanation: Lambda is used to create anonymous functions in Python. 92. What is the result of: sorted([3, 1, 2])? A) [3, 1, 2] B) [1, 2, 3] C) [2, 3, 1] D) Error Answer: B Explanation: The sorted() function returns a new list that is the sorted version of the original list. 93. What is the purpose of the map() function in Python? A) To filter items in an iterable B) To apply a function to every item in an iterable C) To sort an iterable D) To reduce an iterable Answer: B
B) <class 'float'> C) <class 'double'> D) <class 'str'> Answer: B Explanation: The type() function returns the data type of the given value, and 3.14 is a float.
100. Which of the following is a valid function definition with a default parameter? A) def func(x=10): B) def func(x:10): C) def func(x==10): D) def func(default=10): Answer: A Explanation: The correct syntax for default parameters is using the equals sign, as in x=10. 101. How do you handle multiple exceptions in one except block? A) except Exception1, Exception2: B) except (Exception1, Exception2): C) except Exception1 or Exception2: D) except Exception1 and Exception2: Answer: B Explanation: Parentheses allow you to catch multiple exceptions in a single except block. 102. What is the output of: print("Hello"[::-1])? A) Hello B) olleH C) Hlleo D) Error Answer: B Explanation: Slicing with [::-1] reverses the string. 103. Which method is used to convert a string to uppercase in Python? A) toUpper() B) upper() C) capitalize() D) casefold() Answer: B Explanation: The upper() method converts all characters in a string to uppercase. 104. How do you check if an element exists in a list? A) element in list B) list.contains(element) C) list.exists(element) D) element has list Answer: A Explanation: The in keyword is used to check membership in a list.
105. What is the purpose of the str() method in a class? A) To provide a string representation of an object B) To initialize an object C) To compare two objects D) To delete an object Answer: A Explanation: The str () method returns a human-readable string representation of the object. 106. Which of the following is a valid way to create a class in Python? A) class MyClass: B) def MyClass(): C) MyClass = class(): D) new class MyClass: Answer: A Explanation: The correct syntax for defining a class in Python starts with the class keyword followed by the class name. 107. What is the use of the init() method in Python classes? A) To create a new class B) To initialize a new object C) To delete an object D) To represent the object as a string Answer: B Explanation: The init () method is called when an object is instantiated to initialize its attributes. 108. What is multiple inheritance? A) A class inheriting from more than one parent class B) A class inheriting from one parent class C) A class that does not inherit from any class D) A class that inherits from a built-in type only Answer: A Explanation: Multiple inheritance allows a class to inherit attributes and methods from multiple parent classes. 109. Which keyword is used to call the parent class’s constructor in Python? A) super() B) parent() C) base() D) init() Answer: A Explanation: The super() function is used to call methods from a parent class. 110. What is the output of: list(range(5))? A) [0, 1, 2, 3, 4] B) [1, 2, 3, 4, 5] C) [0, 1, 2, 3, 4, 5]