Python MCQ Set of All Units
Python MCQ Set of All Units
python
CopyEdit
x=5
y=3
print(x + y)
a) 53
b) 8
c) 5+3
d) Error
Answer: b) 8
python
CopyEdit
print(2 + 3 * 4)
a) 20
b) 14
c) 12
d) 23
Answer: b) 14
24. Which of the following symbols is used for indentation in Python?
a) Tab
b) Space
c) Both tab and space
d) None of the above
Answer: c) Both tab and space
25. Which of the following is used to create a comment in Python?
a) #
b) /* */
c) //
d) <!-- -->
Answer: a) #
26. Which Python keyword is used to define a constant?
a) constant
b) const
c) final
d) Python does not have constants
Answer: d) Python does not have constants
27. What is the output of the following Python code?
python
CopyEdit
a = 10
b=3
print(a % b)
a) 1
b) 0
c) 3
d) Error
Answer: a) 1
Control Statements
31. Which of the following control statements is used to check a condition and execute code
accordingly in Python?
a) if
b) for
c) while
d) continue
Answer: a) if
32. What does the break statement do in a loop?
a) It skips the current iteration and moves to the next iteration
b) It terminates the entire program
c) It exits the loop and continues with the next statement after the loop
d) It restarts the loop
Answer: c) It exits the loop and continues with the next statement after the loop
33. Which of the following statements is true about the continue statement?
a) It terminates the loop
b) It terminates the program
c) It skips the current iteration of the loop and moves to the next iteration
d) It causes the loop to repeat the current iteration
Answer: c) It skips the current iteration of the loop and moves to the next iteration
34. What does the pass statement do in Python?
a) It skips the loop execution
b) It allows for a conditional branch without executing any action
c) It ends the program
d) It causes an error
Answer: b) It allows for a conditional branch without executing any action
35. Which of the following is the correct syntax for a while loop in Python? a) while x > 0 do:
b) while x > 0:
c) while x > 0 then:
d) while(x > 0)
Answer: b) while x > 0:
36. Which of the following control structures can be used for looping in Python?
a) for
b) while
c) both a and b
d) if
Answer: c) both a and b
37. Which of the following will execute the code inside the else block after a while loop?
a) When the loop condition is True
b) When the loop breaks
c) When the loop finishes normally
d) When an error occurs
Answer: c) When the loop finishes normally
38. What is the syntax to define a function with default arguments in Python? a) def my_function(x =
5):
b) def my_function(x):
c) def my_function(x: 5):
d) def my_function(x -> 5):
Answer: a) def my_function(x = 5):
39. Which of the following represents the ternary operator in Python?
a) x if condition else y
b) condition ? x : y
c) x ? y : z
d) if condition: x else y
Answer: a) x if condition else y
40. In Python, which of the following operators is used for logical comparisons?
a) &&
b) ||
c) and
d) &
Answer: c) and
python
CopyEdit
print(2 + 3 * 4)
a) 20
b) 14
c) 12
d) 23
Answer: b) 14
python
CopyEdit
a = 10
b=3
print(a % b)
a) 1
b) 0
c) 3
d) Error
Answer: a) 1
Control Statements
11. Which of the following control statements is used to check a condition and execute code
accordingly in Python?
a) if
b) for
c) while
d) continue
Answer: a) if
12. What does the break statement do in a loop?
a) It skips the current iteration and moves to the next iteration
b) It terminates the entire program
c) It exits the loop and continues with the next statement after the loop
d) It restarts the loop
Answer: c) It exits the loop and continues with the next statement after the loop
13. Which of the following statements is true about the continue statement?
a) It terminates the loop
b) It terminates the program
c) It skips the current iteration of the loop and moves to the next iteration
d) It causes the loop to repeat the current iteration
Answer: c) It skips the current iteration of the loop and moves to the next iteration
14. What does the pass statement do in Python?
a) It skips the loop execution
b) It allows for a conditional branch without executing any action
c) It ends the program
d) It causes an error
Answer: b) It allows for a conditional branch without executing any action
15. Which of the following is the correct syntax for a while loop in Python? a) while x > 0 do:
b) while x > 0:
c) while x > 0 then:
d) while(x > 0)
Answer: b) while x > 0:
16. Which of the following control structures can be used for looping in Python?
a) for
b) while
c) both a and b
d) if
Answer: c) both a and b
17. Which of the following will execute the code inside the else block after a while loop?
a) When the loop condition is True
b) When the loop breaks
c) When the loop finishes normally
d) When an error occurs
Answer: c) When the loop finishes normally
18. What is the syntax to define a function with default arguments in Python? a) def my_function(x =
5):
b) def my_function(x):
c) def my_function(x: 5):
d) def my_function(x -> 5):
Answer: a) def my_function(x = 5):
19. Which of the following represents the ternary operator in Python?
a) x if condition else y
b) condition ? x : y
c) x ? y : z
d) if condition: x else y
Answer: a) x if condition else y
20. In Python, which of the following operators is used for logical comparisons?
a) &&
b) ||
c) and
d) &
Answer: c) and
python
CopyEdit
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))
a) 4
b) 24
c) 120
d) 1
Answer: b) 24
6. What happens when a recursive function does not have a base case?
a) The program works fine
b) The recursion ends prematurely
c) The program crashes due to infinite recursion
d) The function does not return any value
Answer: c) The program crashes due to infinite recursion
7. Which of the following is NOT true about recursion?
a) Recursion requires a base case to terminate
b) Recursion can simplify the code for certain problems
c) Recursion always uses more memory than iteration
d) Recursion makes use of a stack to store function calls
Answer: c) Recursion always uses more memory than iteration
8. Which is the correct way to represent a recursive function's call in a stack diagram?
a) By showing the function name and parameters at each level
b) By showing only the return values of the function
c) By showing the flow of control without any parameters
d) By showing a loop structure
Answer: a) By showing the function name and parameters at each level
9. What is the result of the following recursive call in Python?
python
CopyEdit
def sum_of_n(n):
if n == 0:
return 0
else:
return n + sum_of_n(n-1)
print(sum_of_n(5))
a) 5
b) 10
c) 15
d) 120
Answer: c) 15
Multiple Assignment
python
CopyEdit
x = (1, 2, 3)
a) x = 1, 2, 3
b) x, y, z = (1, 2, 3)
c) x = (1, 2, 3)
d) x = [1, 2, 3]
Answer: b) x, y, z = (1, 2, 3)
python
CopyEdit
a, b = 5, 10
15. What happens when you try to unpack a sequence with a wrong number of variables? a) It raises
an IndexError
b) It raises a ValueError
c) It returns a tuple
d) It works without any error
Answer: b) It raises a ValueError
python
CopyEdit
x=0
while x < 5:
print(x)
x += 1
a) Prints numbers 0 to 5
b) Prints numbers 0 to 4
c) Prints numbers 1 to 5
d) Causes an infinite loop
Answer: b) Prints numbers 0 to 4
python
CopyEdit
table = [[1, 2], [3, 4], [5, 6]]
print(table[1][0])
a) 3
b) 4
c) 1
d) 5
Answer: a) 3
python
CopyEdit
table = [[1, 2], [3, 4], [5, 6]]
for row in table:
print(row)
25. Which Python statement can be used to represent a matrix? a) matrix = [(1, 2), (3, 4)]
b) matrix = [[1, 2], [3, 4]]
c) matrix = [1, 2, 3, 4]
d) matrix = [1, 2, [3, 4]]
Answer: b) matrix = [[1, 2], [3, 4]]
26. In Python, which of the following is the correct way to define a string?
a) string = 'Hello'
b) string = "Hello"
c) string = 'Hello' or "Hello"
d) Both a and b
Answer: d) Both a and b
27. Which function can be used to find the length of a string in Python?
a) size()
b) length()
c) len()
d) str_len()
Answer: c) len()
28. Which of the following is the correct way to access the first character of a string in Python?
a) string[1]
b) string(1)
c) string[0]
d) string(0)
Answer: c) string[0]
29. What does the find() function do in Python?
a) Returns the index of the first occurrence of a substring
b) Returns the substring itself
c) Returns the length of the string
d) Returns a list of all occurrences of the substring
Answer: a) Returns the index of the first occurrence of a substring
30. What is the output of the following code?
python
CopyEdit
text = "Python"
print(text[2:5])
a) Py
b) tho
c) t
d) Python
Answer: b) tho
python
CopyEdit
text = "Python"
print(text * 2)
a) PythonPython
b) PythonPythonPython
c) PP
d) Error
Answer: a) PythonPython
UNIT: IV
1. Which of the following is the correct syntax for a for loop in Python?
a) for i in range(10):
b) for i < 10:
c) for i = 0; i < 10; i++:
d) for i = 0, i < 10, i++:
Answer: a) for i in range(10):
2. Which function is used to generate a sequence of numbers in Python?
a) range()
b) list()
c) sequence()
d) repeat()
Answer: a) range()
3. What is the output of the following code?
python
CopyEdit
for i in range(1, 6, 2):
print(i)
a) 1 3 5
b) 2 4
c) 1 2 3 4 5
d) 1 3
Answer: a) 1 3 5
4. Which of the following is used to skip the current iteration of a loop in Python?
a) exit
b) break
c) continue
d) pass
Answer: c) continue
5. What does the while loop do in Python?
a) Repeats until a certain condition is met
b) Runs an infinite loop
c) Iterates over a fixed range
d) None of the above
Answer: a) Repeats until a certain condition is met
6. Which of the following terminates a loop prematurely?
a) continue
b) break
c) exit
d) return
Answer: b) break
7. What will be the output of the following code?
python
CopyEdit
x=0
while x < 3:
print(x)
x += 1
a) 1 2 3
b) 0 1 2
c) 0 1 2 3
d) 1 2
Answer: b) 0 1 2
8. Which of the following loops is used when the number of iterations is known in advance?
a) for loop
b) while loop
c) do-while loop
d) foreach loop
Answer: a) for loop
9. Which of the following is the correct syntax for a while loop in Python?
a) while condition {}
b) while(condition):
c) while condition:
d) while condition()
Answer: c) while condition:
10. What will the following code print?
python
CopyEdit
x=0
for x in range(5):
print(x)
a) 1 2 3 4 5
b) 0 1 2 3 4
c) 1 2 3 4
d) 0 1 2 3
Answer: b) 0 1 2 3 4
LIST OPERATIONS
python
CopyEdit
lst = [10, 20, 30, 40]
print(lst[2])
a) 30
b) 20
c) 40
d) 10
Answer: a) 30
12. How can you find the length of a list in Python? a) length(lst)
b) len(lst)
c) size(lst)
d) lst.length()
Answer: b) len(lst)
13. What does the append() method do in Python? a) Adds a new list at the end
b) Adds a new element to the end of the list
c) Removes the last element
d) Sorts the list
Answer: b) Adds a new element to the end of the list
14. Which method is used to add an element at a specific position in a list? a) insert()
b) append()
c) add()
d) extend()
Answer: a) insert()
15. What is the output of the following code?
python
CopyEdit
lst = [1, 2, 3]
lst.append([4, 5])
print(lst)
a) [1, 2, 3, 4, 5]
b) [1, 2, 3, [4, 5]]
c) [4, 5, 1, 2, 3]
d) [[4, 5], 1, 2, 3]
Answer: b) [1, 2, 3, [4, 5]]
16. How do you remove the first occurrence of an element from a list? a) pop()
b) remove()
c) del()
d) delete()
Answer: b) remove()
17. What does the extend() method do in Python? a) Adds a single item to a list
b) Replaces an element in the list
c) Adds multiple items to the list
d) Removes all items from the list
Answer: c) Adds multiple items to the list
18. Which of the following methods can be used to clone a list in Python? a) copy()
b) clone()
c) duplicate()
d) lst = lst.copy()
Answer: a) copy()
19. Which of the following will delete the second element from a list lst = [10, 20, 30]? a) del lst[2]
b) lst.pop(1)
c) lst.remove(20)
d) lst.delete(1)
Answer: b) lst.pop(1)
20. How do you combine two lists in Python? a) lst1 + lst2
b) lst1.append(lst2)
c) lst1.insert(lst2)
d) lst1.extend(lst2)
Answer: a) lst1 + lst2
LIST DELETION AND CLONING
python
CopyEdit
lst = [1, 2, 3, 4, 5]
lst[1:3] = []
print(lst)
a) [1, 4, 5]
b) [1, 2, 3]
c) [1, 5]
d) [1, 2, 4, 5]
Answer: a) [1, 4, 5]
24. What does the clear() method do in Python? a) Clears the entire list
b) Clears an element in the list
c) Removes the first element from the list
d) Removes duplicates from the list
Answer: a) Clears the entire list
25. Which of the following can be used to remove an element from a list by index in Python? a) del
b) remove()
c) pop()
d) delete()
Answer: a) del
python
CopyEdit
class Car:
def __init__(self, make):
self.make = make
def display(self):
print(self.make)
c = Car("Toyota")
c.display()
a) Toyota
b) Car
c) make
d) None
Answer: a) Toyota
STANDARD LIBRARIES
31. Which of the following is the Python library used for working with regular expressions?
a) regex
b) re
c) pattern
d) regexpy
Answer: b) re
32. What module would you import to generate random numbers in Python?
a) math
b) random
c) statistics
d) numpy
Answer: b) random
33. Which Python library is used to work with dates and times?
a) time
b) datetime
c) dateutil
d) calendars
Answer: b) datetime
34. What is the purpose of the os module in Python?
a) To manipulate strings
b) To perform mathematical operations
c) To interact with the operating system
d) To handle date and time
Answer: c) To interact with the operating system
UNIT: 5
python
CopyEdit
arr = [10, 20, 30, 40]
print(arr[2])
a) 20
b) 30
c) 10
d) 40
Answer: b) 30
SEARCHING ALGORITHMS
11. Which searching algorithm works by dividing the search space in half at each step?
a) Linear Search
b) Binary Search
c) Jump Search
d) Interpolation Search
Answer: b) Binary Search
12. What is the time complexity of linear search?
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
Answer: c) O(n)
13. What is the best-case time complexity of binary search?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: a) O(1)
14. Which of the following is required for binary search to work?
a) Unsorted list
b) A sorted list
c) Linked list
d) Queue
Answer: b) A sorted list
15. What is the time complexity of binary search in the worst case?
a) O(log n)
b) O(n)
c) O(n^2)
d) O(1)
Answer: a) O(log n)
16. In a linear search, what happens when the desired element is found?
a) The element is replaced with a new value
b) The search stops and returns the element's index
c) The list is sorted
d) The algorithm continues to search for more occurrences
Answer: b) The search stops and returns the element's index
17. Which of the following is the correct implementation of binary search in Python?
python
CopyEdit
arr = [1, 2, 3, 4, 5]
target = 3
a) binary_search(arr, target)
b) binary_search(arr, 0, len(arr)-1, target)
c) arr.binary_search(target)
d) arr.search(target)
Answer: b) binary_search(arr, 0, len(arr)-1, target)
18. What is the time complexity of linear search in the worst case?
a) O(log n)
b) O(1)
c) O(n)
d) O(n^2)
Answer: c) O(n)
19. Which search algorithm has the highest efficiency for searching a sorted array?
a) Linear search
b) Binary search
c) Jump search
d) Hashing
Answer: b) Binary search
20. Which of the following is not an advantage of binary search over linear search?
a) Faster for large datasets
b) Requires a sorted array
c) Can be implemented on unsorted data
d) Reduces search time logarithmically
Answer: c) Can be implemented on unsorted data
SORTING ALGORITHMS
21. Which sorting algorithm repeatedly selects the smallest element from the unsorted portion and
swaps it with the first unsorted element?
a) Bubble Sort
b) Insertion Sort
c) Selection Sort
d) Merge Sort
Answer: c) Selection Sort
22. What is the average time complexity of bubble sort?
a) O(n log n)
b) O(n^2)
c) O(n)
d) O(log n)
Answer: b) O(n^2)
23. Which of the following sorting algorithms is best for small datasets?
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Heap Sort
Answer: c) Bubble Sort
24. Which sorting algorithm works by dividing the list into two halves and sorting each half
recursively?
a) Merge Sort
b) Selection Sort
c) Bubble Sort
d) Quick Sort
Answer: a) Merge Sort
25. What is the time complexity of selection sort in the worst case?
a) O(n)
b) O(n^2)
c) O(log n)
d) O(n log n)
Answer: b) O(n^2)
26. Which of the following sorting algorithms is an example of a divide and conquer algorithm?
a) Bubble Sort
b) Merge Sort
c) Selection Sort
d) Insertion Sort
Answer: b) Merge Sort
27. In bubble sort, the largest element 'bubbles' to which position?
a) First
b) Last
c) Middle
d) Random
Answer: b) Last
28. What is the best-case time complexity of bubble sort?
a) O(n^2)
b) O(n log n)
c) O(n)
d) O(1)
Answer: c) O(n)
29. Which of the following sorting algorithms uses the concept of "pivot" to divide the list into two
sub-lists?
a) Quick Sort
b) Merge Sort
c) Selection Sort
d) Insertion Sort
Answer: a) Quick Sort
30. What is the worst-case time complexity of insertion sort?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)
Answer: c) O(n^2)
ADVANCED SORTING AND OTHER CONCEPTS