Error Finding Python Questions Class XI

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:

  1. Line 2: Missing colon (:) at the end of the if statement header.
  2. Line 4: Missing colon (:) at the end of the else statement 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:

  1. Line 1: Missing colon (:) at the end of the function definition (def check_even(num)).
  2. 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:

  1. Line 1: 1st_name is an invalid variable identifier because variable names cannot begin with a digit.
  2. Line 3: Indentation error — statements inside the for loop body must be indented.
  3. Line 4: break is 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:

  1. Line 1: Mismatched string quotation marks (starts with single quote ' and ends with double quote ").
  2. Line 3: Incomplete logical expression — and < 50 lacks 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:

  1. Line 2: Missing colon (:) at the end of the for loop header.
  2. Line 4: String and integer concatenation (item + 10) causes an invalid type operation in Python (must convert integer 10 to string using str()).

Corrected Code:

Python

items = ["apple", "banana", "cherry"]
for item in items:
    if "a" in item:
        print(item + str(10))

Answer 6

Errors Identified:

  1. Line 1: Leading zeros in numeric literals (e.g., 0012) are invalid syntax in Python 3.
  2. Line 4: Print starts with an uppercase ‘P’ — Python is case-sensitive, so print() must be lowercase.
  3. Line 4: Missing comma , separating the string parameter "Result is: " and variable c in print().

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 expression a + b + c / 3 divides only c by 3 before adding a and b, yielding 10 + 20 + 10.0 = 40.0 instead of 20.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 by 0 yields 0. Inside the loop, fact * i will always remain 0.
  • Fix: Initialize fact = 1 as 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). Using or requires only one condition to be True. Since age >= 18 is True (20 $\ge$ 18), the entire condition evaluates to True even though has_voter_id is False.
  • Fix: Use the and logical 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 than 0. Thus, num > max_val never evaluates to True, leaving max_val incorrectly as 0.
  • Fix: Initialize max_val with 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)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top