[PYTHON PROGRAMMING MIDTERM EXAM] – QUESTIONS
AND ANSWERS | VERIFIED AND WELL DETAILED ANSWERS |
PLUS RATIONALES | GUARANTEED PASS | LATEST EXAM
UPDATE | EXAM PREP | STUDY GUIDE | PRACTICE TEST
1. A developer needs to store a sequence of ordered values that should not be modified after
creation. Which Python data structure is the best choice?
A. List
B. Dictionary
C. Tuple
D. Set
════════════════════
Correct Answer: C. Tuple
Rationale:
Tuples are immutable, meaning their values cannot be changed after creation. Lists are
mutable, dictionaries store key-value pairs, and sets are unordered collections.
════════════════════
2. Which statement correctly opens a file named report.txt for reading in Python?
A. open("report.txt", "w")
B. open("report.txt", "x")
C. open("report.txt", "a")
D. open("report.txt", "r")
════════════════════
Correct Answer: D. open("report.txt", "r")
Rationale:
The "r" mode opens a file for reading. The "w" mode writes to a file, "a" appends content,
and "x" creates a new file.
════════════════════
3. A financial analyst wants to repeat a task exactly 10 times. Which loop is most appropriate?
,A. while loop
B. for loop with range(10)
C. recursive function
D. infinite loop
════════════════════
Correct Answer: B. for loop with range(10)
Rationale:
A for loop using range(10) is the most efficient and readable method for fixed iteration counts.
A while loop is better when the number of iterations is unknown.
════════════════════
4. What is the output of the following code?
print(type(3.14))
A. int
B. string
C. float
D. decimal
════════════════════
Correct Answer: C. float
Rationale:
The value 3.14 is a floating-point number in Python. Therefore, type(3.14) returns float.
════════════════════
5. Which keyword is used to define a function in Python?
A. define
B. func
C. lambda
D. def
════════════════════
Correct Answer: D. def
Rationale:
,The def keyword is used to define standard functions in Python. Lambda creates anonymous
functions but does not replace def for normal function declarations.
════════════════════
6. A company stores employee IDs as unique values without duplicates. Which collection type
is best suited for this requirement?
A. List
B. Tuple
C. Set
D. String
════════════════════
Correct Answer: C. Set
Rationale:
Sets automatically prevent duplicate entries, making them ideal for storing unique values like
employee IDs.
════════════════════
7. Which operator is used for exponentiation in Python?
A. ^
B. **
C. //
D. %%
════════════════════
Correct Answer: B. **
Rationale:
The double asterisk operator () performs exponentiation in Python. The caret symbol (^) is a
bitwise XOR operator.**
════════════════════
8. What is the primary purpose of indentation in Python?
A. Improve printing speed
B. Organize imports
, C. Define code blocks
D. Separate variables
════════════════════
Correct Answer: C. Define code blocks
Rationale:
Python uses indentation to define blocks of code such as loops, functions, and conditionals.
Improper indentation causes syntax errors.
════════════════════
9. Which built-in function converts a string to an integer?
A. str()
B. float()
C. bool()
D. int()
════════════════════
Correct Answer: D. int()
Rationale:
The int() function converts compatible string values into integers. For example, int("5") returns
5.
════════════════════
10. A support engineer wants to check whether a username exists in a list. Which operator
should be used?
A. in
B. ==
C. is
D. not
════════════════════
Correct Answer: A. in
Rationale:
The in operator checks membership within collections such as lists, tuples, and sets.
AND ANSWERS | VERIFIED AND WELL DETAILED ANSWERS |
PLUS RATIONALES | GUARANTEED PASS | LATEST EXAM
UPDATE | EXAM PREP | STUDY GUIDE | PRACTICE TEST
1. A developer needs to store a sequence of ordered values that should not be modified after
creation. Which Python data structure is the best choice?
A. List
B. Dictionary
C. Tuple
D. Set
════════════════════
Correct Answer: C. Tuple
Rationale:
Tuples are immutable, meaning their values cannot be changed after creation. Lists are
mutable, dictionaries store key-value pairs, and sets are unordered collections.
════════════════════
2. Which statement correctly opens a file named report.txt for reading in Python?
A. open("report.txt", "w")
B. open("report.txt", "x")
C. open("report.txt", "a")
D. open("report.txt", "r")
════════════════════
Correct Answer: D. open("report.txt", "r")
Rationale:
The "r" mode opens a file for reading. The "w" mode writes to a file, "a" appends content,
and "x" creates a new file.
════════════════════
3. A financial analyst wants to repeat a task exactly 10 times. Which loop is most appropriate?
,A. while loop
B. for loop with range(10)
C. recursive function
D. infinite loop
════════════════════
Correct Answer: B. for loop with range(10)
Rationale:
A for loop using range(10) is the most efficient and readable method for fixed iteration counts.
A while loop is better when the number of iterations is unknown.
════════════════════
4. What is the output of the following code?
print(type(3.14))
A. int
B. string
C. float
D. decimal
════════════════════
Correct Answer: C. float
Rationale:
The value 3.14 is a floating-point number in Python. Therefore, type(3.14) returns float.
════════════════════
5. Which keyword is used to define a function in Python?
A. define
B. func
C. lambda
D. def
════════════════════
Correct Answer: D. def
Rationale:
,The def keyword is used to define standard functions in Python. Lambda creates anonymous
functions but does not replace def for normal function declarations.
════════════════════
6. A company stores employee IDs as unique values without duplicates. Which collection type
is best suited for this requirement?
A. List
B. Tuple
C. Set
D. String
════════════════════
Correct Answer: C. Set
Rationale:
Sets automatically prevent duplicate entries, making them ideal for storing unique values like
employee IDs.
════════════════════
7. Which operator is used for exponentiation in Python?
A. ^
B. **
C. //
D. %%
════════════════════
Correct Answer: B. **
Rationale:
The double asterisk operator () performs exponentiation in Python. The caret symbol (^) is a
bitwise XOR operator.**
════════════════════
8. What is the primary purpose of indentation in Python?
A. Improve printing speed
B. Organize imports
, C. Define code blocks
D. Separate variables
════════════════════
Correct Answer: C. Define code blocks
Rationale:
Python uses indentation to define blocks of code such as loops, functions, and conditionals.
Improper indentation causes syntax errors.
════════════════════
9. Which built-in function converts a string to an integer?
A. str()
B. float()
C. bool()
D. int()
════════════════════
Correct Answer: D. int()
Rationale:
The int() function converts compatible string values into integers. For example, int("5") returns
5.
════════════════════
10. A support engineer wants to check whether a username exists in a list. Which operator
should be used?
A. in
B. ==
C. is
D. not
════════════════════
Correct Answer: A. in
Rationale:
The in operator checks membership within collections such as lists, tuples, and sets.