0% found this document useful (0 votes)
7 views10 pages

FZNKT6 Le 2 GBP Fno N2 HBJ

The document contains a series of Python programming questions and answers, focusing on predicting outputs, correcting errors in code, and understanding function parameters. It includes tasks such as rewriting functions with syntax errors, predicting code outputs, and explaining default arguments. Each question is followed by an answer that provides the corrected code or expected output.

Uploaded by

mshivansh616
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)
7 views10 pages

FZNKT6 Le 2 GBP Fno N2 HBJ

The document contains a series of Python programming questions and answers, focusing on predicting outputs, correcting errors in code, and understanding function parameters. It includes tasks such as rewriting functions with syntax errors, predicting code outputs, and explaining default arguments. Each question is followed by an answer that provides the corrected code or expected output.

Uploaded by

mshivansh616
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
You are on page 1/ 10

Working with Python Functions 2 Marks Questions

[1] Predict the output of the following python code:


data = [2,4,2,1,2,1,3,3,4,4]
d = {}
for x in data:
if x in d:
d[x]=d[x]+1
else:
d[x]=1
print(d)
Ans.:
data = [2,4,2,1,2,1,3,3,4,4]

x=2

if 2 in {}:
d[2]=1

if 4 in {2:1}:
d[4]=1

if 2 in {2:1,4:1}:
d[2]=d[2]+1=1+1=2

if 1 in {2:2,4:1}:
d[1]=1
if 2 in {2:2,4:1,1:1}:
d[2]=d[2]+1=2+1=3

if 1 in {2:3,4:1,1:1}:
d[1]=d[1]+1=1+1=2

if 3 in {2:3,4:1,1:2}:
d[3]=1

if 3 in {2:3,4:1,1:2,3:1}:
d[3]=d[3]+1=1+1=2

if 4 in {2:3,4:1,1:2,3:2}:
d[4]=d[4]+1=1+1=2

if 4 in {2:3,4:2,1:2,3:2}:
d[4]=d[4]+1=2+1=3

Ans.:{2:3,4:3,1:2,3:2}
[2] Vivek has written a code to input a number and check whether it is even
or odd number. His code is having errors. Rewrite the correct code and
underline the corrections made.
Def checkNumber(N):
status = N%2
return
#main-code
num=int( input(“ Enter a number to check :"))
k=checkNumber(num)
if k = 0:
print(“This is EVEN number”)
else
print(“This is ODD number”)
Ans.:
def checkNumber(N):
status = N%2
return status
#main-code
num=int( input(“ Enter a number to check :"))
k=checkNumber(num)
if k == 0:
print(“This is EVEN number”)
else:
print(“This is ODD number”)
[3] Sameer has written a python function to compute the reverse of a
number. He has however committed a few errors in his code. Rewrite the
code after removing errors also underline the corrections made.
define reverse(num):
rev = 0
While num > 0:
rem == num %10
rev = rev*10 + rem
num = num/10
return rev
print(reverse(1234))
Ans.:
def reverse(num):
rev = 0
while num > 0:
rem = num %10
rev = rev*10 + rem
num = num//10
return rev
print(reverse(1234))
[4] Predict the output for following code:
def printMe(q,r=2):
p=r+q**3
print(p)
#main-code
a=10
b=5
printMe(a,b)
printMe(r=4,q=2)
Ans.:
a=10
b=5
printMe(10,5)

q=10
r=5
p=r+q**3
=5+10**3
=5+1000
=1005
printMe(4,2)

q=2
r=4
p=r+q**3
=4+2**3
=4+8
=12
Output:
1005
12
[5] Predict the output of the following python code:
def foo(s1,s2):
l1=[]
l2=[]
for x in s1:
l1.append(x)
for x in s2:
l2.append(x)
return l1,l2

a,b=foo("FUN",'DAY')
print(a,b)
Ans:
a,b=foo("FUN","DAY")
foo('FUN','DAY')
l1=[]
l2=[]
for x in 'FUN':
l1.append(x)
So l1=['F','U','N']
for x im 'DAY':
l2.append(x)
So l2=['D','A','Y']

The output is:


['F','U','N']['D','A','Y']
[6] Preety has written a code to add two numbers . Her code is having
errors. Rewrite the correct code and underline the corrections made.
def sum(arg1,arg2):
total=arg1+arg2;
print(”Total:”,total)
return total;

sum(10,20)
print(”Total:”,total)
Ans.:
def sum(arg1,arg2):
total=arg1+arg2 #Semicolon not required
print(”Total:”,total)
return total #Any one statement is enough either return or print
total=sum(10,20)#total variable needs to be call a function
print(”Total:”,total)
[7] What do you understand the default argument in function? Which
function parameter must be given default argument if it is used? Give
example of function header to illustrate default argument.
Ans.:
The value provided in the formal arguments in the definition header of a
function is called as default argument in function.
They should always be from right side argument to the left in sequence.
For example:
def func( a, b=2, c=5):
# definition of function func( )
here b and c are default arguments
[8] Ravi a python programmer is working on a project, for some
requirement, he has to define a function with name CalculateInterest(), he
defined it as:
def CalculateInterest (Principal, Rate=.06,Time):
# code
But this code is not working, Can you help Ravi to identify the error in the
above function and what is the solution?
In the function CalculateInterest (Principal, Rate=.06,Time) parameters
should be default parameters from right to left hence either Time should be
provided with some default value or default value of Rate should be
removed
[9] Rewrite the following code in python after removing all the syntax
errors. Underline each correction done in the code.
Function F1():
num1,num2 = 10
While num1 % num2 = 0
num1+=20
num2+=30
Else:
print('hello')
Ans.:
def F1():
num1,num2 = 10, value is missing
while num1 % num2 == 0:
num1+=20
num2+=30
else:
print('hello')
[10] Predict the output of the following code fragment:
def Change(P ,Q=30):
P=P+Q
Q=P-Q
print(P,"#",Q)
return(P)
R=150
S=100
R=Change(R,S)
print(R,"#",S)
S=Change(S)
Ans.:
R=150
S=100
R=change(150,100)
p=p+q
=150+100
=250
q=p-q
=250-100
=150

r=250
s=150

Print 1 - 250#150
Print 2 - 250#100

R=100
S=30
R=change(100,30)
p=p+q
=100+30
=130
q=p-q
=130-30
=150

Print 3 - 130#100

Answer:
250#150
250#100
130#100

130#100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy