Programming Fundamentals MCQs for NSCT
Programming fundamentals covers how source code becomes a running program (compilers, interpreters and the build steps between them), the syntax rules each language enforces, and how a program is structured from its entry point onwards. It is the first topic in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 47.8% accuracy in this area, per the HEC NSCT report.
Compilers vs interpreters
A compiler translates a whole source program into another form (machine code or bytecode) before it runs. An interpreter translates and executes the program statement by statement while it runs. Real languages mix both:
| Language | What happens | Runs on |
|---|---|---|
| C++ | Preprocessor → compiler → assembler → linker produce a native executable | The CPU directly; rebuild for each OS |
| Java | javac compiles to bytecode (.class); the JVM interprets it and JIT-compiles hot code |
Any machine with a JVM |
| Python (CPython) | Compiles the whole file to bytecode, then the Python virtual machine interprets it | Any machine with Python installed |
| JavaScript | Engines such as V8 parse the source and JIT-compile it | A browser or Node.js |
A linker joins object files and libraries into one executable; a JIT (just-in-time) compiler turns frequently run code into native code at run time.
Exam angle: "Python is interpreted" is a simplification. Because CPython compiles the whole file first, a syntax error on line 10 stops the program before line 1 prints anything.
Syntax
Syntax is the set of grammar rules for writing valid statements. Breaking one gives a syntax error, reported before the faulty code runs. The three languages differ in visible ways:
| Rule | C++ / Java | Python |
|---|---|---|
| End of statement | ; required |
End of line (; optional between statements) |
| Blocks | { } |
Indentation after a : |
| Comments | // and /* */ |
# |
| Case-sensitive | Yes | Yes |
Identifiers (names) may use letters, digits and underscores but cannot start with a digit or be a keyword, so _total2 is valid and 2total and class are not.
Errors fall into three groups. Compile-time errors (syntax errors, and in Java or C++ type errors such as int n = "ten";) stop the build. Runtime errors (division by zero, a bad array index) occur while the program runs. Logic errors produce wrong output with no message at all.
Exam angle: in Python a misspelled variable is not a syntax error. It raises NameError only if that line actually runs, so a typo in an untested branch can hide for months.
Program structure
Every program needs an entry point, the place execution starts:
#include <iostream> // preprocessor directive
int main() { // C++ entry point
std::cout << "Hello";
} // reaching the end of main acts as return 0
In Java the entry point is public static void main(String[] args) inside a class, and a public class must sit in a file with the same name. In Python, code runs top to bottom and def is an executable statement, so calling a function above its def raises NameError. The idiom if __name__ == "__main__": runs code only when the file is executed directly, not when it is imported.
Before coding, algorithms are often sketched as pseudocode or a flowchart: ovals mark start and end, rectangles are process steps, parallelograms are input or output, and diamonds are decisions.
Exam angle: questions often show the same idea in two languages. JavaScript hoists function declarations, so it can call a function above its definition; Python cannot.
Common traps
- Calling Python purely line-by-line: the file is compiled to bytecode first, so syntax errors stop everything.
- Mixing up the linker (joins object files) and the loader (puts the executable in memory).
- Assuming dead code after
returnis an error everywhere. Java rejects it as an "unreachable statement", but Python accepts it silently. - Treating a declared but undefined C++ function as a compile error. It compiles; the linker fails with "undefined reference".
- Forgetting that
Totalandtotalare different names in all three languages.
Worked examples
1. Understanding. Which stage of a C++ build replaces #include <vector> with the header's contents?
Answer: the preprocessor. It runs before compilation proper and handles every line starting with #. The linker comes after compilation and works on object files, not source text.
2. Application. What does this Python script print?
print("A")
total = 5 + "5"
print("B")
Answer: A, followed by a TypeError. The line is grammatically valid, so the file compiles and starts running. The type mismatch is only detected when line 2 executes, so B never prints. Compare this with a missing colon, which would stop the file before A.
3. Analysis. Java is statically typed and checked by a compiler. Why does this code still compile, and what happens when it runs?
int[] marks = new int[3];
marks[3] = 90;
Answer: it compiles, then throws ArrayIndexOutOfBoundsException at run time. The compiler checks types: marks[3] is an int slot and 90 is an int, so the statement is well typed. Array length is a property of the object created at run time, so the bounds check happens when the line executes. Valid indexes are 0 to 2.
Practise Programming Fundamentals MCQs
The quiz below has 99 questions that you can filter by tier and difficulty.
Practise: 99 questions
FAQs
Is Java compiled or interpreted?
Both. javac compiles source code into bytecode ahead of time. The Java Virtual Machine then runs that bytecode, interpreting it at first and JIT-compiling frequently used parts into native machine code. That is why one .class file runs on any operating system with a JVM, while still running fast once it warms up.
Is Python a compiled language?
The standard implementation, CPython, compiles each file to bytecode and then interprets that bytecode in a virtual machine. Textbooks usually call Python interpreted because there is no separate build step and no native executable. For exam questions, remember the practical effect: syntax errors are caught before the file starts running, but most other errors appear only at run time.
What is the difference between a syntax error and a logic error?
A syntax error breaks the language's grammar, such as a missing semicolon in Java or a missing colon in Python, so the code cannot be parsed or run. A logic error is valid code that runs but gives the wrong answer, such as < where <= was needed. Only testing reveals logic errors.
Which language should I use for NSCT programming practice?
HEC names the area "Programming (C++/Java/Python)" and has not said how questions are split between them. Pick the language you know best, then learn the main differences: semicolons and braces against indentation, compiled against interpreted execution, and static against dynamic typing. Our questions state the language in every code stem.
Next step
Continue with Data Types & Variables, the next topic in HEC's order, then Operators & Expressions. You can also browse all Programming MCQs or try a timed NSCT mock test.