C++ CPE study materials
C++ CPE study resources

C++ CPE practice questions

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

First 50 of 200 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++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which C++ header must be included to use std::cout and std::cin?

  1. <string>
  2. <fstream>
  3. <iostream> Correct answer.
  4. <cstdio>

Explanation

The standard streams std::cout, std::cin, std::cerr, and std::clog are declared in the <iostream> header, so it must be included before using them. <cstdio> provides C-style I/O functions such as printf, not the stream objects.

Question 2C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the value of x after this code runs? int x = 7; x %= 4;

  1. 1.75
  2. 3 Correct answer.
  3. 1
  4. 28

Explanation

The compound assignment x %= 4 is equivalent to x = x % 4. Integer remainder of 7 divided by 4 is 3, because 4 fits once into 7 with 3 left over.

Question 3C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Given int a[5]; which expression refers to the last element of the array?

  1. a[0]
  2. a[5]
  3. a[last]
  4. a[4] Correct answer.

Explanation

An array of 5 elements has valid indices 0 through 4, so the final element is a[4]. Accessing a[5] would read past the end of the array.

Question 4C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which declaration correctly creates a function that takes two int parameters and returns nothing?

  1. sum void(int a, int b);
  2. int sum(void a, void b);
  3. void sum(int a, int b); Correct answer.
  4. void sum(int a; int b);

Explanation

A function returning no value uses the void return type, and parameters are separated by commas inside parentheses. The form void sum(int a, int b); follows that syntax exactly.

Question 5C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does the following loop print? for (int i = 0; i < 3; ++i) std::cout << i;

  1. 012 Correct answer.
  2. 0123
  3. 123
  4. 0 1 2

Explanation

The loop starts at i = 0 and runs while i < 3, so it executes for i equal to 0, 1, and 2. No separator is printed, so the output is the concatenation 012.

Question 6C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which type is most appropriate for storing a single character such as 'A' in C++?

  1. bool
  2. int
  3. char Correct answer.
  4. std::string

Explanation

The char type is designed to hold a single character value, and character literals like 'A' have type char. Other types either hold numbers, logical values, or whole text sequences.

Question 7C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the value of the expression 5 / 2 when both operands are int?

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

Explanation

When both operands of / are integers, C++ performs integer division and discards the fractional part. Five divided by two is 2.5, truncated to 2.

Question 8C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about a variable declared inside a for loop body such as for (int i = 0; ...) is correct?

  1. i must be declared static to compile.
  2. i is visible only within the loop's scope. Correct answer.
  3. i is visible for the rest of the enclosing function.
  4. i is visible in every function of the file.

Explanation

A variable declared in the initialization part of a for statement has scope limited to the loop statement itself. Once the loop ends, the name is no longer accessible.

Question 9C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is printed? int n = 5; while (n > 0) { std::cout << n; n -= 2; }

  1. 5310
  2. 54321
  3. 531-1
  4. 531 Correct answer.

Explanation

The loop prints the current n and then subtracts 2, so it prints 5, then 3, then 1. After printing 1 the value becomes -1, which fails the condition n > 0, so the loop ends.

Question 10C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement correctly passes an argument by reference so the function can modify the caller's variable?

  1. void bump(int* x);
  2. void bump(const int x);
  3. void bump(int x);
  4. void bump(int& x); Correct answer.

Explanation

An ampersand after the parameter type declares a reference parameter, which binds directly to the caller's variable so assignments inside the function affect it. A plain int parameter receives a copy.

Question 11C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the output of this code? std::string s = "code"; std::cout << s.size() << s[1];

  1. 4o Correct answer.
  2. 4c
  3. 41
  4. 3o

Explanation

s.size() returns the number of characters, which is 4. Indexing starts at 0, so s[1] is the second character 'o', giving the output 4o.

Question 12C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which line correctly declares a two-dimensional array of 3 rows and 4 columns of int?

  1. int grid[3,4];
  2. int grid[3][4][1];
  3. int grid[3][4]; Correct answer.
  4. int grid[4][3];

Explanation

A two-dimensional array is declared with one bracket pair per dimension, rows first. int grid[3][4]; creates 3 rows of 4 ints each.

Question 13C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What value does result hold after this code? int result = 0; for (int i = 1; i <= 4; ++i) result += i * i;

  1. 30 Correct answer.
  2. 16
  3. 20
  4. 10

Explanation

The loop adds the squares 1, 4, 9, and 16 for i from 1 through 4. Their sum is 30.

Question 14C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which expression correctly compares two std::string objects a and b for equality of content?

  1. a = b
  2. a.compare(b)
  3. a.length() == b
  4. a == b Correct answer.

Explanation

The == operator is overloaded for std::string and compares the character content of the two strings. It returns a bool that is true when the contents match exactly.

