All subjectsFundamentals · 14 questions · 9 junior / 5 mid / 0 senior

Programming Fundamentals quiz and interview questions

Programming fundamentals are where every developer starts, and they are usually taught in C++. Exams and first interviews love one thing: read a few lines and say what they print. This quiz trains exactly that — operators, loops, functions and the classic beginner bugs.

Programming Fundamentals quiz14 questions in the bank · 10 per round
Level
Question type
Mode

Programming Fundamentals: key concepts

Variables and types
int stores whole numbers, double stores decimals, char stores one character and bool stores true/false. Always initialize variables before using them.
Integer division and modulus
7 / 2 is 3 with integers (the decimal part is dropped). 7 % 2 is the remainder, 1.
Loops
for loops count, while loops repeat while a condition is true. continue skips to the next iteration; break exits the loop.
Functions
By default, arguments are passed by value (a copy). Use a reference parameter (int &x) when the function must change the caller's variable.
Assignment vs comparison
= assigns, == compares. if (x = 5) assigns 5 and is always true — a classic logic error.
Compiler
C++ is compiled: syntax errors stop the build, while logic errors compile fine and give wrong results at run time.

All 14 Programming Fundamentals questions with answers

Try each one first, then open the answer. Making a class test? Use the test maker.

  1. juniorPredict the output

    What does this print?

    int x = 10;
    x += 5;
    x = x % 4;
    cout << x;
    • A. 15
    • B. 3
    • C. 1
    • D. 0
    Show answer

    B. 3 — x becomes 15, and 15 % 4 is the remainder of 15 ÷ 4, which is 3.

  2. juniorPredict the output

    What does this print?

    for (int i = 0; i < 5; i += 2)
        cout << i << " ";
    • A. 0 1 2 3 4
    • B. 0 2 4
    • C. 2 4
    • D. 0 2 4 6
    Show answer

    B. 0 2 4 — i starts at 0 and increases by 2: 0, 2, 4. When i becomes 6, the condition i < 5 fails.

  3. juniorPredict the output

    What does this print?

    cout << 7 / 2 << " " << 7 % 2;
    • A. 3.5 1
    • B. 3 1
    • C. 3 0
    • D. 4 1
    Show answer

    B. 3 1 — Integer division drops the decimal (3), and % gives the remainder (1).

  4. juniorFind the bug

    x is 10, yet this always prints "five". Why?

    int x = 10;
    if (x = 5)
        cout << "five";
    • A. cout is wrong
    • B. = assigns 5 instead of comparing, so the condition is always true
    • C. Missing braces
    • D. x is not declared
    Show answer

    B. = assigns 5 instead of comparing, so the condition is always true — x = 5 assigns 5 and evaluates to 5, which is non-zero (true). Use == to compare.

  5. juniorFind the bug

    What kind of error does this code have?

    int a = 5
    cout << a;
    • A. Logic error
    • B. Syntax error — missing semicolon
    • C. Runtime error
    • D. No error
    Show answer

    B. Syntax error — missing semicolon — Every C++ statement must end with ;. The compiler stops with an error before the program runs.

  6. juniorPredict the output

    What does this print?

    int n = 3;
    while (n > 0) {
        cout << n;
        n--;
    }
    • A. 123
    • B. 321
    • C. 3210
    • D. 32
    Show answer

    B. 321 — It prints 3, 2, 1 and stops when n becomes 0.

  7. midPredict the output

    What does this print?

    int count = 0;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 4; j++)
            count++;
    cout << count;
    • A. 7
    • B. 12
    • C. 16
    • D. 9
    Show answer

    B. 12 — The inner loop runs 4 times for each of the 3 outer iterations: 3 × 4 = 12.

  8. midPredict the output

    What does this print?

    void change(int a) { a = 100; }
    
    int main() {
        int x = 5;
        change(x);
        cout << x;
    }
    • A. 100
    • B. 5
    • C. 0
    • D. Compile error
    Show answer

    B. 5 — x is passed by value, so the function changes its own copy. The original x stays 5.

  9. midPredict the output

    What does this print?

    void change(int &a) { a = 100; }
    
    int main() {
        int x = 5;
        change(x);
        cout << x;
    }
    • A. 100
    • B. 5
    • C. 0
    • D. Compile error
    Show answer

    A. 100 — int &a is a reference to the caller's variable, so assigning 100 changes x itself.

  10. midFind the bug

    What's wrong with this code that should print 15?

    int sum;
    for (int i = 1; i <= 5; i++)
        sum += i;
    cout << sum;
    • A. Loop runs 4 times
    • B. sum is never initialized, so the result is garbage
    • C. <= should be <
    • D. Nothing
    Show answer

    B. sum is never initialized, so the result is garbage — A local int starts with whatever was in memory. Write int sum = 0;

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp