C++ course menu
Beginner C++ Interview Questions and Answers
Common C++ interview questions for freshers with clear answers and runnable examples: C vs C++, references vs pointers, pass by value vs reference, const, struct vs class, integer division, overloading, and short coding questions.
On this page
- Concepts
- 1. What is the difference between C and C++?
- 2. What is the difference between a reference and a pointer?
- 3. What is the difference between pass by value and pass by reference?
- 4. What is the difference between a struct and a class?
- 5. What does const mean in different positions?
- 6. What does 7 / 2 give in C++? What about 7 % 2 and 7.0 / 2?
- 7. What is function overloading?
- 8. What is the difference between std::endl and '\n'?
- 9. What is undefined behaviour? Give examples.
- 10. What is a namespace, and why avoid using namespace std?
- Short coding questions
- 11. Reverse a string.
- 12. Check whether a number is prime.
- 13. Find the largest element of a vector.
- 14. Swap two numbers without a third variable.
- Tips for beginner interviews
These questions come up in nearly every entry-level C++ interview. For each one, try answering out loud first, then open the solution. A clear explanation with a tiny example impresses interviewers more than a memorised definition.
Concepts#
1. What is the difference between C and C++?#
Show solution
C++ began as an extension of C and can compile most C code, but adds a great deal:
2. What is the difference between a reference and a pointer?#
Show solution
#include <iostream>
int main() {
int x = 10;
int& r = x;
int* p = &x;
r += 5;
*p += 5;
std::cout << x << '\n';
}20Use a reference when something must always refer to a valid object; use a pointer when "nothing" is a valid option or you need to change what it points to.
3. What is the difference between pass by value and pass by reference?#
Show solution
Pass by value copies the argument, so the function cannot change the caller's variable. Pass by reference gives the function access to the original:
#include <iostream>
void by_value(int n) { n = 100; }
void by_reference(int& n) { n = 100; }
int main() {
int a = 1;
int b = 1;
by_value(a);
by_reference(b);
std::cout << a << ' ' << b << '\n';
}1 100A good follow-up point: pass large read-only objects by const reference (const std::string& s) to avoid copying them.
4. What is the difference between a struct and a class?#
Show solution
The only language difference is the default access: members (and base classes) of a struct are public by default, and those of a class are private by default. By convention, struct is used for plain data bundles and class for types with behaviour and invariants to protect.
5. What does const mean in different positions?#
Show solution
const int x = 5;: a variable that cannot change.void print(const std::string& s): the function will not modify the argument.int value() const;: a member function that does not modify the object.const int* p: a pointer to a value that cannot be changed throughp.int* const p: a pointer that cannot be re-pointed.
const documents intent and lets the compiler catch accidental changes.
6. What does 7 / 2 give in C++? What about 7 % 2 and 7.0 / 2?#
Show solution
#include <iostream>
int main() {
std::cout << 7 / 2 << ' ' << 7 % 2 << ' ' << 7.0 / 2 << ' ' << -7 / 2 << '\n';
}3 1 3.5 -3Dividing two integers gives an integer, truncated toward zero. If either operand is a floating-point number, the division is floating-point.
7. What is function overloading?#
Show solution
Several functions can share a name if their parameter lists differ in type or number. The compiler picks the best match at compile time:
#include <iostream>
#include <string>
int area(int side) { return side * side; }
int area(int w, int h) { return w * h; }
std::string area(const std::string& room) { return "area of " + room; }
int main() {
std::cout << area(4) << ' ' << area(3, 5) << ' ' << area("kitchen") << '\n';
}16 15 area of kitchenA different return type alone is not enough to overload.
8. What is the difference between std::endl and '\n'?#
Show solution
Both end the line. std::endl also flushes the output buffer, forcing the text to be written immediately. Flushing is slow when printing many lines, so '\n' is preferred unless you need the output to appear right away (for example before a crash or a long computation).
9. What is undefined behaviour? Give examples.#
Show solution
Undefined behaviour (UB) is code whose result the C++ standard does not define. The program may crash, give wrong results, or appear to work. Common examples:
- Reading an uninitialized variable.
- Accessing an array or vector out of bounds with
[]. - Dereferencing a null or dangling pointer.
- Signed integer overflow.
- Using an object after it has been deleted or moved-from in an unsupported way.
Compiler warnings (-Wall -Wextra) and sanitizers (-fsanitize=address,undefined) help find UB.
10. What is a namespace, and why avoid using namespace std?#
Show solution
A namespace groups names to prevent clashes; the standard library lives in std. using namespace std; imports hundreds of names into your scope, which can collide with your own (count, distance, size) and change which function a call refers to. Writing std:: explicitly, or importing single names (using std::cout;), avoids that. It must never appear in header files.
Short coding questions#
11. Reverse a string.#
Show solution
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s = "interview";
std::string r1(s.rbegin(), s.rend()); // using reverse iterators
std::string r2 = s; // manual two-pointer swap
for (std::size_t i = 0, j = r2.size(); i + 1 < j; ++i) {
--j;
std::swap(r2[i], r2[j]);
}
std::reverse(s.begin(), s.end()); // in place, with the algorithm
std::cout << r1 << ' ' << r2 << ' ' << s << '\n';
}weivretni weivretni weivretni12. Check whether a number is prime.#
Show solution
#include <iostream>
bool is_prime(int n) {
if (n < 2) return false;
for (int d = 2; d * d <= n; ++d) {
if (n % d == 0) return false;
}
return true;
}
int main() {
for (int n : {1, 2, 17, 21, 97}) {
std::cout << n << (is_prime(n) ? " prime" : " not prime") << '\n';
}
}1 not prime
2 prime
17 prime
21 not prime
97 primeChecking divisors only up to √n gives O(√n) time.
13. Find the largest element of a vector.#
Show hint
Track the best value so far, or use std::max_element.
Show solution
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {12, 45, 7, 89, 23};
int largest = v[0];
for (int x : v) {
if (x > largest) largest = x;
}
std::cout << largest << ' ' << *std::max_element(v.begin(), v.end()) << '\n';
}89 89Mention the empty-vector case: v[0] and *max_element are undefined on an empty vector, so check v.empty() first.
14. Swap two numbers without a third variable.#
Show solution
#include <iostream>
#include <utility>
int main() {
int a = 3;
int b = 8;
a = a + b; // 11
b = a - b; // 3
a = a - b; // 8
std::cout << a << ' ' << b << '\n';
std::swap(a, b); // what you should write in real code
std::cout << a << ' ' << b << '\n';
}8 3
3 8The arithmetic trick can overflow for large values; the XOR trick fails when both are the same variable. Interviewers like hearing that std::swap is the correct answer in practice.
Tips for beginner interviews#
- Say the time complexity of your solution without being asked.
- Mention edge cases: empty input, one element, negative numbers, overflow.
- Prefer standard library tools (
std::vector,std::string,std::sort) over C-style arrays and manual memory. - Next, try the Intermediate questions.
Finished this lesson?