Learn › Class 12 › Computer Science / Informatics Practices › Python revision, functions, exceptions
Functions in Python
Work it out on paper first. Then check the hint or the answer.
1. def f(a, b=4): return a * b. What does f(3) return?
Hint
b takes its default value.
Answer and explanation
Answer: 12
f(3) uses b = 4, so it returns 3 * 4 = 12.
2. def g(n): return n % 3 + n // 3. What does g(10) return?
Hint
10 % 3 is the remainder; 10 // 3 is floor division.
Answer and explanation
Answer: 4
10 % 3 = 1 and 10 // 3 = 3, so the result is 4.
3. def h(x): x.append(1). After L = [0] and h(L), what is len(L)?
Hint
Lists are mutable.
Answer and explanation
Answer: 2
h adds 1 to the same list object, so L becomes [0, 1].
4. What value does a function return if it has no return statement?
Hint
It is a special Python object meaning 'no value'.
Answer and explanation
Answer: None
Python returns None automatically when a function ends without return.