Question 15C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the difference between break and continue inside a loop?

  1. break skips one iteration; continue exits the loop entirely.
  2. Both exit the loop, but break also returns a value.
  3. break works only in for loops; continue works only in while loops.
  4. break exits the loop entirely; continue skips to the next iteration. Correct answer.

Explanation

The break statement terminates the innermost enclosing loop immediately. The continue statement abandons the rest of the current iteration and proceeds to the loop's next iteration or condition check.

Question 16C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Given int* p = nullptr; what happens if the program evaluates *p?

  1. The behavior is undefined and typically crashes. Correct answer.
  2. It evaluates to 0 safely.
  3. It allocates a new int automatically.
  4. It throws a C++ exception that can be caught.

Explanation

Dereferencing a null pointer is undefined behavior in C++; there is no guarantee of any particular result. In practice it usually causes a segmentation fault.

Question 17C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is printed? int x = 3; int y = ++x + x++; std::cout << y;

  1. 9
  2. 7
  3. The result is undefined because x is modified twice without a sequencing point. Correct answer.
  4. 8

Explanation

The expression modifies x more than once between sequence points, which is undefined behavior in C++ before C++17 rules on such expressions. Any numeric answer would be an unjustified assumption about evaluation order.

Question 18C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about the default access of members in a class declared with the class keyword is correct?

  1. Members are public by default.
  2. Members are protected by default.
  3. Access defaults depend on the compiler used.
  4. Members are private by default. Correct answer.

Explanation

In a class, members declared before any access specifier are private by default. The struct keyword differs in that its members default to public.

Question 19C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does the following code print? int a = 10; int& r = a; r = 20; std::cout << a;

  1. 20 Correct answer.
  2. 10
  3. 0
  4. A compile error occurs.

Explanation

The declaration int& r = a; makes r an alias for a rather than a copy. Assigning 20 through r therefore modifies a itself, so printing a shows 20.

Question 20C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the value of the expression true && false || true?

  1. Undefined
  2. true Correct answer.
  3. false
  4. 1

Explanation

The && operator has higher precedence than ||, so the expression groups as (true && false) || true. The left part is false, and false || true evaluates to true.

Question 21C++ Essentials 1 (CP101 / CPPE1) aligned objectives

A function is declared int f(int n); and its body calls f(n - 1) without any base case. What is the primary consequence?

  1. Recursion continues until the call stack overflows. Correct answer.
  2. The recursion is converted into a loop by the compiler.
  3. The function returns 0 automatically.
  4. The compiler rejects the program at compile time.

Explanation

A recursive function without a terminating base case keeps making calls, each consuming stack space. Eventually the stack is exhausted and the program terminates abnormally.

Question 22C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does this code print? int arr[3] = {1, 2, 3}; int* p = arr; std::cout << *(p + 2);

  1. 1
  2. 3 Correct answer.
  3. The address of arr[2]
  4. 2

Explanation

An array name decays to a pointer to its first element, so p points at arr[0]. Adding 2 moves the pointer to arr[2], and dereferencing yields 3.

Question 23C++ Essentials 1 (CP101 / CPPE1) aligned objectives

In which order are the parts of a for statement executed for the loop for (int i = 0; i < n; ++i) body;?

  1. Initialization, increment, condition, body repeatedly.
  2. Initialization, body, condition, increment repeatedly.
  3. Condition, body, initialization, increment repeatedly.
  4. Initialization once, then condition, body, increment repeatedly. Correct answer.

Explanation

A for statement first runs the initialization once. It then tests the condition, executes the body if true, performs the increment, and repeats from the condition check.

Question 24C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Consider a function void swap(int a, int b) that exchanges a and b inside its body. What happens to the caller's variables?

  1. They are swapped only if the function is declared inline.
  2. They are swapped because parameters alias the arguments.
  3. They are swapped only if they are global variables.
  4. They are unchanged because the parameters are copies. Correct answer.

Explanation

Passing by value creates independent copies for the parameters, so swapping the copies leaves the caller's variables untouched. Reference or pointer parameters would be required to affect the originals.

Question 25C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the output? int total = 0; for (int i = 0; i < 5; ++i) { if (i == 3) continue; total += i; } std::cout << total;

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

Explanation

The loop adds 0, 1, 2, and 4, skipping 3 because of the continue statement. The sum is 7.

Question 26C++ Essentials 1 (CP101 / CPPE1) aligned objectives

In C++, which header must be included to use std::cout and std::endl?

  1. <stdio>
  2. <iostream> Correct answer.
  3. <console>
  4. <ostream>

Explanation

The standard streams std::cout, std::cin and the manipulator std::endl are declared in the <iostream> header, so a program using them must include it. Other headers such as <ostream> only declare output stream types without the predefined stream objects.

