Advanced Programming Concepts MCQs for the NSCT
Advanced programming concepts are the features that let you write code once and reuse it safely across types and behaviours: generics in Java, templates in C++, lambdas and higher-order functions, and iterators and generators. It is topic 13 of the 16 in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. Candidates averaged 47.8% accuracy in this area in 2026, per the HEC NSCT report.
Most questions are short output traces, so make sure you are comfortable with functions and methods first. Closures and decorators build directly on them.
Generics
Generics let a class or method work with a type parameter, so the compiler can check types for you. In Java, List<String> rejects an Integer at compile time, and get() returns a String with no cast.
static <T extends Comparable<T>> T largest(List<T> xs) {
T best = xs.get(0);
for (T x : xs) if (x.compareTo(best) > 0) best = x;
return best;
}
The bound T extends Comparable<T> is what allows the call to compareTo. Java implements generics by type erasure: the type arguments are checked and then removed, so at run time List<String> and List<Integer> share one class. That is why new T() and List<int> are illegal.
| Rule | Meaning |
|---|---|
| Invariance | List<String> is not a List<Object> |
? extends Number |
Safe to read as Number; cannot add (except null) |
? super Integer |
Safe to add an Integer; reads give Object |
Exam angle: a line like List<Object> objs = new ArrayList<String>(); does not compile. Expect at least one "which line fails?" question built on invariance.
Lambdas
A lambda is an anonymous function written inline. Each language has its own syntax:
| Language | Syntax | Notes |
|---|---|---|
| Python | lambda a, b: a + b |
One expression only; no return |
| Java | (a, b) -> a + b |
Must target a functional interface (one abstract method) |
| C++ | [&](int a, int b) { return a + b; } |
Capture list: [=] copies, [&] references |
| JavaScript | (a, b) => a + b |
Arrow function |
Lambdas are usually passed to higher-order functions, which take or return functions:
students = [("Hira", 82), ("Usman", 74), ("Zara", 91)]
print(sorted(students, key=lambda s: s[1], reverse=True))
# [('Zara', 91), ('Hira', 82), ('Usman', 74)]
A closure is an inner function that keeps access to its enclosing function's variables after that function returns. A decorator such as @timer is shorthand for f = timer(f).
Exam angle: closures capture variables, not values. [lambda: i for i in range(3)] gives three functions that all return 2.
Templates
A C++ template is a blueprint from which the compiler generates a separate function or class for each type it is used with:
template <typename T>
T maxOf(T a, T b) { return a > b ? a : b; }
// maxOf(3, 7) -> maxOf<int>, maxOf(2.5, 1.5) -> maxOf<double>
Templates and Java generics solve the same problem in different ways. Textbooks contrast them like this:
| C++ templates | Java generics | |
|---|---|---|
| When resolved | Compile time, code generated per type | Compile time, then erased |
| Primitive types | Allowed (maxOf<int>) |
Not allowed (use Integer) |
| Type checked | When instantiated | When declared, using bounds |
Exam angle: maxOf(3, 2.5) does not compile, because T is deduced as int from one argument and double from the other. maxOf<double>(3, 2.5) fixes it.
Iterators
An iterator hands out the items of a collection one at a time. Python's protocol is __iter__ plus __next__, which raises StopIteration at the end. Java's is hasNext() plus next(). A generator is the easiest way to write one in Python:
def countdown(n):
while n > 0:
yield n
n -= 1
print(list(countdown(3))) # [3, 2, 1]
Generators are lazy: calling countdown(3) runs nothing until values are requested.
Exam angle: an iterator can be consumed once. After sum(it), a second sum(it) returns 0. In Java, removing from a list inside a for-each loop throws ConcurrentModificationException.
Common traps
- Thinking
List<String>is a subtype ofList<Object>. Java generics are invariant. - Writing
returninside a Python lambda. The body must be a single expression. - Forgetting late binding: lambdas created in a loop all see the loop variable's final value.
- Reusing an exhausted iterator or generator and expecting the values again.
- Passing
key=lambda w: w.lowerwithout parentheses. That compares method objects and raisesTypeError. - Assuming a template compiles for any type. It fails when instantiated with a type that lacks an operator it uses.
Worked examples
1. Understanding. Is Python's range(3) an iterator?
Answer: No, it is an iterable. It has no __next__ method, and you can loop over the same range many times. iter(range(3)) returns a separate iterator object that can be used once.
2. Application. What does this print?
def pairs():
yield from zip("ab", [1, 2])
print(dict(pairs()))
Answer: {'a': 1, 'b': 2}. yield from passes on each tuple that zip produces, and dict() turns the key-value pairs into a dictionary.
3. Analysis. In JavaScript, [10, 1, 2].sort() returns [1, 10, 2]. Why, and what is the fix?
Answer: with no comparator, sort() converts elements to strings and compares them as text, and "10" sorts before "2". Passing a lambda, .sort((a, b) => a - b), compares numerically and returns [1, 2, 10].
Practise
The quiz below has 100 questions on advanced programming concepts, filterable by tier and difficulty.
Practise: 100 questions
FAQs
Are Java generics and C++ templates the same thing?
No. Both give type-safe reusable code, but C++ generates separate code for each type at compile time, while Java checks the types and then erases them, leaving one shared class. So C++ templates accept primitive types and Java generics do not. Java also cannot create a new T().
When should I use a lambda instead of a named function?
Use a lambda for a short, one-off function passed straight to another function, such as a sort key or a map callback. If the logic needs several statements, a name or a docstring, write a normal function. Python lambdas are limited to one expression, so longer logic must be a def.
What is the difference between an iterable and an iterator?
An iterable can produce an iterator; lists, strings, ranges and dictionaries are iterables. An iterator holds the current position and returns the next item each time next() is called, until it is exhausted. A for loop calls iter() on the iterable for you, which is why lists can be looped over repeatedly.
Do I need to know C++20 concepts or Java streams for the NSCT?
HEC's topic list names this area but does not go into that level of detail. Make sure you are solid on generics, lambdas, templates and iterators first. Streams are a natural extension, since stream().filter(...).map(...) is just higher-order functions and lambdas used together.
Next step
Continue to Concurrency & Parallelism MCQs, where lambdas reappear as thread tasks. Revisit the Programming MCQs hub or test yourself in an NSCT mock test.