C++ CPA study materials
C++ CPA study resources

C++ CPA practice questions

One question at a time. Build your C++ CPA understanding.

First 50 of 190 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1C++ types, expressions, functions and control flow

Which header must be included to use std::cout for console output in standard C++?

  1. <iostream> Correct answer.
  2. <stdio.h>
  3. <cstdlib>
  4. <string>

Explanation

std::cout, std::cin, std::cerr, and std::clog are declared in <iostream>. <stdio.h> is the C header for printf/scanf, <cstdlib> exposes general utilities like std::exit, and <string> declares std::string but not the streams.

Question 2C++ types, expressions, functions and control flow

What does the following snippet print? #include <iostream> int main() { int x = 5; int y = 2; std::cout << x / y << " " << x % y; return 0; }

  1. 2 1 Correct answer.
  2. 2.5 1
  3. 2 0.5
  4. 3 1

Explanation

Both operands are int, so / performs integer division: 5 / 2 truncates toward zero to 2. The remainder operator % gives 5 % 2 = 1. The output is therefore '2 1' separated by a single space.

Question 3C++ types, expressions, functions and control flow

Which statement about C++ identifiers is TRUE?

  1. Identifiers can contain letters, digits, and underscores, but the first character cannot be a digit. Correct answer.
  2. Identifiers may begin with a digit if they contain at least one letter.
  3. Identifiers can contain spaces if surrounded by underscores.
  4. Identifiers are limited to a maximum length of eight characters.

Explanation

A valid C++ identifier is a sequence of letters, digits, and underscores that starts with a letter or an underscore - never a digit. There is no portable maximum length, identifiers cannot contain whitespace, and reserved words cannot be used as identifiers.

Question 4C++ types, expressions, functions and control flow

What does the following code print? #include <iostream> int main() { int a = 10; int b = 3; std::cout << a + b * 2; return 0; }

  1. 16 Correct answer.
  2. 26
  3. 13
  4. 20

Explanation

Multiplication has higher precedence than addition, so b * 2 evaluates first to 6, then a + 6 yields 16. Without parentheses, C++ never overrides standard arithmetic precedence.

Question 5C++ types, expressions, functions and control flow

Which of the following is the correct way to declare a constant integer in C++?

  1. const int MAX = 100; Correct answer.
  2. constant int MAX = 100;
  3. int const MAX == 100;
  4. #const int MAX 100

Explanation

The const qualifier is placed before (or after) the type to declare a read-only variable: const int MAX = 100;. C++ has no 'constant' keyword, == is comparison not assignment, and #const is not a valid preprocessor directive.

Question 6C++ types, expressions, functions and control flow

Which of these is NOT a fundamental built-in type in C++?

  1. string Correct answer.
  2. char
  3. double
  4. bool

Explanation

string is not a built-in fundamental type - std::string is a class template instantiation declared in <string>. char, double, and bool are fundamental types defined by the language itself.

Question 7C++ types, expressions, functions and control flow

What does this snippet print? #include <iostream> int main() { bool b = 5; std::cout << b << " " << !b; return 0; }

  1. 1 0 Correct answer.
  2. 5 0
  3. true false
  4. 0 1

Explanation

Any non-zero integer assigned to bool becomes true (stored as 1). Default stream output for bool prints integers, so true is shown as 1. !b negates true to false, which prints as 0. The result is '1 0'.

Question 8C++ types, expressions, functions and control flow

Which statement about a C++ literal like 3.14 is TRUE?

  1. It has type double by default. Correct answer.
  2. It has type float by default.
  3. It has type long double by default.
  4. Its type depends on the surrounding variable.

Explanation

An unsuffixed floating-point literal in C++ has type double. To get float you must add the suffix f or F (3.14f), and L produces long double (3.14L). The literal type is fixed by the literal itself, not by context.

Question 9C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int a = 7; int b = 4; std::cout << (a > b ? a : b); return 0; }

  1. 7 Correct answer.
  2. 4
  3. 1
  4. 0

Explanation

The conditional (ternary) operator evaluates a > b. Since 7 > 4 is true, the expression yields a, which is 7. The pattern (cond ? x : y) returns x when cond is true, y otherwise.

Question 10C++ types, expressions, functions and control flow

Which output does the following produce? #include <iostream> int main() { int x = 5; int y = 2; double z = static_cast<double>(x) / y; std::cout << z; return 0; }

  1. 2.5 Correct answer.
  2. 2
  3. 2.0
  4. 3

Explanation

static_cast<double>(x) converts x to 5.0. Dividing a double by an int promotes y to double too, so the division 5.0 / 2.0 yields 2.5. The default << for double prints 2.5 without trailing zeros.

