0% found this document useful (0 votes)
17 views10 pages

Final Exam Reviewer

This document is a final exam reviewer for Data Structures and Algorithms using Python, containing a series of multiple-choice questions covering various topics such as Python's history, syntax, data types, control structures, and built-in functions. Each question is followed by the correct answer, providing a comprehensive overview of essential Python concepts. It serves as a study guide for students preparing for an exam in this subject.

Uploaded by

luxuriousdior3
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)
17 views10 pages

Final Exam Reviewer

This document is a final exam reviewer for Data Structures and Algorithms using Python, containing a series of multiple-choice questions covering various topics such as Python's history, syntax, data types, control structures, and built-in functions. Each question is followed by the correct answer, providing a comprehensive overview of essential Python concepts. It serves as a study guide for students preparing for an exam in this subject.

Uploaded by

luxuriousdior3
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/ 10

Data Structure and Algorithm

Using Python
Final Exam Reviewer

1. Who developed Python and when was it first released


A. Dennis Ritchie, 1972
B. Guido van Rossum, 1991
C. James Gosling, 1995
D. Bjarne Stroustrup, 1983
Answer: B
2. Which of the following is NOT a use of Python?
A. Web Development
B. Data Analysis
C. System Programming
D. Operating System Development
Answer: D
3. What makes Python popular among developers?
A. Its speed and complexity
B. Simple syntax and large community support
C. Lack of libraries
D. Limited use cases
Answer: B
4. What is a key benefit of Python's interpreted nature?
A. Faster execution than compiled languages
B. Platform independence
C. Requires a separate compilation step
D. Works only on Windows
Answer: B
5. Which of the following companies uses Python extensively?
A. Google
B. Microsoft
C. Netflix
D. All of the above
Answer: D
6. Which of the following IDEs is NOT commonly used for Python?
A. IDLE
B. Jupyter Notebook
C. Visual Studio Code
D. Eclipse (without plugins)
Answer: D
7. What is the command to check the installed Python version?
A. python --check
B. python --version
C. python -v
D. python -check-version
Answer: B
8. Where can you download Python from?
A. www.python.org
B. www.python.com
C. www.python.net
D. www.python.edu
Answer: A
9. Jupyter Notebook is widely used for:
A. Game Development
B. Data Science and Machine Learning
C. Database Administration
D. Embedded Systems
Answer: B
10. What is the purpose of a virtual environment in Python?
A. To manage different versions of Python on the same machine
B. To allow Python to run on virtual machines only
C. To compile Python code into a virtual format
D. To enhance Python's speed
Answer: A
11. What does the following code output?
print("Hello, World!")
A. hello, world!
B. Hello, World!
C. "Hello, World!"
D. SyntaxError
Answer: B
12. How do you execute a Python script file named example.py?
A. run example.py
B. python example.py
C. execute example.py
D. py script example.py
Answer: B
13. Which character is used for comments in Python?
A. //
B. /* */
C. #
D. --
Answer: C
14. What is the file extension for Python scripts?
A. .py
B. .python
C. .pt
D. .pys
Answer: A
15. What is the correct syntax for a print statement in Python 3?
A. print "Hello"
B. echo("Hello")
C. print("Hello")
D. printf("Hello")
Answer: C
16. What is the output of the following code?
type(3.14)
A. <class 'float'>
B. <class 'int'>
C. <class 'str'>
D. <class 'bool'>
Answer: A
17. Which of the following is a boolean value in Python?
A. yes
B. true
C. True
D. TRUE
Answer: C
18. What data type is the following value: "123"?
A. Integer
B. Float
C. String
D. Boolean
Answer: C
19. Which of the following is a valid variable name in Python?
A. 123var
B. var_name
C. var-name
D. var name
Answer: B
20. What happens if you try to change a constant in Python?
A. It raises a SyntaxError
B. The value changes
C. The interpreter warns but allows it
D. Python does not support true constants
Answer: D
21. What will be the output of the following code?
x = 10
if x > 5:
print("A")
elif x == 10:
print("B")
else:
print("C")
A. A
B. B
C. C
D. No output
Answer: A
22. Which keyword is used for multi-way branching in Python?
A. switch
B. elif
C. else if
D. elseif
Answer: B
23. What is the correct syntax for a conditional statement in Python?
A. if (x > 0):
B. if x > 0 then
C. if x > 0:
D. if x > 0 {
Answer: C
24. How many times will the following code execute?
for i in range(3):
print(i)

A. 2
B. 3
C. 4
D. Infinite
Answer: B
25. Which of the following is NOT iterable in a for loop?
A. List
B. Tuple
C. Dictionary
D. Integer
Answer: D
26. What will be the output of the following code?
i=0
while i < 3:
print(i)
i += 1
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. Infinite loop
Answer: A
27. What is the purpose of the break statement in loops?
A. Skip the current iteration
B. Stop the entire loop
C. Restart the loop
D. Jump to the next iteration
Answer: B
28. What will the following code output?
for i in range(5):
if i == 3:
continue
print(i)
A. 0 1 2 4
B. 0 1 2 3 4
C. 0 1 2
D. Infinite loop
Answer: A
29. What is the role of the else clause in loops?
A. Executes if the loop finishes normally
B. Executes after every iteration
C. Executes when the loop is infinite
D. Executes when the loop encounters a break
Answer: A
30. What is the output of the following code?
name = input("Enter your name: ")
print(f"Hello, {name}!")
A. Hello, name!
B. Enter your name:
C. Depends on user input
D. SyntaxError
Answer: C

31. Which function is used to take input from the user?


A. input()
B. read()
C. scanf()
D. get()
Answer: A
32. What does the following code print?
print("Result: {:.2f}".format(3.14159))
A. Result: 3.14159
B. Result: 3.14
C. Result: 3.1
D. Result: 3
Answer: B
33. Which of the following is mutable?
A. List
B. Tuple
C. String
D. All of the above
Answer: A
34. What is the output of the following code?
my_set = {1, 2, 3, 4}
my_set.add(5)
print(my_set)
A. {1, 2, 3, 4}
B. {1, 2, 3, 4, 5}
C. [1, 2, 3, 4, 5]
D. Error
Answer: B
35. What method is used to access a value in a dictionary?
A. get()
B. add()
C. append()
D. index()
Answer: A
36. What is the correct syntax to define a function in Python?
A. function myFunc():
B. def myFunc():
C. def myFunc:
D. func myFunc():
Answer: B
37. What is the purpose of the return statement in a function?
A. To print a value
B. To define the function
C. To send a value back to the caller
D. To stop the function execution
Answer: C
38. What will be the output of the following code?
def add(a, b):
return a + b
print(add(2, 3))
A. 5
B. 2
C. 3
D. Error
Answer: A
39. What type of argument allows a function to have optional parameters with default values?
A. Positional argument
B. Keyword argument
C. Default argument
D. Variable-length argument
Answer: C
40. Which of the following allows passing an arbitrary number of arguments to a function?
A. *args
B. **kwargs
C. Both A and B
D. None of the above
Answer: A
41. What will the following code output?
def greet(name="Guest"):
print(f"Hello, {name}!")
greet("Alice")
greet()
A. Hello, Alice!
Hello, Guest!
B. Hello, Guest!
Hello, Guest!
C. Hello, Alice!
Error
D. Error
Answer: A
42. Which of the following is a valid lambda function?
A. lambda x: x * 2
B. lambda(x): x * 2
C. lambda x => x * 2
D. lambda: x * 2
Answer: A
43. What will the following code output?
double = lambda x: x * 2
print(double(5))
A. 10
B. 25
C. 5
D. Error
Answer: A
44. Which of the following is a primary use case for lambda functions?
A. Defining global variables
B. Writing short, anonymous functions
C. Looping over sequences
D. Managing file input and output
Answer: B
45. What is the output of the following code?
x = 10
def change():
x=5
change()
print(x)
A. 5
B. 10
C. Error
D. None
Answer: B
46. How can you modify a global variable inside a function?
A. Use the global keyword
B. Assign it directly
C. Use the local keyword
D. It's not possible
Answer: A
47. What is the lifetime of a local variable?
A. Until the program ends
B. Until the function finishes execution
C. Until the next function call
D. Forever
Answer: B
48. Which statement is used to import a module in Python?
A. include
B. use
C. import
D. require
Answer: C
49. What is the purpose of the __name__ == "__main__" condition in a Python script?
A. To check if the script is being run directly or imported
B. To import a module
C. To define the main function
D. To check the name of the script
Answer: A
50. How do you install external Python packages?
A. Using the install command
B. Using the apt-get command
C. Using the pip command
D. Using the setup command
Answer: C
51. What is the output of the following code?
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
A. [1, 2, 3]
B. [1, 2, 3, 4]
C. [4, 1, 2, 3]
D. Error
Answer: B
52. How can you access the last element of a list?
A. list[-1]
B. list[last]
C. list[len(list)]
D. list[len(list) - 2]
Answer: A
53. What is the result of slicing the list my_list = [1, 2, 3, 4, 5] using my_list[1:4]?
A. [1, 2, 3]
B. [2, 3, 4]
C. [1, 2, 3, 4]
D. [2, 3, 4, 5]
Answer: B
54. Which method removes the first occurrence of a specific value in a list?
A. delete()
B. remove()
C. pop()
D. discard()
Answer: B
55. What will the following code output?
my_list = [1, 2, 3]
my_list[1] = 10
print(my_list)
A. [1, 10, 3]
B. [1, 2, 3]
C. [10, 2, 3]
D. Error
Answer: A
56. What will the following code output?
my_dict = {"a": 1, "b": 2}
print(my_dict["a"])
A. 1
B. 2
C. a
D. Error
Answer: A
57. Which method is used to get all keys in a dictionary?
A. keys()
B. values()
C. items()
D. get_keys()
Answer: A
58. What does the get() method do in dictionaries?
A. Adds a new key-value pair
B. Retrieves the value for a given key
C. Removes a key-value pair
D. Updates the dictionary
Answer: B
59. How do you check if a key exists in a dictionary?
A. if key in dict:
B. if key exists dict:
C. if dict.has_key(key):
D. if key is in dict:
Answer: A
60. What is the result of the following code?
my_dict = {"x": 5, "y": 10}
my_dict["z"] = 15
print(my_dict)
A. {"x": 5, "y": 10}
B. {"x": 5, "y": 10, "z": 15}
C. {"z": 15}
D. Error
Answer: B
61. What is the output of the following code?
my_set = {1, 2, 3, 1}
print(my_set)
A. {1, 2, 3, 1}
B. {1, 2, 3}
C. [1, 2, 3]
D. Error
Answer: B
62. Which method returns the union of two sets?
A. union()
B. intersection()
C. difference()
D. merge()
Answer: A
63. What will the following code output?
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1 & set2)
A. {1, 2, 3, 4}
B. {2, 3}
C. {1, 4}
D. Error
Answer: B
64. What does the add() method do in a set?
A. Adds a value at a specific position
B. Adds a value only if it does not exist
C. Updates a value
D. Adds a key-value pair
Answer: B
65. What will be the output of the following code?
set1 = {1, 2, 3}
set2 = {2, 4, 6}
print(set1 | set2)
A. {1, 2, 3, 4, 6}
B. {1, 2, 3}
C. {2, 4, 6}
D. Error
Answer: A
66. Which of the following creates a tuple?
A. [1, 2, 3]
B. {1, 2, 3}
C. (1, 2, 3)
D. tuple[1, 2, 3]
Answer: C
67. What happens when you try to modify a tuple?
A. The tuple is updated
B. A new element is added
C. A TypeError occurs
D. Nothing happens
Answer: C
68. What will the following code output?
my_tuple = (1, 2, 3)
print(my_tuple[1])
A. 1
B. 2
C. (1, 2)
D. Error
Answer: B
69. What is the primary use of tuples?
A. Storing data that will not change
B. Performing set operations
C. Managing key-value pairs
D. Iterating over sequences
Answer: A
70. Which specialized data structure is used for counting elements?
A. deque
B. Counter
C. set
D. OrderedDict
Answer: B
71. What is the output of the following code?
from collections import Counter
data = [1, 2, 2, 3, 3, 3]
print(Counter(data))
A. Counter({1: 1, 2: 2, 3: 3})
B. {1: 1, 2: 2, 3: 3}
C. [1: 1, 2: 2, 3: 3]
D. Error
Answer: A
72. What is a primary use case for the deque object in the collections module?
A. Sorting data
B. Efficiently appending and popping elements
C. Counting elements
D. Managing unique elements
Answer: B
73. What will the following code output?
from collections import deque
d = deque([1, 2, 3])
d.appendleft(0)
print(d)
A. [0, 1, 2, 3]
B. deque([0, 1, 2, 3])
C. {0, 1, 2, 3}
D. Error
Answer: B
74. What method removes elements from the right end of a deque?
A. pop()
B. remove()
C. popleft()
D. discard()
Answer: A
75. Which of the following is not a data structure provided by the collections module?
A. Counter
B. OrderedDict
C. deque
D. Array
Answer: D

You might also like