Question 27C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which declaration creates a named constant whose value cannot be changed after initialization?

  1. int const_value = 10;
  2. static int limit = 10;
  3. mutable int limit = 10;
  4. const int limit = 10; Correct answer.

Explanation

Placing const before the type makes the variable read-only; any later assignment to it is a compile error. The identifier in the other options is just an ordinary variable name, since const_value is a single identifier and static and mutable do not imply immutability for a plain int.

Question 28C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the value of the expression 7 % 3 + 7 / 3 when evaluated with int operands?

  1. 4
  2. 2
  3. 1
  4. 3 Correct answer.

Explanation

The remainder 7 % 3 is 1 and integer division 7 / 3 truncates to 2, so the sum is 3. Both operators have the same precedence and are evaluated left to right, giving the same result either way.

Question 29C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Given double d = 3.9; which expression yields the integer part 3 without using a library function?

  1. static_cast<int>(d) Correct answer.
  2. d % 1
  3. int(d + 0.5)
  4. round(d)

Explanation

A static_cast from double to int performs truncation toward zero, so 3.9 becomes 3. Adding 0.5 first would round to 4, the % operator is not defined for floating-point operands, and round is a library function.

Question 30C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about short-circuit evaluation of the expression a && b is correct?

  1. b is evaluated before a because it is on the right.
  2. If a evaluates to true, b is skipped.
  3. Both a and b are always evaluated exactly once.
  4. If a evaluates to false, b is not evaluated at all. Correct answer.

Explanation

The built-in && operator evaluates its left operand first and, when that operand is false, the result is already known to be false so the right operand is not evaluated. This short-circuiting is guaranteed by the language and is commonly used to guard a dereference or division.

Question 31C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does this code print? int x = 0; if (x = 5) std::cout << "yes"; else std::cout << "no";

  1. 5
  2. nothing
  3. yes Correct answer.
  4. no

Explanation

The condition uses assignment, not comparison: x = 5 stores 5 in x and the value of the assignment expression is 5, which converts to true. The if branch therefore runs and prints yes.

Question 32C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about a switch statement over an int is correct?

  1. A switch requires a default label to compile.
  2. Falling through from one case to the next is a compile error.
  3. Case labels may overlap and the first matching one wins.
  4. Each case label must be a compile-time constant expression. Correct answer.

Explanation

Case labels in a switch must be integral constant expressions that are distinct from one another, which lets the compiler build a jump table. Falling through is legal and simply continues execution into the next label, and a default label is optional.

Question 33C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which declaration correctly defines a function that takes no parameters and returns nothing?

  1. void reset { }
  2. void reset() { } Correct answer.
  3. reset() -> void { }
  4. void reset(void x) { }

Explanation

A function returning no value has return type void and an empty parameter list written as (). The body is enclosed in braces, and no return statement is needed for void functions.

Question 34C++ Essentials 1 (CP101 / CPPE1) aligned objectives

A function is declared void grow(int& n) { ++n; } and called as int v = 4; grow(v); What is the value of v afterwards?

  1. 0
  2. 5 Correct answer.
  3. 4
  4. The program does not compile.

Explanation

The parameter is a reference to int, so n is another name for the caller's variable v. Incrementing n therefore increments v, leaving it at 5.

Question 35C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about overloading the function int area(int) with int area(int, int) is correct?

  1. The second declaration hides the first one entirely.
  2. They are legal only if one is declared static.
  3. The compiler rejects them because the return type is the same.
  4. The two functions may coexist because their parameter lists differ. Correct answer.

Explanation

Overload resolution distinguishes functions by their parameter lists, so area(int) and area(int, int) are distinct overloads. Return type does not participate in overload resolution, but that is not a problem here because the parameter lists already differ.

Question 36C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does this code print? int a[4] = {10, 20, 30, 40}; std::cout << a[4 - 2] + a[1];

  1. 50 Correct answer.
  2. It is undefined behavior.
  3. 40
  4. 30

Explanation

The index 4 - 2 is 2, so a[2] is 30, and a[1] is 20; their sum is 50. Both indices are within the valid range 0 to 3, so the access is well defined.

Question 37C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Given std::string s = "hello"; which expression returns the number of characters stored in s?

  1. s.max_size()
  2. s.capacity()
  3. sizeof(s)
  4. s.length() Correct answer.

Explanation

length() (and its synonym size()) returns the number of characters currently in the string, which is 5 here. sizeof yields the size of the string object itself and capacity reports allocated storage, not the character count.

Question 38C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is printed? std::string s = "abc"; s += s; std::cout << s;

  1. abcabcabc
  2. aabbcc
  3. abc
  4. abcabc Correct answer.

Explanation

The += operator with a string argument appends a copy of that argument, so s becomes "abc" followed by "abc". The result is the six-character string abcabc.