Question 11C++ types, expressions, functions and control flow

What is the output of this snippet? #include <iostream> int main() { int x = 10; x += 5; x *= 2; std::cout << x; return 0; }

  1. 30 Correct answer.
  2. 25
  3. 20
  4. 15

Explanation

x += 5 makes x equal to 15. x *= 2 then doubles 15 to 30. Compound assignment operators apply their right-hand side to x in place: x op= y is equivalent to x = x op y.

Question 12C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { std::cout << (5 == 5) << " " << (5 != 5); return 0; }

  1. 1 0 Correct answer.
  2. true false
  3. 0 1
  4. 5 5

Explanation

Comparison operators in C++ produce bool: 5 == 5 is true, 5 != 5 is false. Streamed via the default << without std::boolalpha, true prints as 1 and false prints as 0, giving '1 0'.

Question 13C++ types, expressions, functions and control flow

Which of these is the correct C++ logical AND operator?

  1. && Correct answer.
  2. &
  3. and_eq
  4. ||

Explanation

&& is the logical AND operator that yields a bool result with short-circuit evaluation. & is the bitwise AND between integers, and_eq is the alternative spelling of &= (compound bitwise AND assignment), and || is logical OR.

Question 14C++ types, expressions, functions and control flow

What is the output of the following program? #include <iostream> int main() { int x = 5; int y = ++x + x++; std::cout << x << " " << y; return 0; }

  1. Undefined behavior - modifying x more than once between sequence points Correct answer.
  2. 7 12
  3. 7 13
  4. 6 11

Explanation

The two modifications of x are unsequenced relative to each other as operands of the addition in this expression, so the program has undefined behavior. C++17 did not make this arithmetic expression safe. Do not assign a guaranteed numeric output based on one observed compiler run.

Question 15C++ types, expressions, functions and control flow

How is a single-line comment written in C++?

  1. // this is a comment Correct answer.
  2. # this is a comment
  3. ** this is a comment **
  4. <!-- this is a comment -->

Explanation

C++ supports two comment forms: // for single-line comments and /* ... */ for block comments. # introduces preprocessor directives, ** is unrelated, and <!-- --> is HTML/XML comment syntax.

Question 16C++ types, expressions, functions and control flow

What is the output of the following code? #include <iostream> int main() { int a = 12; int b = 5; std::cout << (a & b) << " " << (a | b); return 0; }

  1. 4 13 Correct answer.
  2. 5 12
  3. 60 17
  4. 0 17

Explanation

12 in binary is 1100 and 5 is 0101. Bitwise AND combines bits set in both operands: 1100 & 0101 = 0100 = 4. Bitwise OR sets bits present in either: 1100 | 0101 = 1101 = 13. Output is '4 13'.

Question 17C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int x = 9; int y = 4; std::cout << x / y << " " << x % y; return 0; }

  1. 2 1 Correct answer.
  2. 2 0
  3. 2.25 1
  4. 9 4

Explanation

9 / 4 with integer division yields 2 (truncating the .25). 9 % 4 returns the remainder 1. The output is '2 1' separated by a space.

Question 18C++ types, expressions, functions and control flow

Which header declares std::vector?

  1. <vector> Correct answer.
  2. <array>
  3. <list>
  4. <iostream>

Explanation

std::vector is declared in <vector>. <array> defines std::array (fixed-size), <list> defines std::list (doubly linked list), and <iostream> defines stream objects but not vector.

Question 19C++ types, expressions, functions and control flow

What does this code print? #include <iostream> #include <string> int main() { std::string s = "Hello"; std::cout << s.length(); return 0; }

  1. 5 Correct answer.
  2. 6
  3. 4
  4. Hello

Explanation

std::string::length() returns the number of characters in the string. 'Hello' has five characters: H, e, l, l, o. Note that std::string does not include a null terminator in its length count.

Question 20C++ types, expressions, functions and control flow

What is the output? #include <iostream> #include <string> int main() { std::string s = "abc"; s += "def"; std::cout << s; return 0; }

  1. abcdef Correct answer.
  2. abc def
  3. def abc
  4. abc

Explanation

std::string overloads operator+= to append characters. After 'abc' += 'def', the string contains 'abcdef'. No automatic separator is inserted.

Question 21C++ types, expressions, functions and control flow

Assume C++17 with an ASCII-compatible execution character set. What does this code print? #include <iostream> int main() { char c = 'A'; std::cout << c << " " << static_cast<int>(c); return 0; }

  1. A 65 Correct answer.
  2. 65 A
  3. A A
  4. 65 65

