Functions / Methods MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. What is the difference between a parameter and an argument?

    1. An argument is the variable in the definition; a parameter is the value passed in the call
    2. They are two names for exactly the same thing in every context
    3. Parameters belong to methods and arguments belong to free functions
    4. A parameter is the variable in the function definition; an argument is the value passed in the call
    Show answer

    Answer: D. A parameter is the variable in the function definition; an argument is the value passed in the call

    In def area(r): ..., r is a parameter (also called a formal parameter). In area(5), 5 is the argument (actual parameter). When the call runs, the parameter is initialised from the argument.

  2. How does Java pass arguments to methods?

    1. Primitives by value and objects by reference
    2. Always by reference
    3. Always by value; for an object, the value copied is the reference
    4. By value for methods and by reference for constructors
    Show answer

    Answer: C. Always by value; for an object, the value copied is the reference

    Java is strictly pass-by-value. For a primitive the value itself is copied. For an object the reference is copied, so the method can change the object's fields but cannot make the caller's variable point to a different object.

  3. Which description of Python argument passing is correct?

    1. Every argument is copied, so the caller never sees any change
    2. It works like C++ references: assigning to the parameter changes the caller's variable
    3. The parameter is bound to the same object as the argument; mutating a mutable object is visible to the caller, rebinding the parameter is not
    4. Immutable objects are passed by reference and mutable objects are copied
    Show answer

    Answer: C. The parameter is bound to the same object as the argument; mutating a mutable object is visible to the caller, rebinding the parameter is not

    Python passes object references, often called pass-by-object-reference or call by sharing. x.append(1) changes the shared list, but x = [] only rebinds the local name. Integers and strings are immutable, so a function cannot change them in place.

  4. Which C++ function declaration lets the function change the caller's int variable directly?

    1. void f(int x);
    2. void f(const int x);
    3. void f(const int &x);
    4. void f(int &x);
    Show answer

    Answer: D. void f(int &x);

    int &x is a reference parameter: x becomes another name for the caller's variable, so assignments to x change it. int x receives a copy, and the two const versions forbid modification.

  5. In a recursive function, what is the base case?

    1. The first call made from main
    2. A condition under which the function returns without calling itself
    3. The call with the largest input
    4. The line that contains the recursive call
    Show answer

    Answer: B. A condition under which the function returns without calling itself

    The base case stops the recursion. Every recursive call must move the input towards it; otherwise calls pile up until the stack overflows.

  6. What happens in CPython when a recursive function never reaches its base case?

    1. It runs forever without any error
    2. It returns None after the limit is reached
    3. A SyntaxError is reported before the program runs
    4. A RecursionError is raised once the recursion limit (about 1,000 frames by default) is exceeded
    Show answer

    Answer: D. A RecursionError is raised once the recursion limit (about 1,000 frames by default) is exceeded

    CPython guards the call stack with a recursion limit (sys.getrecursionlimit(), 1000 by default). Exceeding it raises RecursionError. Java reports a similar problem as StackOverflowError.

  7. In Java, which of these can distinguish two overloaded methods with the same name?

    1. Their return types alone
    2. The number, types or order of their parameters
    3. Their parameter names alone
    4. Their access modifiers alone
    Show answer

    Answer: B. The number, types or order of their parameters

    Overloads must differ in their parameter lists (the method signature). Two methods that differ only in return type, parameter names or modifiers cause a compile error.

  8. What happens when a Python module defines two functions with the same name but different numbers of parameters?

    1. Python keeps both and chooses by argument count, like Java
    2. Python raises a SyntaxError for the duplicate name
    3. The second def replaces the first, so only the later version exists
    4. typing.overload makes both run as separate implementations
    Show answer

    Answer: C. The second def replaces the first, so only the later version exists

    def is an assignment of a function object to a name, so the later definition rebinds the name. Python gets flexible calls from default values, *args and **kwargs instead. typing.overload only describes signatures for type checkers.

  9. What is the purpose of a function prototype in C++?

    1. It allocates memory for the function's local variables
    2. It declares the function's name, return type and parameter types so calls can be checked before the definition appears
    3. It provides the function body
    4. It forces the compiler to inline the function
    Show answer

    Answer: B. It declares the function's name, return type and parameter types so calls can be checked before the definition appears

    A prototype such as int add(int, int); tells the compiler the function's interface. The body can then appear later in the file or in another translation unit.

  10. Which rule about default parameter values holds in both C++ and Python?

    1. Defaults may be given to any parameters in any order
    2. Only the first parameter may have a default
    3. Once a parameter has a default, every parameter after it must have one too
    4. Default values are allowed only in methods, not in free functions
    Show answer

    Answer: C. Once a parameter has a default, every parameter after it must have one too

    Arguments fill parameters from the left, so defaults must be trailing. In C++, void f(int a = 1, int b); is an error; in Python, def f(a=1, b): is a SyntaxError. (Python's keyword-only parameters after * are the exception.)

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.