Question 39C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which loop is guaranteed to execute its body at least once before the condition is tested?

  1. for (int i = 0; i < cond; ++i) { body }
  2. while (cond) { body }
  3. for (; cond; ) { body }
  4. do { body } while (cond); Correct answer.

Explanation

A do-while loop evaluates the body first and checks the condition only afterwards, so the body always runs at least once. The other loops test their condition before the first iteration and may skip the body entirely.

Question 40C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the final value of sum? int sum = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 2; ++j) sum += i;

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

Explanation

The inner loop runs twice for each outer value, so i = 0 adds 0 twice, i = 1 adds 1 twice, and i = 2 adds 2 twice. The total is 0 + 2 + 4 = 6.

Question 41C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about a std::vector<int> v is correct?

  1. The size of a vector is fixed once it is created.
  2. v.size() returns the number of elements currently stored. Correct answer.
  3. Elements can only be added at the front.
  4. v[0] is valid even after default construction with no elements.

Explanation

A vector manages a dynamically sized array, and size() reports how many elements it currently holds. Elements are added at the back with push_back, and indexing an empty vector is undefined behavior.

Question 42C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does this code print? int n = 10; int* p = &n; *p = *p + 5; std::cout << n;

  1. 10
  2. 5
  3. 15 Correct answer.
  4. The address of n

Explanation

p stores the address of n, so *p names the same object as n. Assigning *p + 5 back through the pointer stores 15 into n, and printing n shows 15.

Question 43C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is printed? int x = 5; int* p = &x; int* q = p; *q = 7; std::cout << x;

  1. The address of x
  2. 7 Correct answer.
  3. 5
  4. 0

Explanation

Copying a pointer copies the address it holds, so p and q both point to x. Writing through q therefore modifies x, and the output is 7.

Question 44C++ Essentials 1 (CP101 / CPPE1) aligned objectives

A class has a private data member int count; and a public member function int getCount() const; Which statement is correct?

  1. getCount must be declared static to read count.
  2. getCount may read count but must not modify it. Correct answer.
  3. getCount may modify count because it is a member.
  4. getCount cannot access count because it is private.

Explanation

A const member function promises not to modify the object, so it may read members but cannot assign to them. Private access applies to non-members, not to the class's own member functions.

Question 45C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is printed? struct Point { int x; int y; }; Point p{1, 2}; Point q = p; q.x = 9; std::cout << p.x;

  1. The program does not compile.
  2. 2
  3. 1 Correct answer.
  4. 9

Explanation

Copying a struct copies each member, so q is an independent object holding its own x and y. Changing q.x leaves p.x unchanged at 1.

Question 46C++ Essentials 1 (CP101 / CPPE1) aligned objectives

For a class declared with the class keyword, what is the default access of its members?

  1. It depends on whether the class has a constructor.
  2. public
  3. protected
  4. private Correct answer.

Explanation

Members of a class are private by default, whereas members of a struct are public by default. Access specifiers such as public: can be used to change the access of subsequent members.

Question 47C++ Essentials 1 (CP101 / CPPE1) aligned objectives

A function takes a std::string by value and the caller passes a large string. What is the main consequence compared with taking it by const reference?

  1. The argument is automatically moved out of the caller.
  2. The function cannot read the string's characters.
  3. The caller's string is modified inside the function.
  4. A copy of the string is made, which can be expensive. Correct answer.

Explanation

Passing by value constructs a new string object from the argument, which copies the character data and may allocate memory. Passing by const reference avoids the copy while still allowing read-only access.

Question 48C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What is the value of x after this code? int x = 1; { int x = 2; x += 3; } x += 10;

  1. 15
  2. 11 Correct answer.
  3. 6
  4. 2

Explanation

The inner block declares a new x that shadows the outer one; the inner x becomes 5 and then goes out of scope. The outer x was never touched inside the block, so x += 10 leaves it at 11.

Question 49C++ Essentials 1 (CP101 / CPPE1) aligned objectives

What does this code print? int n = 3; int result = n > 2 ? n * 10 : n * 100; std::cout << result;

  1. 3
  2. 300
  3. 1
  4. 30 Correct answer.

Explanation

The conditional operator evaluates the condition n > 2, which is true, so it yields the second operand n * 10. With n equal to 3 that expression is 30.

Question 50C++ Essentials 1 (CP101 / CPPE1) aligned objectives

Which statement about the null terminator in a C-style string literal is correct?

  1. The terminating character is a space.
  2. The literal "hi" occupies three char elements including the terminating '\0'. Correct answer.
  3. The terminator is stored only when the literal is assigned to std::string.
  4. The literal "hi" occupies two char elements with no terminator.

Explanation

A string literal is an array of char that includes an implicit terminating null character '\0' after the visible characters. Thus "hi" has the elements 'h', 'i' and '\0', for a total of three.