Test Index

CBSE Class 12 Computer Science 2026 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 26 of 37
Marks: +1, -0
The code given below is intended to display all numbers not divisible by 7 from the given list. However, there are syntax and logical errors in the code. Rewrite it after removing all errors. Also, underline all the corrections made.
define display(L):
for i in range(0,len(L))
if i % 7 = = 0:
break
else:
print(L[i])
numbers = [11,21,33,42,55]
display(numbers)
Solution:  
Corrected code (corrections underlined):
def display(L):
for i in range(0,len(L)):
if L[i] % 7 == 0:
continue
else:
print(L[i])
numbers = [11,21,33,42,55]
display(numbers)
Corrections:
1. define → def: a function is defined with the keyword def (syntax error).
2. Colon : added at the end of the for statement (syntax error).
3. = = → ==: the equality operator has no space inside it (syntax error).
4. i % 7 → L[i] % 7: i is only the index; the number itself is L[i] (logical error).
5. break → continue: break would stop the loop at the first multiple of 7, so later numbers (33, 55) would never be checked; continue just skips that number (logical error).
Output:
11
33
55
21 and 42 are divisible by 7, so they are skipped; 11, 33 and 55 are printed.
© examsnet.com
Advertisements
Go to Question: