loading…
CBSE Class 12 · Computer Science (083)
CBSE Class 12 Computer Science functions: default arguments, global and local scope, return values — predict-the-output questions with answers and programs with output.
def even_odd(L):
even = odd = 0
for n in L:
if n % 2 == 0:
even += 1
else:
odd += 1
return even, odd
e, o = even_odd([4, 7, 10, 13, 18])
print("Even:", e, "Odd:", o)
Output
Even: 3 Odd: 2
def LShift(arr, n):
n = n % len(arr)
arr[:] = arr[n:] + arr[:n]
A = [10, 20, 30, 40, 12, 11]
LShift(A, 2)
print(A)
Output
[30, 40, 12, 11, 10, 20]
def change(a, b=10):
a = a + b
b = a - b
return a, b
x, y = change(5)
print(x, y)
print(change(b=1, a=2))
15 5 (3, 2)
Why: b uses its default 10 when not passed; keyword arguments can come in any order.
count = 0
def increment():
global count
count += 2
return count
print(increment(), increment(), count)
2 4 4
Why: global lets the function change the variable outside it.
def show(n):
n = n * 2
print("inside", n)
n = 7
show(n)
print("outside", n)
inside 14 outside 7
Why: The parameter n is a separate local name; the outside n is not changed.
def area(l, b)
a = l * b
return a
print(area(4, 5))
Running it stops with SyntaxError.
def area(l, b):
a = l * b
return a
print(area(4, 5))
PyRun is not affiliated with CBSE. Questions are written in the board's style for practice; they are not past papers.