Functions / Methods MCQs for NSCT
A function is a named block of code that takes inputs (parameters), does one job and can return a value; a method is a function that belongs to a class, which is the only kind Java has. NSCT-style questions on this topic test how arguments reach parameters, whether a change inside a function is visible to the caller, and how recursive calls unwind. It is the fifth topic in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. Programming averaged 47.8% accuracy in 2026, per the HEC NSCT report and APP.
Parameters
A parameter is the variable named in the definition; an argument is the value supplied in the call. Arguments fill parameters by position, or by name in Python (keyword arguments).
def f(a, b=2, c=3):
return a + b * c
f(1, c=4) # a=1, b=2 (default), c=4 -> 9
Defaults must be trailing in both C++ and Python. Python also collects extra arguments: *args into a tuple and **kwargs into a dict. Java and C++ support overloading, where methods share a name but differ in the number, types or order of parameters; the return type alone cannot tell overloads apart. Python has no overloading by signature: a second def with the same name replaces the first.
Exam angle: in Python, the default value is evaluated once, when def runs. A mutable default such as bag=[] is therefore shared by every call that omits it; use bag=None and create the list inside.
Pass by value vs reference
This is where the three languages differ most, and where textbook shortcuts mislead.
| Language | What is passed | Can the callee change the caller's object? | Can it make the caller's variable point elsewhere? |
|---|---|---|---|
C++ int x |
a copy of the value | no (it has a copy) | no |
C++ int &x |
a reference (alias) | yes | yes, assignment changes the caller's variable |
C++ int *p |
a copy of an address | yes, through *p |
no |
| Java | always a copy; for objects, a copy of the reference | yes, via fields or methods | no |
| Python | a reference to the same object (pass-by-object-reference) | yes, if the object is mutable | no |
Java is pass-by-value, full stop; for objects the value copied is the reference. The common phrase "objects are passed by reference" is wrong in the strict sense, because reassigning the parameter never affects the caller:
static void f(Box b) {
b.v = 5; // changes the caller's object
b = new Box(); // only the local copy now points elsewhere
b.v = 9;
}
// after f(x), x.v is 5
Python behaves the same way, and the term for it is call by sharing. x.append(4) inside a function is visible outside; x = [9] is not. Integers, strings and tuples are immutable, so a function can never change them in place.
Exam angle: for every line in the callee, ask "does this mutate the shared object, or rebind the local name?"
Recursion
A recursive function calls itself on a smaller input until it reaches a base case that returns directly. Each call gets its own stack frame with its own locals.
def p(n):
if n == 0:
return
p(n - 1)
print(n, end=" ")
p(3) # 1 2 3: prints happen as the calls unwind
If the base case is missed, as with n - 2 starting from an odd number, the calls never stop: CPython raises RecursionError after about 1,000 frames and Java throws StackOverflowError. Neither CPython nor the standard JVM removes tail calls, so even tail-recursive code is limited by stack depth.
Exam angle: code placed before the recursive call runs on the way down; code placed after it runs on the way back up, in reverse order.
Common traps
- A recursive branch that computes
n + total(n - 1)but forgetsreturnhands backNonein Python. - Assigning to a global name inside a Python function makes it local there; reading it first raises
UnboundLocalErrorunless you declareglobal. - A Java method that reassigns a
Stringor array parameter leaves the caller's variable unchanged. - Naive recursive Fibonacci recomputes the same values;
fib(5)alone makes 15 calls. Memoisation or a loop makes it linear. - JavaScript evaluates default parameters on every call, unlike Python, so the same
bag = []pattern is safe there.
Worked examples
1. Understanding. Which C++ parameter declaration lets a function read a large std::string without copying it and without being able to change it?
Answer: const std::string &s. The reference avoids the copy, and const forbids modification.
2. Application. What does this Python code print?
def g(items):
items += [3]
a = [1, 2]
g(a)
print(a)
Answer: [1, 2, 3]. For a list, += calls extend and mutates the shared object in place. (By contrast, items = items + [3] would build a new list and leave a unchanged.)
3. Analysis. A Java method static int sum(int n) { return n + sum(n - 1); } is called as sum(3). What happens, and what is the fix?
Answer: there is no base case, so the calls continue with 2, 1, 0, -1 and so on until the stack runs out and the JVM throws StackOverflowError. Adding if (n <= 0) return 0; at the start makes it return 6.
Practise Functions / Methods MCQs
The quiz below has 98 questions that you can filter by tier and difficulty.
Practise: 98 questions
FAQs
Is Java pass by value or pass by reference?
Java is always pass-by-value. For primitives the value itself is copied. For objects the value is a reference, and that reference is copied, so a method can change the object's fields but cannot make the caller's variable refer to a different object. This is why a swap(a, b) method cannot swap two caller variables in Java.
Is Python pass by value or pass by reference?
Neither label fits exactly. Python passes a reference to the object, which is called pass-by-object-reference or call by sharing. Mutating a mutable argument, such as appending to a list, is visible to the caller. Assigning a new object to the parameter only rebinds the local name, so the caller sees no change.
When should I use recursion instead of a loop?
Use recursion when the problem is naturally self-similar, such as tree traversal, divide and conquer or backtracking, because the code is shorter and clearer. Use a loop for simple repetition and very deep inputs, because each recursive call costs a stack frame and CPython and Java do not remove tail calls.
What is the difference between a function and a method?
A method is a function defined inside a class and called on an object or the class, for example list.append or Math.max. A function stands alone. C++ and Python have both; in Java every function is a method, and a static method is the closest thing to a free function.
Next step
Continue with Input / Output Handling, the next topic in HEC's order, or go back to Control Structures. You can also browse all Programming MCQs or try an NSCT mock test.