Explanation

Streamed as char, 'A' prints as the letter A. Casting to int produces the ASCII code 65 for 'A'. The output is 'A 65'.

Question 22C++ types, expressions, functions and control flow

What does this print? #include <iostream> int main() { int a = 6; int b = 4; std::cout << (a > b && b > 0) << " " << (a < b || b == 4); return 0; }

  1. 1 1 Correct answer.
  2. 0 1
  3. 1 0
  4. true true

Explanation

a > b is true and b > 0 is true, so (a > b && b > 0) is true (1). a < b is false, but b == 4 is true, so (a < b || b == 4) is true (1) due to OR's short-circuit. Default cout shows true as 1.

Question 23C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int x = 8; std::cout << (x << 1) << " " << (x >> 1); return 0; }

  1. 16 4 Correct answer.
  2. 8 8
  3. 9 7
  4. 0 0

Explanation

<< and >> on integers are bit-shift operators. x << 1 shifts 8 (1000) left by one, producing 16 (10000). x >> 1 shifts right by one, producing 4 (100). The output is '16 4'.

Question 24C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int a = 3; int b = a++ + ++a; std::cout << a << " " << b; return 0; }

  1. Undefined behavior - a is modified more than once between sequence points Correct answer.
  2. 5 8
  3. 5 7
  4. 4 7

Explanation

The postfix and prefix modifications of a occur in unsequenced operands of the addition, which makes this expression undefined. C++17 does not impose the needed ordering for these operands. A plausible printed pair from one run cannot be treated as a portable defined answer.

Question 25C++ types, expressions, functions and control flow

What does the following loop print? #include <iostream> int main() { for (int i = 0; i < 4; i++) { std::cout << i; } return 0; }

  1. 0123 Correct answer.
  2. 1234
  3. 01234
  4. 0,1,2,3

Explanation

The for loop initializes i to 0, runs while i < 4, and increments i each iteration. The body prints i for i = 0, 1, 2, 3 - exiting before i becomes 4. With no separators, the output is '0123'.

Question 26C++ types, expressions, functions and control flow

Which keyword exits the closest enclosing loop or switch in C++ immediately?

  1. break Correct answer.
  2. continue
  3. exit
  4. return

Explanation

break terminates the innermost enclosing loop or switch and resumes execution at the statement after it. continue skips to the next iteration of the loop, exit() ends the entire program, and return leaves the current function entirely.

Question 27C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int n = 0; while (n < 3) { std::cout << n; n++; } return 0; }

  1. 012 Correct answer.
  2. 0123
  3. 123
  4. infinite loop

Explanation

n starts at 0; the loop executes while n < 3, printing n and incrementing it. Iterations print 0, 1, 2, then n becomes 3 and the test fails. There is no infinite loop because n is incremented inside the body.

Question 28C++ types, expressions, functions and control flow

What is the output of this snippet? #include <iostream> int main() { int i = 0; do { std::cout << i; i++; } while (i < 0); return 0; }

  1. 0 Correct answer.
  2. (no output)
  3. 01
  4. infinite loop

Explanation

A do-while loop executes its body at least once before checking the condition. The body prints 0 and increments i to 1; the test i < 0 is then false, so the loop exits. The output is just '0'.

Question 29C++ types, expressions, functions and control flow

What does this program print? #include <iostream> int main() { for (int i = 0; i < 5; i++) { if (i == 2) continue; std::cout << i; } return 0; }

  1. 0134 Correct answer.
  2. 01234
  3. 01
  4. 012

Explanation

continue skips the rest of the current iteration and proceeds to the loop's update step. When i == 2 the cout is skipped, but other iterations print 0, 1, 3, and 4. The result is '0134'.

Question 30C++ types, expressions, functions and control flow

What does this switch print when called with x = 2? #include <iostream> int main() { int x = 2; switch (x) { case 1: std::cout << "A"; case 2: std::cout << "B"; case 3: std::cout << "C"; default: std::cout << "D"; } return 0; }

  1. BCD Correct answer.
  2. B
  3. BC
  4. ABCD

Explanation

Without break, switch statements 'fall through' from one case to the next. Execution enters at case 2 (printing B), then continues through case 3 (C) and default (D), printing 'BCD'. To stop after case 2 you would need a break.

Question 31C++ types, expressions, functions and control flow

Which of the following statements about exception handling in C++ is TRUE?

  1. A try block can be followed by one or more catch blocks that handle different exception types. Correct answer.
  2. Every try block must be followed by exactly one catch block, no more.
  3. throw must always rethrow the same type as the function declared.
  4. An uncaught exception is silently ignored at runtime.

