0% found this document useful (0 votes)
21 views23 pages

Python MCQ Set of All Units

The document covers fundamental concepts in programming, including problem-solving, algorithm characteristics, debugging, and Python programming basics. It discusses the structure of Python programs, control statements, and recursion, emphasizing the importance of documentation and structured programming. Key features of the Python interpreter and basic operations are also outlined, along with examples and syntax rules.

Uploaded by

tanni6159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views23 pages

Python MCQ Set of All Units

The document covers fundamental concepts in programming, including problem-solving, algorithm characteristics, debugging, and Python programming basics. It discusses the structure of Python programs, control statements, and recursion, emphasizing the importance of documentation and structured programming. Key features of the Python interpreter and basic operations are also outlined, along with examples and syntax rules.

Uploaded by

tanni6159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT-I: PLANNING THE COMPUTER PROGRAM

Concept of Problem Solving and Program Design


1. What is the first step in problem-solving for programming?
a) Write the code
b) Test the code
c) Define the problem
d) Debug the code
Answer: c) Define the problem
2. Which of the following is a key characteristic of a good algorithm?
a) It should be fast
b) It should solve the problem
c) It should be easy to understand
d) All of the above
Answer: d) All of the above
3. In programming, what is the purpose of debugging?
a) To improve the performance of the program
b) To remove all errors and fix logical issues
c) To make the code longer
d) To add more features
Answer: b) To remove all errors and fix logical issues
4. Which of the following is an example of a syntax error?
a) Missing parenthesis in a function call
b) Incorrect result from a mathematical operation
c) Unhandled exception
d) Infinite loop
Answer: a) Missing parenthesis in a function call
5. What is the purpose of documentation in programming?
a) To make the code difficult to read
b) To explain how the code works for others
c) To increase the size of the program
d) To slow down the program
Answer: b) To explain how the code works for others
6. Which of the following is NOT a technique used in problem-solving in programming?
a) Flowcharting
b) Decision Table
c) Brainstorming
d) Debugging
Answer: c) Brainstorming
7. Which of the following is a benefit of structured programming?
a) Code is easier to understand and maintain
b) The program runs faster
c) Less memory is used
d) It automatically optimizes the code
Answer: a) Code is easier to understand and maintain
8. Which of the following is an example of a programming methodology?
a) Top-down programming
b) Bubble sort
c) Iterative debugging
d) Problem-solving
Answer: a) Top-down programming
9. What does "Top-down" programming refer to?
a) Writing low-level code first
b) Breaking the problem into smaller modules starting from the main function
c) Writing the complete code and then testing it
d) Debugging the entire program
Answer: b) Breaking the problem into smaller modules starting from the main function
10. In Python, what is the typical structure of a program?
a) Variables, Functions, Loops
b) Functions, Classes, Variables
c) Modules, Classes, Statements
d) Statements, Functions, Variables
Answer: d) Statements, Functions, Variables

Overview of Python Program

11. Which of the following is the main element of a Python program?


a) Keywords
b) Functions
c) Statements
d) Operators
Answer: c) Statements
12. Which keyword is used to define a function in Python?
a) func
b) def
c) function
d) define
Answer: b) def
13. Which of the following is an essential characteristic of Python syntax?
a) Use of semicolons to end statements
b) Strict indentation rules
c) Use of braces to define code blocks
d) All of the above
Answer: b) Strict indentation rules
14. What is the purpose of a function in Python?
a) To execute a program
b) To define reusable blocks of code
c) To define variables
d) To create classes
Answer: b) To define reusable blocks of code
15. Which of the following is a built-in function in Python?
a) print()
b) input()
c) str()
d) All of the above
Answer: d) All of the above
16. What is a Python "module"?
a) A block of code that is reusable across programs
b) A type of loop
c) A Python class
d) A special function
Answer: a) A block of code that is reusable across programs
17. What is the output of the following Python code?

python
CopyEdit
x=5
y=3
print(x + y)
a) 53
b) 8
c) 5+3
d) Error
Answer: b) 8

18. Which of the following is a valid Python identifier?


a) 1st_variable
b) first-variable
c) first_variable
d) first variable
Answer: c) first_variable
19. What does "recursion" in programming refer to?
a) A function that calls itself
b) A loop that executes a set of instructions repeatedly
c) A conditional block
d) None of the above
Answer: a) A function that calls itself
20. In Python, which statement is used to exit a function?
a) return
b) break
c) exit
d) continue
Answer: a) return

UNIT-II: INTRODUCTION TO PYTHON


Python Interpreter and Basic Operations

21. Which of the following is a feature of the Python interpreter?


a) It directly compiles Python code into machine language
b) It reads and executes Python code line by line
c) It creates a binary file of the Python program
d) It only runs Python code in an IDE
Answer: b) It reads and executes Python code line by line
22. Which of the following can be used to execute Python code interactively?
a) Python interpreter
b) Jupyter Notebook
c) Python Shell
d) All of the above
Answer: d) All of the above
23. What is the result of running the following code in the Python shell?

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

