0% found this document useful (0 votes)
2 views2 pages

Python Debugging Questions

The document provides a list of common beginner Python debugging questions along with their answers. Each question highlights a specific error type, such as Syntax Error, Indentation Error, and Name Error, and offers the correct code to resolve the issue. This serves as a helpful guide for new Python programmers to troubleshoot and understand common mistakes.
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)
2 views2 pages

Python Debugging Questions

The document provides a list of common beginner Python debugging questions along with their answers. Each question highlights a specific error type, such as Syntax Error, Indentation Error, and Name Error, and offers the correct code to resolve the issue. This serves as a helpful guide for new Python programmers to troubleshoot and understand common mistakes.
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

Beginner Python Debugging Questions with Answers

1. Syntax Error
print("Hello World"
Answer: Missing closing parenthesis. Correct Code: print("Hello World")

2. Indentation Error
if 5 > 2:
print("Five is greater")
Answer: Python requires indentation inside blocks. Correct Code: if 5 > 2: print("Five is greater")

3. Name Error
print(name)
Answer: Variable 'name' not defined. Correct Code: name = "Python" print(name)

4. Type Error
age = 20
print("Age is " + age)
Answer: Cannot concatenate string and integer. Correct Code: print("Age is", age)

5. Division by Zero
num = 10 / 0
Answer: Division by zero is not allowed. Fix by checking denominator before division.

6. Wrong Variable Name


marks = 90
print(mark)
Answer: Used wrong variable name. Correct Code: print(marks)

7. Missing Colon
for i in range(5)
print(i)
Answer: Colon ':' missing after loop. Correct Code: for i in range(5): print(i)

8. Index Error
list1 = [1,2,3]
print(list1[3])
Answer: Index out of range (Python starts from 0). Correct Code: print(list1[2])
9. Infinite Loop
while True:
print("Hello")
Answer: Loop never stops. Fix by adding condition: count = 0 while count < 5: print("Hello")
count += 1

10. Incorrect Comparison


if x = 5:
print("Yes")
Answer: Used assignment '=' instead of comparison '=='. Correct Code: if x == 5:

11. String Not Closed


msg = "Welcome to Python
Answer: Closing quotation missing. Correct Code: msg = "Welcome to Python"

12. Using Keyword as Variable


class = 10
Answer: 'class' is reserved keyword. Correct Code: cls = 10

13. Forgetting to Convert Input


age = input("Enter age: ")
print(age + 5)
Answer: input() returns string. Correct Code: age = int(input("Enter age: ")) print(age + 5)

14. Wrong Function Name


import math
print([Link](16))
Answer: Function name incorrect. Correct Code: print([Link](16))

15. List Modification Error


numbers = (1,2,3)
numbers[0] = 10
Answer: Tuple is immutable (cannot change). Correct Code: numbers = [1,2,3] numbers[0] = 10

You might also like