[go: up one dir, main page]

0% found this document useful (0 votes)
33 views6 pages

Working With Functions Problems 2

The document contains a series of programming questions and their outputs related to local and global variables in Python. Each question involves defining functions that manipulate global variables and tracing the flow of control to determine the output. Additionally, some questions ask for missing statements to achieve specific outputs or suggest remedies for issues in the code.

Uploaded by

thanoshu55
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)
33 views6 pages

Working With Functions Problems 2

The document contains a series of programming questions and their outputs related to local and global variables in Python. Each question involves defining functions that manipulate global variables and tracing the flow of control to determine the output. Additionally, some questions ask for missing statements to achieve specific outputs or suggest remedies for issues in the code.

Uploaded by

thanoshu55
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/ 6

The following solved questions deal with local and global variables.

In case of additional
instructions in a few questions, follow those, in addition to tracing the flow of control and
calculating the output.

1.
1. def state1 ( ) : Output :
2. global tigers 95
3. tigers = 15 15
4. print (tigers) 15
5.
6. tigers = 95
7. print (tigers)
8. state1 ( )
9. print (tigers)

2.
1. v = 50 Output :
2. def Change (N) : 20#50@20
3. global V
4. V, N = N, V
5. print (V, N, sep = “#”, end = “@”)
6. Change (20)
7. print (V)

3.
1. c = 10 Output :
2. def add ( ) : Inside function : 12
3. global c In main : 15
4. c=c+2
5. print (“Inside function : ”, c)
6. add ( )
7. c = 15
8. print (“In main : ”, c)

4.
1. p=5 Output :
2. def sum (q, r = 2) : 105#6#
3. global p
4. p = r + q ** 2
5. print (p, end = “#”)
6. a = 10
7. b=5
8. sum (a, b)
9. sum (r = 5, q = 1)
5.
1. b = 100 Answer :
2. def test (a) : (c) global b
3. ________ #missing statement
4. b=b+a
5. print (a, b)
6. test (10)
7. print (b)

Q. Which statement should be written in the blank if


output is 110 ?
(a) global a (b) global b = 100
(c) global b (d) global a = 100

6. The above program is written to swap the values of two variables through a function, but upon
running the code, the output shows that the values are swapped inside the function but in the
main program, the variables remain un-swapped. Suggest a remedy.
1. def switch (x, y) : Answer :
2. x, y = y, x Changing x and y inside to function
3. print (“Inside switch :”, end = “ ”) from local variables to global
4. print (“x =”, x, “y =”, y) variables.
5. x=5
6. y=7
7. print (“x =”, x, “y =”, y)
8. switch (x, y)
9. print (“x =”, x, “y =”, y)

7. Find the output of the following programs and note the difference.
1. num = 1 Output :
2. def myfunc ( ) : 1
3. return num 1
4. print (num) 1
5. print (myfunc ( ))
6. print (num)
---------------------------------------------------------------------- -----------------------------------------------
1. num = 1 Output :
2. def myfunc ( ) : 1
3. num = 10 10
4. return num 1
5. print (num)
6. print (myfunc ( ))
7. print (num)
---------------------------------------------------------------------- -----------------------------------------------
1. num = 1 Output :
2. def myfunc ( ) : 1
3. global num 10
4. num = 10 10
5. return num
6. print (num)
7. print (myfunc ( ))
8. print (num)
8.
1. a = 10 Output :
2. def call ( ) : 15
3. global a
4. a = 15
5. b = 20
6. print (a)
7. call ( )

9.
1. L = [5, 10, 15, 1] Output :
2. G=4 9$14$19$5$
3. def Change (X) :
4. global G
5. N = len (X)
6. for i in range (N) :
7. X [i] += G
8. Change (L)
9. for i in L :
10. print (i, end = “$”)

10.
1. p=1 Output :
2. q=6 6 6
3.
4. def change_values ( ) :
5. global p
6. q=5
7. p=p+q
8. return (p)
9.
10. change_values ( )
11. print (p, q)

11.
1. a = 30 Output :
2. def call (x) : 65#70@
3. global a
4. if a % 2 == 0 :
5. x += a
6. else :
7. x -= a
8. return x
9. x = 20
10. print (call (35), end = “#”)
11. print (call (40), end = “@”)
12.
1. Local = 100 Output :
2. def Update (Global = 0) : 150 # 50
3. global Local 150 # 0
4. Local += Global
5. print (Local, “#”, Global)
6.
7. Update (50)
8. Update ( )

13.
1. value = 10 Output :
2. def show (n) : 10#5%
3. global value
4. value = 25
5. if value % 2 == 0 :
6. value = value + n
7. else :
8. value = value – n
9. print (value, end = “#”)
10. show (20)
11. print (value, end = “%”)

14.
1. x=5 Output :
2. def function1 ( ) : 21 , 7
3. global x
4. y=x+x*2
5. print (y, end = “,”)
6. x=7
7. function1 ( )
8. print (x)

15.
1. value = 50 Output :
2. def display (N) : 50#5
3. global value
4. value = 25
5. if N % 7 == 0 :
6. value = value + N
7. else :
8. value = value – N
9. print (value, end = “#”)
10. display (20)
11. print (value)
16.
1. V = 50 Output :
2. def display (N) : 70*2
3. global V
4. V = 35
5. if N % 9 == 0 :
6. V=V*N
7. else :
8. V=V/N
9. print (V, end = “*”)
10. display (15)
11. print (int (V))

17.
1. g=0 Output :
2. def fun1 (x, y) : –2
3. global g
4. g=x+y
5. return g
6. def fun2 (m, n) :
7. global g
8. g=m–n
9. return g
10. x = fun1 (2, 3)
11. fun2 (x, 7)
12. print (g)

18.
1. g=0 Output :
2. def fun1 (x, y) : –3
3. global g
4. g=x+y
5. return g
6. def fun2 (m, n) :
7. global g
8. g=m–n
9. return g
10. x = fun1 (3, 4)
11. fun2 (x, 10)
12. print (g)

19.
1. x=3 Output :
2. def myfunc ( ) : 3 5 5
3. global x
4. x += 2
5. print (x, end = “ ”)
6. print (x, end = “ ”)
7. myfunc ( )
8. print (x, end = “ ”)
20.
1. c = 10 Output :
2. def add ( ) : 12#15%
3. global c
4. c=c+2
5. print (c, end = “#”)
6.
7. add ( )
8. c = 15
9. print (c, end = “%”)

21.
1. a = 15 Output :
2. def update (x) : 20$4
3. global a
4. a += 2
5. if x % 2 == 0 :
6. a *= x
7. else :
8. a //= x
9. a=a+5
10. print (a, end = “$”)
11. update (5)
12. print (a)

You might also like