28. Which of the following is NOT a valid Python operator?


a) +
b) -
c) ++
d) *
Answer: c) ++
29. What is the purpose of the input() function in Python?
a) To print data to the screen
b) To receive input from the user
c) To execute a file
d) To store data into a variable
Answer: b) To receive input from the user
30. Which of the following is NOT an arithmetic operator in Python?
a) +
b) -
c) /
d) is
Answer: d) is

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

UNIT-II: INTRODUCTION TO PYTHON


Python Interpreter and Basic Operations

1. Which of the following is a feature of the Python interpreter?


a) It directly compiles Python code into machine language
b) It reads and executes Python code line by line
c) It creates a binary file of the Python program
d) It only runs Python code in an IDE
Answer: b) It reads and executes Python code line by line
2. Which of the following can be used to execute Python code interactively?
a) Python interpreter
b) Jupyter Notebook
c) Python Shell
d) All of the above
Answer: d) All of the above
3. What is the result of running the following code in the Python shell?

python
CopyEdit
print(2 + 3 * 4)

a) 20
b) 14
c) 12
d) 23
Answer: b) 14

4. 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
5. Which of the following is used to create a comment in Python?
a) #
b) /* */
c) //
d) <!-- -->
Answer: a) #
6. 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
7. 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

8. Which of the following is NOT a valid Python operator?


a) +
b) -
c) ++
d) *
Answer: c) ++
9. What is the purpose of the input() function in Python?
a) To print data to the screen
b) To receive input from the user
c) To execute a file
d) To store data into a variable
Answer: b) To receive input from the user
10. Which of the following is NOT an arithmetic operator in Python?
a) +
b) -
c) /
d) is
Answer: d) is

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

UNIT-III: RECURSION, STACK DIAGRAMS, LISTS, AND STRINGS


Recursion and Stack Diagrams

1. What is recursion in programming?


a) A function calling another function
b) A function calling itself
c) A function that never terminates
d) A function that uses iteration
Answer: b) A function calling itself
2. Which of the following is essential to stop a recursive function?
a) Return statement
b) Base case
c) Loop statement
d) Recursion depth
Answer: b) Base case
3. What does a stack diagram show in recursion?
a) The flow of the program's execution
b) The recursive calls and their corresponding variables
c) The memory used by the program
d) The output of the program
Answer: b) The recursive calls and their corresponding variables
4. Which of the following is the correct base case for a recursive function? a) A condition where the
function calls itself
b) A condition that prevents further recursion
c) A condition that causes a program to crash
d) A condition that uses a loop
Answer: b) A condition that prevents further recursion
5. What is the output of the following recursive function?

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

10. What is the maximum recursion depth in Python?


a) 10
b) 50
c) 1000
d) It depends on the system's available memory
Answer: d) It depends on the system's available memory

Multiple Assignment

11. Which of the following is an example of multiple assignment in Python?


a) x = y = z = 10
b) x, y = 10
c) x = 10, y = 20
d) x = 10 and y = 20
Answer: a) x = y = z = 10
12. What does multiple assignment in Python allow?
a) Assigning the same value to multiple variables
b) Assigning different values to multiple variables in one statement
c) Creating tuples
d) Both a and b
Answer: d) Both a and b
13. Which of the following is an example of multiple assignment with unpacking?

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)

14. What does the following code do?

python
CopyEdit
a, b = 5, 10

a) Creates a tuple (5, 10)


b) Assigns 5 to a and 10 to b
c) Assigns 10 to a and 5 to b
d) Creates two separate variables a and b
Answer: b) Assigns 5 to a and 10 to b

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

The While Statement

16. What is the correct syntax of a while loop in Python?


a) while condition {}
b) while (condition)
c) while condition:
d) while (condition) {}
Answer: c) while condition:
17. What does the following Python while loop do?

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

18. What happens if the condition in a while loop is always True?


a) The program will break
b) The loop will execute indefinitely
c) The program will exit the loop
d) The program will give an error
Answer: b) The loop will execute indefinitely
19. Which of the following keywords can be used to exit a while loop prematurely?
a) continue
b) break
c) pass
d) return
Answer: b) break
20. Which of the following is the correct way to increment a variable inside a while loop?
a) x++
b) x += 1
c) x++1
d) ++x
Answer: b) x += 1

Tables and Two-Dimensional Tables

21. What does a two-dimensional table (array) represent in programming?


a) A list of lists
b) A single list
c) A dictionary
d) A sequence of strings
Answer: a) A list of lists
22. How can you access an element in a two-dimensional list in Python? a) table(x, y)
b) table[x][y]
c) table(x)[y]
d) table[x, y]
Answer: b) table[x][y]
23. What is the output of the following code?

python
CopyEdit
table = [[1, 2], [3, 4], [5, 6]]
print(table[1][0])

a) 3
b) 4
c) 1
d) 5
Answer: a) 3

24. What is the result of the following code?

