Error Finding Python Questions Class XI
Instructions:
- Read each Python code snippet carefully.
- Identify all errors in each question.
- Rewrite the corrected Python code for each snippet.
Question 1
Find the syntax errors in the following code snippet:
Python
x = int(input("Enter a number: "))
if x > 10
print("Greater than 10")
else
print("Less than or equal to 10")
Question 2
Identify all syntax errors in the following Python program:
Python
def check_even(num)
if num % 2 = 0:
return True
else:
return False
print(check_even(8))
Question 3
Identify the syntax errors in the code below and explain why each error occurs:
Python
1st_name = "Alice"
for i in range(1, 5):
print("Count:", i)
break = True
Question 4
Locate the syntax errors in the given Python script:
Python
message = 'Hello, Python World!"
val = 25
if val > 20 and < 50:
print(message)
Question 5
Find all syntax errors in the snippet below:
Python
items = ["apple", "banana", "cherry"]
for item in items
if "a" in item:
print(item + 10)
Question 6
Identify the syntax errors in the following code snippet:
Python
a = 0012
b = 5
c = a / b
Print("Result is: " c)
Answer Key
Answer 1
Errors Identified:
- Line 2: Missing colon (
:) at the end of theifstatement header. - Line 4: Missing colon (
:) at the end of theelsestatement header.
Corrected Code:
Python
x = int(input("Enter a number: "))
if x > 10:
print("Greater than 10")
else:
print("Less than or equal to 10")
Answer 2
Errors Identified:
- Line 1: Missing colon (
:) at the end of the function definition (def check_even(num)). - Line 2: Assignment operator (
=) used instead of the relational equality operator (==) in the conditional expression.
Corrected Code:
Python
def check_even(num):
if num % 2 == 0:
return True
else:
return False
print(check_even(8))
Answer 3
Errors Identified:
- Line 1:
1st_nameis an invalid variable identifier because variable names cannot begin with a digit. - Line 3: Indentation error — statements inside the
forloop body must be indented. - Line 4:
breakis a reserved keyword in Python and cannot be assigned as a variable name.
Corrected Code:
Python
first_name = "Alice"
for i in range(1, 5):
print("Count:", i)
is_broken = True
Answer 4
Errors Identified:
- Line 1: Mismatched string quotation marks (starts with single quote
'and ends with double quote"). - Line 3: Incomplete logical expression —
and < 50lacks a left operand; it must explicitly compare against the variable (and val < 50).
Corrected Code:
Python
message = "Hello, Python World!"
val = 25
if val > 20 and val < 50:
print(message)
Answer 5
Errors Identified:
- Line 2: Missing colon (
:) at the end of theforloop header. - Line 4: String and integer concatenation (
item + 10) causes an invalid type operation in Python (must convert integer10to string usingstr()).
Corrected Code:
Python
items = ["apple", "banana", "cherry"]
for item in items:
if "a" in item:
print(item + str(10))
Answer 6
Errors Identified:
- Line 1: Leading zeros in numeric literals (e.g.,
0012) are invalid syntax in Python 3. - Line 4:
Printstarts with an uppercase ‘P’ — Python is case-sensitive, soprint()must be lowercase. - Line 4: Missing comma
,separating the string parameter"Result is: "and variablecinprint().
Corrected Code:
Python
a = 12
b = 5
c = a / b
print("Result is: ", c)
Here are 4 logical error identification questions tailored for Class XI Computer Science (Python) students, followed by the complete answer key at the end.
Question Paper: Find the Logical Errors
Instructions:
- Logical errors occur when code runs without throwing a syntax or runtime error, but produces incorrect results due to flawed logic.
- Identify the logical error in each question, explain why it produces incorrect output, and write the corrected Python code.
Question 1
A student writes the following program to calculate the average of three numbers, but the output generated is incorrect:
Python
a = 10
b = 20
c = 30
average = a + b + c / 3
print("The average is:", average)
Task: Identify the logical error, explain why the output is wrong, and write the corrected code.
Question 2
The following code is intended to compute the factorial of a given positive integer ($n! = n \times (n-1) \times \dots \times 1$). However, the program consistently outputs 0 regardless of the input:
Python
num = 5
fact = 0
for i in range(1, num + 1):
fact = fact * i
print("Factorial of", num, "is:", fact)
Task: Point out the logical bug causing the output to remain 0 and write the corrected program.
Question 3
The following function is supposed to check whether a person is eligible to vote (age $\ge 18$) and has a valid voter ID. However, the program gives an incorrect output for a 20-year-old person who does not have a voter ID:
Python
age = 20
has_voter_id = False
if age >= 18 or has_voter_id == True:
print("Eligible to vote")
else:
print("Not eligible to vote")
Task: Identify why this logical condition fails and provide the corrected code.
Question 4
A student writes a function to find the maximum value in a list of non-positive integers (e.g., negative numbers). However, the program fails to find the correct maximum element:
Python
numbers = [-15, -42, -8, -23, -4]
max_val = 0
for num in numbers:
if num > max_val:
max_val = num
print("Maximum number is:", max_val)
Task: Identify the flaw in initializing max_val and provide the corrected code.
Answer Key
Answer 1
- Logical Error: Operator precedence flaw. In Python, division (
/) has higher precedence than addition (+). The expressiona + b + c / 3divides onlycby 3 before addingaandb, yielding10 + 20 + 10.0 = 40.0instead of20.0. - Fix: Enclose the sum in parentheses
(a + b + c)so addition takes place before division.
Corrected Code:
Python
a = 10
b = 20
c = 30
average = (a + b + c) / 3
print("The average is:", average)
Answer 2
- Logical Error: Initializing
fact = 0. In multiplication, multiplying any number by0yields0. Inside the loop,fact * iwill always remain0. - Fix: Initialize
fact = 1as the identity element for multiplication.
Corrected Code:
Python
num = 5
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial of", num, "is:", fact)
Answer 3
- Logical Error: Incorrect boolean operator (
or). Usingorrequires only one condition to beTrue. Sinceage >= 18isTrue(20 $\ge$ 18), the entire condition evaluates toTrueeven thoughhas_voter_idisFalse. - Fix: Use the
andlogical operator so that both conditions must be satisfied simultaneously.
Corrected Code:
Python
age = 20
has_voter_id = False
if age >= 18 and has_voter_id == True:
print("Eligible to vote")
else:
print("Not eligible to vote")
Answer 4
- Logical Error: Initializing
max_val = 0. Since all elements in the list are negative numbers, none of them are greater than0. Thus,num > max_valnever evaluates toTrue, leavingmax_valincorrectly as0. - Fix: Initialize
max_valwith the first element of the list (numbers[0]) so the comparison works regardless of whether the list contains positive or negative numbers.
Corrected Code:
Python
numbers = [-15, -42, -8, -23, -4]
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
print("Maximum number is:", max_val)