TYPE C: PROGRAMMING PRACTICE / KNOWLEDGE BASED
QUESTIONS
Python — Functions, Scope & Output Tracing
Q1. What is the output of the following code snippet?
def ChangeVal(M, N):
for i in range(N):
if M[i] % 5 == 0:
M[i] //= 5
if M[i] % 3 == 0:
M[i] //= 3
L = [25, 8, 75, 12]
ChangeVal(L, 4)
for i in L:
print(i, end="#")
Q2. What is the output of the following code snippet?
x = 3
def myfunc():
global x
x += 2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')
Q3. Differentiate between actual parameters and formal parameters with a suitable example
for each.
Q4. Explain the use of the global keyword used in a function with the help of a suitable
example.
Q5. Write the output on execution of the following Python code:
def ALTER(Y=25):
global X
Y += X
X += Y
print(X, Y, sep="#")
X = 5
Y = 15
ALTER(Y)
ALTER()
print(X, Y, sep="@")
Q6. What will be the output of the following code segment?
a = 5
def func_1(b=10):
global a
a = b - 10
b += a
print(a, b)
func_1(a)
Choose the correct option:
(A) 0 5
(B) 5 0
(C) 0 -5
(D) -5 0
Q7. The code below is intended to remove the first and last characters of a given string and
return the resulting string. Rewrite it after removing all syntax and logical errors:
def remove_first_last(s):
if len(s) < 2:
return s
new_str = s[1:-1]
return new_str
result = remove_first_last("Hello")
print("Resulting string:", result)
Q8. What will be the output of the following Python code?
i = 5
print(i, end='@@')
def add():
global i
i = i + 7
print(i, end='##')
add()
print(i)
Choose the correct option:
(a) 5@@12##12
(b) 5@@5##12
(c) 5@@12##15
(d) 12@@12##12
Q9. Find and write the output of the following Python code:
def Display(str):
m = ""
for i in range(0, len(str)):
if str[i].isupper():
m = m + str[i].lower()
elif str[i].islower():
m = m + str[i].upper()
else:
if i % 2 == 0:
m = m + str[i-1]
else:
m = m + "#"
print(m)
Display('Fun@Python3.0')
Q10. The code below accepts N as an integer argument and returns the sum of all integers
from 1 to N. Rewrite it after removing all syntax and logical errors. Underline all corrections
made.
def Sum(N):
S = 0
for I in range(1, N + 1):
S = S + I
return S
print(Sum(10))
Q11. What does the return statement do in a function? Explain with the help of an example.
Q12. Consider the statements given below and choose the correct output:
def Change(N):
N = N + 10
print(N, end='$$')
N = 15
Change(N)
print(N)
Choose the correct option:
(A) 25$$15
(B) 15$$25
(C) 25$$25
(D) 2525$$
Q13. Write the output on execution of the following Python code:
def ALTER(Y=25):
global X
Y += X
X += Y
print(X, Y, sep="#")
X = 5
Y = 15
ALTER(Y)
ALTER()
print(X, Y, sep="@")