Find Syntax error
Find Syntax error
1) Rewrite the following python code after removing any/ all syntactical errors with each
correction underlined:
print("Hello")
print(a)
a=5
var=2
guess
total=0
n=input("Enter a number:
total=m+n
print total
add()
tot= a+b
print(tot)
m=10
n=20
fun(m,p)
if b>0 then:
return “Positive”
else return "Not Positive"
msg=f1()
print(msg)
if a%b= 0:
return true
if isMultiple[m,n]:
h=a
h-=1
return h
HCF (m.n)=h
print("HCF=",h)
Ans
print("Hello")
a=5 #error 2
print(a) #error 3
var=23
guess() #error 4
total=0
total=m+n
print(total) #error 4
add()
tot= a+b
print(tot)
m=10
n=20
if b>0: #error 2
return "Positive"
else: #error 3
msg=f1(m) #error 4
print(msg)
else:
if isMultiple(m,n): error 5
else:
h=a
h-=1
return h
print("HCF=", h) #error 5
return display(string)
return n
return string==str(5)
print(show(“Children’s Day”))
print (display (string=’true’):)
Ans:
return display(string)
print(show("Children’s Day"))
3. Rewrite the following python code after removing any/all syntactical errors with each
correction underlined:
if lst[i]%2=0:
print (lst[i])
displaylist()
Ans:
if lst[i]%2==0:
print (lst[i])
4. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code.
S=0
S+=I
RETURN S
print (SUM[5])
Ans:
S=0
S+=I
return S
print (Sum(5))
x = 50
def func():
global x
print('x is', x)
x=2
func()
Ans: x is 50
Changed global x to 2
print(message*times)
say(‘Hello’)
say( ‘World’, 5)
Ans: Hello
WorldWorldWorldWorldWorld
func (3, 7)
func(c=50,a=100)
a is 25 and b is 5 and c is 24
def f1():
X = 15
print(X)
X = 12
f1()
Ans : 15
x = 12
def f1(a, b = x):
print (a,b)
x = 15
f1(4)
Ans: 4 12
def f():
global a
print(a)
a = “hello”
print(a)
a = “world”
f()
print(a)
Ans: world
hello
hello
x += 15
print(“x=”, x)
x=20
Update()
print(“x=”, x)
Ans: x= 25
x= 20
12. Write the output of the following Python code:
def div5(n):
if n%5==0:
return n* 5
else:
return n + 5
def output ( m= 5) :
print(div5(i),'@',end="")
print()
output(7)
output()
output(3)
0 @6 @7 @8 @9 @
0 @6 @7 @
def func(b):
global x
print(“Global x =”,x)
y = x+b
x=7
z=x-b
print(‘Local x =’ ,x)
print(‘z=’, z)
x=5
func(10)
Ans: Global x = 5
Local x = 7
Y = 15
z = -3
temp = x+y
x+=temp
if (y!=200) :
print(temp,x,x)
a=20
b=10
func(b)
print(a,b)
func(a,b)
print (a,b)
20 10
30 50 50
20 10
l=len(Old)
New=""
for i in range (0,l):
if Old[i].isupper():
New=New+Old[i].lower()
New=New+Old [i].upper()
elif Old[i].isdigit():
New=New+"*"
else:
New=New+ "%"
return New
Older="InDIa@2020"
Newer= Convert(Older)
def display(n):
sum=5
i= 1
while i<=n:
sum += i
i+= 5
sum=sum-2
x = 30
display(x)
Ans: Value = 74 i= 31
x = 15
def change():
global x
# increment value of a by 5
x=x+5
change()
import random
n = random.randrange(1,10)
if guess < n:
print("Too low")
print("Too high!")
guess = int(input("Enter number again: "))
else:
break
Ans: (a)
Q8. What is possible output(s) is/are expected to be displayed on screen after execution of
the following program?
import random
MIN= 25
SCORE=5
for i in range(1,5):
Num=MIN+random.randrange(0,SCORE)
print(Num, end = “&”)
SCORE -=1
Output Options:
(a) 29&26&28&27& (b) 25&26&27&28& (c) 29&25&27&25& (d)
25&26&25&26&
Ans: (b)
Q12. What possible output(s) are expected to be displayed on one of the execution of the
following code is specify the maximum and minimum values which can be assigned to
the ‘Target’ by randrange() function of random module.
import random
X= [11000, 999,99, 9]
Target = random.randrange(0,4)
for i in range(Target):
print(X[i], “#”)
(a) 1000# (b) 999# (c) 1000# (d) 999#
999# 99# 1000# 99#
99# 9# 999#
Ans: (a) , Maximum:3 , Minimum : 0
Q13. From the following Python code shown, below, find out the possible output(s) from
the suggested options.
import random
def week_days():
days=[“Mon”, “Tue”, “Wed”, “Thu”, “Fri”]
v = random.randrange(len(days)-1)
for i in range(v,0,-1):
print(days[i], “-“, i+1)
week_days()
(a) Thu- 4 (b) Wed-3 (c) Wed -3 (d) Fri-5
Wed-3 Tue-2 Tue-2 Thu-4
Tue-2 Mon-1
Ans : (a) & (b)
Q14. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to both the variables Beg and End.
import random
score= [11, 22, 33, 44, 55]
Beg = random.randrange(3)
End = random.randint(2,3)
for C in range(Beg, End):
print(Score[C],'#')
(a) 11# (b) 22# (c) 22# (d) 33#
22# 33# 33# 44#
33# 44#
Ans: (a), (c) Max value to a variable Beg: 2, Min value to a variable End: 3
Q14. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to both the variables Y.
import random
X = random.randrange(1,4)
Y= random.random()
print(“%2d”, %(Y+X), “@”, X)
(a) 00 @ 0 (b) 01 @ 1 (c) 02 @ 2 (d) 04 @ 4
Ans: (b) & (c), Max value to Y:0, Min value to Y: 1
Q15. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code?
import random
n=4
while n>1:
val= random.randint(2,n)
for m in range(val)
if m%2= = 0:
print(m*2, end= “:”)
n=1
Output Option
(a) 0:4:0:0: (b) 0:0:0: (c) 0:4:0:4:0 (d) 0:4:0:4
Ans:
Q16. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the first and last
values that can be assigned to a the variables val in the last iteration.
import random
msg= “Luck”
x=1
for a in msg:
val=random.randint(0, len(msg)-x)
print(mag[val]+ “:”, end= “”)
x=x+1
(a) c:k:u:k: (b) k : u : c : c : (c) l : u : u: c : (d) l : c : u : l :
Q17. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to the variables ‘Assigned’.
import random
Ar= [10,7]
Assigned = random.randrange(10)+1
for C in range(0,2,1):
R=random.randint(1,2)-1
print(Ar[R]+Assigned, “@”, end= “ ”)
(a) 11@8@ (b) 20@21@ (c) 21@20@ (d) 8@11@
Q18. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to the variables ‘high’.
import random
Ar=[11, 22, 33, 44, 55, 66 ]
low=random.randint(1,3)
high=random.randint(2,4)
for c in range(low, high+1):
print(Ar[c], end= “:”)
(a) 11 : 22 : 33 : (b) 22 : 33 : (c) 11 : 22: (d) 44 : 55 : 66
:
Q19. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code?
from random import randint
Vibgyor=[[‘V’, ‘Violet’], [‘I’, ‘Indgo’], [‘B’, ‘Blue’], [‘G’, ‘Green’],[‘Y’, ‘Yellow’],[‘O’,
‘Orange’],[‘R’, ‘Red’]]
for i in range(3):
first=randint(0,1)
last=randint(1,2)+1
print(Vibgyor[last-first], end= ‘:’)
(a) [‘G’, ‘Green’]: [‘G’, ‘Green’]: [‘Y’, ‘Yellow’]:
(b) [‘G’, ‘Green’]: [‘B’, ‘Blue’]: [‘G’, ‘Green’]:
(c) [‘V’, ‘Violet’]: [‘B’, ‘Blue’]: [‘B’, ‘Blue’]:
(d) [‘I’, ‘Indgo’]: [‘B’, ‘Blue’]: [‘B’, ‘Blue’]:
Q20. What are the possible outcome(s) executed from the following code? Also specify the
maximum and minimum values that can be assigned to variable NUMBER.
import random
STRING="CBSEONLINE"
N=9
NUMBER = NUMBER +1
N=N-1
Because Minimum value of NUMBER from random.randint (0, 3) is 0 and while loop will
run only 3 times. So, Minimum value of NUMBER is 3.
Maximum value of NUMBER from random.randint (0, 3) is 3 and while loop will run only
3 times. So, Maximum value of NUMBER is 6.
import random
print (int( 20 + random.random()*5), end =" ")
print (int( 20+ random.random()*5), end =" ")
print (int(20 + random.random()*5), end = " ")
print (int( 20 + random.random()*5))
Find the suggested output options (i) to (iv). Also, write the least value and highest value
that can be generated.
import random
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10))
Find the suggested output options (i) to (iv). Also, write the least value and highest value
that can be generated.
(i) 102 105 104 105 (ii) 110 103 104 105 (iii) 105 107 105 110 (iv) 110 105 105
110
Q23. What are the possible outcome(s) executed from the following code ? Also specify the
maximum and minimum values that can be assigned to variable PICKER.
import random
PICK = random.randint (0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY :
for j in range(1, PICK):
print(I, end =" ")
print()
(i) DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
(ii) DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
(iii) DELHI
MUMBAI
CHENNAI
KOLKATA
(iv) DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA