Programming Fundamentals MCQs for NSCT

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

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

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

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

  1. Which translator converts an entire source program into machine code before the program is run?

    1. Interpreter
    2. Debugger
    3. Compiler
    4. Text editor
    Show answer

    Answer: C. Compiler

    A compiler translates the whole source program ahead of time into object or machine code; the program is then run as a separate step. An interpreter translates and executes statements one at a time while the program runs.

  2. How does a pure interpreter run a program?

    1. It produces a standalone executable file, then runs it
    2. It sends the source code directly to the CPU without translation
    3. It checks the syntax only and never executes the program
    4. It translates and executes the source one statement at a time
    Show answer

    Answer: D. It translates and executes the source one statement at a time

    An interpreter reads a statement, translates it and executes it before moving on, so no standalone executable is produced. This is why an interpreted program can run its first lines before reaching a later error that is only found at run time.

  3. What does the Java compiler (javac) produce from a .java source file?

    1. Native machine code for the current CPU in an .exe file
    2. Python bytecode in a .pyc file
    3. Assembly language that the programmer must assemble
    4. Bytecode in a .class file, run by the Java Virtual Machine
    Show answer

    Answer: D. Bytecode in a .class file, run by the Java Virtual Machine

    javac compiles Java source into platform-independent bytecode stored in .class files. The Java Virtual Machine (JVM) then interprets that bytecode and, in modern JVMs, also JIT-compiles hot code to native instructions.

  4. Which tool combines separately compiled object files and library code into one executable program?

    1. Linker
    2. Loader
    3. Preprocessor
    4. Assembler
    Show answer

    Answer: A. Linker

    The linker resolves references between object files and libraries (for example, a call to a function defined in another file) and writes the final executable. The loader later places that executable in memory to run it.

  5. In C++, which stage handles lines that begin with #, such as #include <iostream>?

    1. The linker, after compilation
    2. The loader, when the program starts
    3. The preprocessor, before compilation proper
    4. The CPU, while the program is running
    Show answer

    Answer: C. The preprocessor, before compilation proper

    Directives starting with # are handled by the preprocessor. #include pastes the named header into the source text, and #define performs macro substitution, all before the compiler translates the code.

  6. Which line is a single-line comment in Python?

    1. // total marks
    2. # total marks
    3. /* total marks */
    4. <!-- total marks -->
    Show answer

    Answer: B. # total marks

    Python single-line comments start with #. The // and /* */ forms are comments in C++, Java and JavaScript, and <!-- --> is an HTML comment.

  7. A program breaks a grammar rule of its language, such as a missing closing bracket. What kind of error is this?

    1. Logic error
    2. Runtime error
    3. Syntax error
    4. Linker error
    Show answer

    Answer: C. Syntax error

    A syntax error breaks the rules for how statements must be written, so the compiler or interpreter cannot parse the code. It is reported before the faulty code can run, unlike runtime errors (raised during execution) and logic errors (code runs but gives the wrong result).

  8. A program compiles and runs without crashing but prints the wrong total. What type of error is this?

    1. Syntax error
    2. Compile-time type error
    3. Logic error
    4. Linker error
    Show answer

    Answer: C. Logic error

    A logic error is a mistake in the program's reasoning, such as a wrong formula or loop bound. The code is valid and runs, so neither the compiler nor the runtime reports anything; only testing reveals it.

  9. Which method header does the JVM look for to start a standalone Java application?

    1. public static void main(String[] args)
    2. public void start()
    3. static int main()
    4. void run(String args)
    Show answer

    Answer: A. public static void main(String[] args)

    The JVM starts a Java application by calling public static void main(String[] args) in the class it is given. It is static so it can run without creating an object, and void because a Java main returns nothing.

  10. Which statement best describes how the standard Python implementation (CPython) runs a .py file?

    1. It compiles the source ahead of time into a native executable, like a C++ compiler
    2. It hands the .py text straight to the CPU, which understands Python
    3. It compiles the source to bytecode, which the Python virtual machine then interprets
    4. It requires a separate linker step before every run
    Show answer

    Answer: C. It compiles the source to bytecode, which the Python virtual machine then interprets

    CPython first compiles the whole source file to bytecode (cached as .pyc files for imported modules), then its virtual machine interprets that bytecode. So calling Python 'purely interpreted' is a simplification; the compile step is why a syntax error anywhere in the file stops the file before any line runs.

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.