Explanation

C++ allows multiple catch blocks after a single try, each handling a different exception type. The first matching catch handles the exception. Uncaught exceptions cause std::terminate to abort the program - they are not ignored.

Question 32C++ types, expressions, functions and control flow

What does this program print? #include <iostream> #include <stdexcept> int main() { try { throw std::runtime_error("oops"); } catch (const std::runtime_error& e) { std::cout << "caught"; } catch (...) { std::cout << "generic"; } return 0; }

  1. caught Correct answer.
  2. generic
  3. caughtgeneric
  4. (no output)

Explanation

When throw is executed, the runtime searches matching catch blocks in order. The first catch handles std::runtime_error by reference, so 'caught' is printed and the exception is consumed. The catch (...) handler is only used when no earlier catch matches.

Question 33C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int i = 10; while (i > 0) { std::cout << i << " "; i -= 3; } return 0; }

  1. 10 7 4 1 Correct answer.
  2. 10 7 4
  3. 10 7 4 1 -2
  4. 10 9 8 7

Explanation

Starting from 10, the loop prints i and subtracts 3. Sequence: 10 (then i=7), 7 (then 4), 4 (then 1), 1 (then -2). When i = -2 the test i > 0 fails, so the loop ends. Output: '10 7 4 1 '.

Question 34C++ types, expressions, functions and control flow

What is the output of this snippet? #include <iostream> int main() { int i = 0; for (; i < 3; ) { std::cout << i; i++; } return 0; }

  1. 012 Correct answer.
  2. infinite loop
  3. compile error
  4. 0123

Explanation

All three for-loop expressions are optional. With no init or post-expression, the loop body itself increments i, exactly like a while loop. It prints 0, 1, 2 and exits when i == 3.

Question 35C++ types, expressions, functions and control flow

Which statement about a switch statement in C++ is FALSE?

  1. The switch expression can be of type double. Correct answer.
  2. case labels must be compile-time constant integral or enumeration values.
  3. The default label is optional.
  4. Without break, execution falls through to the next case.

Explanation

A switch expression in C++ must be of integral or enumeration type - double is not allowed. case labels must be constant integral expressions, default is optional, and missing break causes fall-through.

Question 36C++ types, expressions, functions and control flow

What does this print? #include <iostream> int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { std::cout << i * j << " "; } } return 0; }

  1. 1 2 2 4 3 6 Correct answer.
  2. 1 2 3 4 5 6
  3. 1 2 4 6
  4. 2 4 6

Explanation

Outer i takes values 1, 2, 3; inner j takes 1, 2. Products: i=1 -> 1,2; i=2 -> 2,4; i=3 -> 3,6. With trailing spaces, the full output is '1 2 2 4 3 6 '.

Question 37C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int x = 5; if (x > 0) if (x > 10) std::cout << "A"; else std::cout << "B"; return 0; }

  1. B Correct answer.
  2. A
  3. (no output)
  4. AB

Explanation

The dangling-else rule pairs the else with the nearest unmatched if - that is, with 'if (x > 10)'. Since x = 5 is not > 10, the else branch runs and prints 'B'. The outer if (x > 0) succeeds, so the whole inner construct executes.

Question 38C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int n = 1; while (n < 10) { n *= 2; } std::cout << n; return 0; }

  1. 16 Correct answer.
  2. 8
  3. 10
  4. 32

Explanation

n doubles each iteration: 1, 2, 4, 8, 16. After 16, the test 16 < 10 is false and the loop ends. The final value is 16.

Question 39C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int x = 1; if (x = 5) std::cout << "yes"; else std::cout << "no"; return 0; }

  1. yes Correct answer.
  2. no
  3. (no output)
  4. compile error

Explanation

Note the single = inside the condition: x = 5 is assignment, not comparison. The expression value is the assigned value 5, which is non-zero and thus truthy. So 'yes' prints. This is a classic bug - using == is the comparison.

Question 40C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int n = 0; for (int i = 1; i <= 5; i++) n += i; std::cout << n; return 0; }

  1. 15 Correct answer.
  2. 10
  3. 20
  4. 5

Explanation

The loop accumulates 1 + 2 + 3 + 4 + 5 = 15 into n. The closed form n*(n+1)/2 with n=5 gives 5*6/2 = 15.

Question 41C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int x = 0; try { x = 1; throw 42; x = 2; } catch (int e) { x += e; } std::cout << x; return 0; }

  1. 43 Correct answer.
  2. 42
  3. 2
  4. 44