python
CopyEdit
table = [[1, 2], [3, 4], [5, 6]]
for row in table:
print(row)

a) Prints each row as a list


b) Prints the sum of each row
c) Prints the transpose of the table
d) Prints all elements in a single line
Answer: a) Prints each row as a list

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]]

Strings and Lists

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

String Comparison and Operations

31. Which of the following is used to compare two strings in Python?


a) ==
b) is
c) cmp()
d) equals()
Answer: a) ==
32. Which of the following operations will return True if both strings are equal?
a) str1 == str2
b) str1 is str2
c) str1 != str2
d) str1.equals(str2)
Answer: a) str1 == str2
33. What is the result of str1 < str2 if str1 = "apple" and str2 = "banana"?
a) True
b) False
c) SyntaxError
d) TypeError
Answer: a) True
34. How do you reverse a string in Python?
a) string.reverse()
b) string[::-1]
c) reverse(string)
d) reversed(string)
Answer: b) string[::-1]
35. What is the result of the following operation?

python
CopyEdit
text = "Python"
print(text * 2)
a) PythonPython
b) PythonPythonPython
c) PP
d) Error
Answer: a) PythonPython

UNIT: IV

LOOPING AND COUNTING

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

11. What is the output of the following code?

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

21. Which of the following is used to clone a list in Python? a) lst.copy()


b) copy(lst)
c) lst.duplicate()
d) lst.clone()
Answer: a) lst.copy()
22. How do you remove the last element from a list in Python? a) lst.pop()
b) lst.remove()
c) lst.del()
d) lst.delete()
Answer: a) lst.pop()
23. What will the following code output?

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

OBJECT-ORIENTED PROGRAMMING (OOP)

26. Which of the following is used to define a class in Python?


a) class MyClass:
b) MyClass class:
c) def class MyClass:
d) class = MyClass:
Answer: a) class MyClass:
27. In Python, what does self refer to inside a class?
a) A global variable
b) A method inside a class
c) The instance of the object
d) The name of the class
Answer: c) The instance of the object
28. Which of the following methods is used to initialize an object in Python?
a) __start__()
b) __init__()
c) initialize()
d) object()
Answer: b) init()
29. How do you create an object of the class Car in Python?
a) Car.new()
b) Car.create()
c) Car()
d) Car.object()
Answer: c) Car()
30. What is the output of the following code?

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

DATA STRUCTURES: ARRAYS, LISTS, SETS, STACKS, AND QUEUES

1. Which of the following is the correct syntax to define an array in Python?


a) arr = [1, 2, 3, 4]
b) arr = (1, 2, 3, 4)
c) arr = {1, 2, 3, 4}
d) arr = <1, 2, 3, 4>
Answer: a) arr = [1, 2, 3, 4]
2. What is the primary characteristic of an array?
a) Elements are stored in non-contiguous memory locations
b) Elements are of different data types
c) Elements are stored in contiguous memory locations
d) Arrays are unordered collections
Answer: c) Elements are stored in contiguous memory locations
3. What will be the output of the following code?

python
CopyEdit
arr = [10, 20, 30, 40]
print(arr[2])

a) 20
b) 30
c) 10
d) 40
Answer: b) 30

4. Which operation on an array has the time complexity O(1)?


a) Insertion
b) Deletion
c) Accessing an element by index
d) Searching
Answer: c) Accessing an element by index
5. In Python, which data structure represents an ordered collection of elements and supports
duplicates?
a) Array
b) Set
c) List
d) Dictionary
Answer: c) List
6. Which of the following is true about sets in Python?
a) Sets can contain duplicates
b) Sets are unordered collections
c) Sets maintain the order of elements
d) Sets support indexing
Answer: b) Sets are unordered collections
7. Which data structure in Python is used for Last In First Out (LIFO) access?
a) List
b) Set
c) Stack
d) Queue
Answer: c) Stack
8. What is the primary characteristic of a queue?
a) Last In First Out (LIFO)
b) First In First Out (FIFO)
c) Random Access
d) Can store only unique elements
Answer: b) First In First Out (FIFO)
9. Which Python module provides a deque (double-ended queue) data structure?
a) queue
b) deque
c) collections
d) data
Answer: c) collections
10. Which of the following operations is not supported by a stack?
a) Push
b) Pop
c) Peek
d) Delete
Answer: d) Delete

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

31. Which of the following sorting algorithms is non-comparative?


a) Merge Sort
b) Quick Sort
c) Heap Sort
d) Radix Sort
Answer: d) Radix Sort
32. Which algorithm is generally faster than others for large datasets but requires extra space for
sorting?
a) Merge Sort
b) Bubble Sort
c) Quick Sort
d) Selection Sort
Answer: a) Merge Sort
33. What is the primary disadvantage of quicksort?
a) It is too slow for large datasets
b) It requires extra memory
c) Its worst-case time complexity is O(n^2)
d) It cannot be implemented recursively
**Answer: c) Its worst-case time complexity is

You might also like