Debugging Questions and Answers
Practice ModeShowing 10 of 25 questions
Q1
What is the output of the following code?
x = [1, 2, 3]
y = x
x.append(4)
print(y)
Answer: Option B
Explanation: When you assign y = x, both variables reference the same list object. Modifying x also affects y.
Q2
Which method is used to debug by printing variable values in Python?
Answer: Option B
Explanation: print() is the most basic debugging technique to output variable values during execution.
Q3
What does the "NameError: name x is not defined" indicate?
Answer: Option B
Explanation: This error occurs when trying to use a variable that hasn't been declared or is out of scope.
Q4
What is the purpose of the pdb module in Python?
Answer: Option C
Explanation: pdb (Python Debugger) provides an interactive debugging environment for Python programs.
Q5
What will be the output?
try:
print(10/0)
except ZeroDivisionError:
print("Error occurred")
else:
print("No error")
Answer: Option A
Explanation: The else block in try-except executes only if no exception occurs. Since division by zero raises an exception, else block doesn't run.
Q6
Which pdb command steps into function calls?
Answer: Option C
Explanation: The "s" command in pdb steps into function calls, while "n" steps over them.
Q7
What is a common cause of "IndexError: list index out of range"?
Answer: Option C
Explanation: This error occurs when trying to access an index that doesn't exist in the list (beyond the list length).
Q8
How do you set a breakpoint in Python code using pdb?
Answer: Option C
Explanation: import pdb; pdb.set_trace() is the traditional way to set breakpoints in Python code for debugging.
Q9
What does "TypeError: unsupported operand type(s)" typically indicate?
Answer: Option B
Explanation: This error occurs when performing operations between incompatible data types.
Q10
Which command continues execution until the next breakpoint in pdb?
Answer: Option C
Explanation: The "c" command in pdb continues execution until the next breakpoint is encountered.