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: 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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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;
Which type is best for storing 3.75?
- A. int
- B. char
- C. double
- D. bool
Show answer
C. double — double stores decimal numbers. Storing 3.75 in an int would keep only 3.
What does this print?
for (int i = 1; i <= 5; i++) { if (i == 3) continue; if (i == 5) break; cout << i; }- A. 12345
- B. 124
- C. 1245
- D. 12
Show answer
B. 124 — i = 3 is skipped by continue, and the loop exits at i = 5 before printing. Output: 1, 2, 4.
What does a compiler do?
- A. Runs code line by line
- B. Translates the whole source code into machine code before it runs
- C. Finds logic errors
- D. Formats code
Show answer
B. Translates the whole source code into machine code before it runs — C++ is compiled into an executable first. An interpreter (like Python's) executes code directly, statement by statement.
What does this print?
char c = 'A'; c = c + 2; cout << c;- A. A2
- B. 67
- C. C
- D. B
Show answer
C. C — Characters are stored as numbers ('A' is 65). 65 + 2 = 67, which is 'C', and cout prints a char as a character.
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.