Courses / HEC NSCT – National Skill Competency Test: Complete Preparation for Computing Graduates
2.2 Functions, Arrays, Strings and Recursion
Area 2 – Programming (C++ / Java / Python) (10%) · 45 min read · 26 MCQs to check yourself
Functions
- Parameters are placeholders; arguments are the actual values passed.
- Pass by value: the function gets a copy (C++ default; Java primitives).
- Pass by reference: the function can change the caller's variable (C++
int &x). Java passes object references by value – you can change the object's contents but not make the caller's variable point to a new object. - Overloading: same name, different parameter lists (C++/Java). Python uses default arguments instead.
- Scope: local variables exist only inside their function.
Arrays and lists
| C++ array | Java array | Python list | |
|---|---|---|---|
| Size | fixed | fixed (length) |
grows (append) |
| First index | 0 | 0 | 0; negative index from the end (a[-1]) |
| Out of range | undefined behaviour | ArrayIndexOutOfBoundsException |
IndexError |
Python slicing: a[start:stop:step] – stop is excluded. a[::-1] reverses.
Strings
Strings are immutable in Java and Python – methods return new strings. Java: s.length(), s.charAt(i), s.substring(a, b). Python: len(s), s[i], s.upper(), s.split(), "-".join(list).
Recursion
A function that calls itself. Needs a base case and a step that moves toward it. Each call uses stack memory, so very deep recursion causes stack overflow.
def fact(n):
if n <= 1: # base case
return 1
return n * fact(n - 1)
A better way to understand recursion (Alex Hyett)
This lesson ends with a quiz of 26 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 2.1 Programming Basics – Data Types, Operators and Control Flow · Next: 2.3 Object-Oriented Programming