C++ Basics Interview Questions and Answers
These are the questions interviewers ask to check your C++ fundamentals, typically in fresher interviews, campus placements and the first round for embedded, game and systems roles. They cover the topics of Phase 1 of the MakeMyCodes C++ tutorial: the language itself, installation and compilation, syntax, variables and data types. Each answer starts with what you would say in the interview, followed by a short explanation. Where the answer depends on behaviour, the output shown is from a real run with g++ 14.2.
Introduction to C++
1. What is C++?
Answer: C++ is a general-purpose, compiled, statically typed programming language created by Bjarne Stroustrup as an extension of C. It supports procedural, object-oriented and generic programming, and gives direct control over memory, which makes it suitable for high-performance software.
Typical uses are game engines, browsers, databases, operating-system components, embedded systems and financial trading systems.
2. What are the main differences between C and C++?
| C | C++ |
|---|---|
| Procedural only | Procedural, object-oriented and generic |
| No classes, inheritance or polymorphism | Classes, inheritance, polymorphism, encapsulation |
| No function overloading | Function and operator overloading |
malloc/free | new/delete, plus RAII and smart pointers |
| No references, no namespaces, no exceptions | References, namespaces, exceptions |
printf/scanf | std::cout/std::cin (type-safe streams) |
| Small standard library | Large Standard Template Library (STL) |
3. Is C++ a pure object-oriented language?
Answer: No. You can write a C++ program without any class: main() is a free function, and global functions and variables are allowed. C++ is multi-paradigm. By contrast, in Java (and traditionally in C#) every method must belong to a class.
4. What are the key features of C++?
Answer: Object-oriented programming (classes, inheritance, polymorphism, encapsulation, abstraction), templates and the STL, compile-time type checking, direct memory access through pointers, RAII for automatic resource management, exceptions, operator overloading, high performance with zero-overhead abstractions, and portability of source code across platforms.
5. What is the difference between C++ and Java (or C#)?
Answer: C++ compiles to native machine code for a specific platform and manages memory deterministically (manually or with RAII); Java and C# compile to bytecode/IL that runs on a virtual machine with a garbage collector. C++ supports multiple inheritance of classes, pointers and operator overloading freely; Java doesn't support operator overloading or multiple class inheritance.
Compilation and installation
6. Explain the compilation process of a C++ program.
Answer: Four stages: preprocessing (handles #include, #define, conditional compilation), compilation (checks syntax and types, translates to assembly), assembly (produces an object file, .o/.obj) and linking (combines object files and libraries into an executable).
A 7-line hello-world program grew to 33,392 lines after preprocessing with g++ (g++ -E), because <iostream> was pasted in. Details: C++ introduction.
7. What is the difference between a compiler error and a linker error?
Answer: A compiler error means the code itself is invalid: a syntax error, an undeclared name or a type mismatch. A linker error ("undefined reference to ...") means the code compiled, but the linker couldn't find the definition of something that was declared, such as a missing function body, a missing library, or a program without main.
8. Name some C++ compilers and IDEs.
Answer: Compilers: GCC (g++, also MinGW-w64 on Windows), Microsoft Visual C++ (MSVC, cl.exe) and Clang. IDEs and editors: Visual Studio, VS Code with the C/C++ extension, CLion, Code::Blocks and Xcode. See installing a C++ compiler.
Syntax
9. Why is main() required, and what does its return value mean?
Answer: main is the program's entry point, called by the operating system. It returns an int exit status: 0 means success, non-zero means failure. If the end of main is reached without a return, the compiler returns 0 automatically (this applies only to main).
10. What does #include do? What is the difference between #include <file> and #include "file"?
Answer: #include is a preprocessor directive that inserts the contents of a header file. Angle brackets search the system and standard-library include directories; quotes search the current project directory first, then the system paths. Use <> for standard headers and "" for your own.
11. What is a namespace, and why is std:: used?
Answer: A namespace groups names to avoid conflicts. The whole standard library is in namespace std, so std::cout means "cout from the standard library". :: is the scope resolution operator. Without the prefix (and without a using declaration), g++ reports:
12. Why is "using namespace std;" discouraged?
Answer: It imports every name from std into the current scope, which can cause name clashes and ambiguous calls, especially when placed in a header file that other files include. Prefer std:: or a specific using std::cout;.
13. What is the difference between std::endl and '\n'?
Answer: Both insert a newline. std::endl also flushes the output buffer, which is slower when done repeatedly. Use '\n' unless you need the output to appear immediately.
14. What is the difference between cin >> and getline()?
Answer: cin >> reads one whitespace-separated word; std::getline(std::cin, s) reads a whole line including spaces. With input "New Delhi", cin >> city stores only "New". After reading a number with >>, call cin.ignore() before getline, or it will read the leftover newline as an empty line.
Variables
15. What is the difference between declaration, definition and initialisation?
Answer: A declaration introduces a name and type (extern int x;). A definition also allocates storage (int x;). Initialisation gives the variable its first value when it's created (int x = 5;). Assignment changes the value later (x = 7;).
16. What are the ways to initialise a variable in C++? Which is preferred?
Answer: Copy (int a = 5;), direct (int a(5);) and brace/uniform initialisation (int a{5};, C++11). Brace initialisation is preferred because it rejects narrowing conversions:
With int copyInit = 9.99; the program compiles and silently stores 9.
17. What is the value of an uninitialised local variable?
Answer: Indeterminate. Reading it is undefined behaviour. In our test the same program printed 31933, then 31534, and 0 when compiled with -O2. Global and static variables are zero-initialised; local variables of built-in types are not. Always initialise (int count{}; gives 0).
18. What are the rules for naming variables in C++?
Answer: Use only letters, digits and underscores; the name can't start with a digit, can't be a keyword (int, class, new...) and can't contain spaces or symbols. Names are case sensitive. Avoid names beginning with an underscore and a capital letter, or containing __, because they're reserved.
19. What is the difference between local and global variables? What is shadowing?
Answer: A local variable is declared inside a block and exists only until the block ends; a global variable is declared outside all functions and is visible throughout the file. When a local variable has the same name as a global one, it shadows (hides) it; the global is still reachable as ::name.
20. What is the difference between const and constexpr?
Answer: Both make a variable read-only. const can be initialised with a run-time value; constexpr requires a value that can be computed at compile time, so it can be used where a compile-time constant is needed (array sizes, template arguments). Assigning to a const variable is a compile error: assignment of read-only variable 'maxUsers'.
21. What does the auto keyword do?
Answer: It tells the compiler to deduce the variable's type from its initialiser: auto x = 3; is an int, auto y = 3.5; is a double. The type is still static and fixed at compile time, and an auto variable must be initialised.
Data types
22. What are the fundamental data types in C++ and their typical sizes?
Answer: Typical sizes on a 64-bit system:
| Type | Typical size |
|---|---|
bool, char | 1 byte |
short | 2 bytes |
int, float | 4 bytes |
long | 8 bytes on 64-bit Linux/macOS, 4 bytes on 64-bit Windows |
long long, double | 8 bytes |
Sizes are implementation-defined; only sizeof(char) == 1 and the minimum ranges are guaranteed. Use <cstdint> types like std::int32_t when you need an exact size.
23. What is the difference between signed and unsigned integers? What happens on overflow?
Answer: Signed integers store negative and positive values; unsigned integers store only zero and positive values with twice the positive range. Unsigned overflow wraps around (well defined); signed overflow is undefined behaviour.
A related trap: -1 < 10u evaluates to false, because -1 is converted to unsigned. g++ warns: comparison of integer expressions of different signedness.
24. What is the difference between float and double? Why is 0.1 + 0.2 != 0.3?
Answer: float (usually 4 bytes) has about 7 significant digits; double (8 bytes) about 15-16. Both store values in binary, which can't represent most decimal fractions exactly:
Compare floating-point numbers with a tolerance, and store money as integers in the smallest unit.
25. What is the output of 7 / 2, and how do you get 3.5?
Answer: 7 / 2 is 3: when both operands are integers, C++ performs integer division and discards the fraction. Make one operand a floating-point value: 7 / 2.0 or static_cast<double>(7) / 2 gives 3.5. Similarly, static_cast<int>(4.89) is 4, because conversion to int truncates rather than rounds.
How to prepare
- Compile small programs for each answer yourself with
-Wall -Wextra; interviewers often ask "what is the output?". - Know the "trap" behaviours: integer division, uninitialised variables, unsigned wrap-around, floating-point comparison and
cinvsgetline. - Use modern style in written answers:
std::, brace initialisation,constexprandstatic_cast.