Explanation

x is set to 1, then 'throw 42' immediately exits the try (so 'x = 2' is skipped). The catch binds e = 42 and runs x += 42, making x = 43.

Question 42C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { int n = 17; if (n % 2 == 0) std::cout << "even"; else std::cout << "odd"; return 0; }

  1. odd Correct answer.
  2. even
  3. (no output)
  4. compile error

Explanation

17 % 2 is 1, which is not equal to 0. The if condition is false, so the else branch runs and 'odd' is printed. The modulus operator returns the remainder of integer division.

Question 43C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int main() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; if (i + j > 3) break; std::cout << i << j << " "; } } return 0; }

  1. 01 02 10 12 20 21 Correct answer.
  2. 01 02 10 12 20
  3. 00 11 22
  4. 01 02 10 12 21

Explanation

For each (i, j): when i == j, the continue skips printing. The break fires only when i + j > 3 - for i = 0, j = 0..2 max sum is 2, no break. For i = 1, j = 0..2 max sum is 3, no break. For i = 2, j = 0..2 max sum is 4 (when j = 2), but j = 2 sums to 4 only when i = 2 - and 2 + 2 = 4, but i == j there so continue fires first. So break never actually fires. Output: i=0: skip j=0, print 01, 02; i=1: print 10, skip j=1, print 12; i=2: print 20, 21, skip j=2. Result: '01 02 10 12 20 21 '.

Question 44C++ types, expressions, functions and control flow

What is the output of the following code? #include <iostream> void greet() { std::cout << "Hello"; } int main() { greet(); greet(); return 0; }

  1. HelloHello Correct answer.
  2. Hello
  3. HelloHelloHello
  4. (no output)

Explanation

greet() prints 'Hello' once per call. Calling greet() twice in main produces 'HelloHello' with no separator because the function does not output a newline or space.

Question 45C++ types, expressions, functions and control flow

What does this code print? #include <iostream> int add(int a, int b = 10) { return a + b; } int main() { std::cout << add(5) << " " << add(5, 20); return 0; }

  1. 15 25 Correct answer.
  2. 5 25
  3. 10 25
  4. compile error

Explanation

add has a default argument b = 10. add(5) uses b = 10, so 5 + 10 = 15. add(5, 20) supplies b = 20 explicitly, giving 5 + 20 = 25. Output is '15 25'.

Question 46C++ types, expressions, functions and control flow

Which mechanism allows two functions to share a name in the same scope but accept different parameter types?

  1. Function overloading Correct answer.
  2. Function overriding
  3. Function templates
  4. Function pointers

Explanation

Function overloading lets multiple functions share a name as long as their parameter lists differ in number or types. Overriding is the OOP mechanism for replacing a virtual function in a derived class. Templates and function pointers are different features.

Question 47C++ types, expressions, functions and control flow

What does this code print? #include <iostream> void modify(int x) { x = 100; } int main() { int a = 5; modify(a); std::cout << a; return 0; }

  1. 5 Correct answer.
  2. 100
  3. 0
  4. compile error

Explanation

modify takes its parameter x by value, so x is a copy of a. Changing x has no effect on a in main. The print therefore shows the original value 5. To mutate a you would need a reference (int&) or pointer parameter.

Question 48C++ types, expressions, functions and control flow

What does this snippet print? #include <iostream> void modify(int& x) { x = 100; } int main() { int a = 5; modify(a); std::cout << a; return 0; }

  1. 100 Correct answer.
  2. 5
  3. 0
  4. compile error

Explanation

Here x is declared int&, a reference parameter. Inside modify, x is just another name for a. Assigning 100 to x changes a in main. The output is '100'. References give the function direct access to the caller's variable.

Question 49C++ types, expressions, functions and control flow

What does this recursive function print when called as factorial(4)? #include <iostream> int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } int main() { std::cout << factorial(4); return 0; }

  1. 24 Correct answer.
  2. 10
  3. 12
  4. 16

Explanation

factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1) = 4 * 3 * 2 * 1 = 24. The recursion bottoms out when n <= 1 returns 1.

Question 50C++ types, expressions, functions and control flow

Which function signature pair is a valid example of overloading?

  1. int f(int x); and int f(double x); Correct answer.
  2. int f(int x); and double f(int x);
  3. int f(int x); and int f(int y);
  4. int f(int x); and int f(int x) const;

Explanation

Overloading requires functions to differ in the number or types of their parameters. f(int) vs f(double) differ in parameter type - valid. Differing only in return type or in parameter name does not produce a valid overload, and 'const' applies only to member functions, not free ones.