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

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

All lessons in HEC NSCT – National Skill Competency Test: Complete Preparation for Computing Graduates