Interview Q&A · C++ · 10 questions in this topic
C++ Fundamentals — C++ Interview Q&A
The vocabulary every C++ interview opens with — what the language is, how source becomes an executable, namespaces, I/O and the basic type system. Part of C++ Fundamentals · 213 questions in the C++ bank.
What is C++?
Answer
C++ is a general-purpose, statically typed, compiled programming language that extends C with object-oriented programming, generic programming (templates) and a rich standard library. It gives low-level control over memory and performance while also supporting high-level abstractions with zero or minimal runtime overhead.
It was created by Bjarne Stroustrup (first released in 1985) and is standardized by ISO — C++11, C++14, C++17, C++20 and C++23 are the major modern revisions. Typical uses are systems software, game engines, browsers, databases, embedded systems and performance-critical backends.
Example
#include <iostream> int main() { std::cout << "Hello, interviewer!\n"; return 0; }
💡 Name one low-level strength (deterministic memory control, no garbage collector) and one high-level strength (classes, templates, STL) — that frames every follow-up answer.
How does a C++ program go from source code to a running program?
Answer
Roughly four stages: preprocessing (handles #include, #define, conditional compilation), compilation (each translation unit is compiled to object code), linking (object files plus libraries are combined into an executable), and loading/execution (the OS loader maps the executable into memory and starts main).
Most interview bugs live at a specific stage: a missing header is preprocessing, a type error is compilation, an undefined reference is linking, and a segfault is runtime.
Key points
- 1Preprocessing — expands #include files and macros into a translation unit.
- 2Compilation — each .cpp compiles independently to a .o object file.
- 3Linking — resolves symbols across object files and libraries into one binary.
- 4Loading — the OS loads the binary; execution starts at main().
Example
// a.cpp int add(int a, int b) { return a + b; } // main.cpp int add(int a, int b); // declaration int main() { return add(2, 3); } // build: g++ -c a.cpp; g++ -c main.cpp; g++ a.o main.o -o app
💡 Saying undefined reference is a linker error (not a compiler error) is a classic signal you have actually built multi-file C++ programs.
What happens when you run `int main()` and what can main return?
Answer
main is the program entry point. The hosted-environment signatures are int main() and int main(int argc, char* argv[]). Returning 0 (or falling off the end of main, which implicitly returns 0) means success; returning non-zero signals failure to the operating system.
argc is the argument count including the program name, and argv holds the argument strings with argv[argc] guaranteed to be nullptr.
Example
#include <iostream> int main(int argc, char* argv[]) { std::cout << "args: " << argc << "\n"; for (int i = 0; i < argc; ++i) std::cout << argv[i] << "\n"; return 0; }
💡 Mention that falling off main returns 0 implicitly — it is the only function where that is legal, and interviewers sometimes probe it.
What are the stages of compilation in detail (preprocessor, compiler, assembler, linker)?
Answer
The preprocessor handles directives starting with #: file inclusion, macro expansion and conditional compilation. The compiler proper translates the preprocessed C++ into assembly. The assembler turns assembly into machine-code object files. The linker merges object files and libraries, resolving external symbols into the final executable or shared library.
Key points
- 1Preprocessor — textual transformation: #include, #define, #ifdef.
- 2Compiler — semantic analysis, optimization, assembly output.
- 3Assembler — assembly to relocatable machine code (.o/.obj).
- 4Linker — symbol resolution, relocation, library merging.
Example
// inspect each stage with GCC/Clang: // g++ -E main.cpp -o main.i // preprocessed // g++ -S main.cpp -o main.s // assembly // g++ -c main.cpp -o main.o // object file // g++ main.o -o app // linked executable
💡 Knowing g++ -E / -S / -c maps each flag to a pipeline stage and makes this answer concrete instead of theoretical.
What is a namespace and why is `using namespace std;` discouraged in headers?
Answer
A namespace is a named scope that groups identifiers to prevent name collisions — the standard library lives in std. `using namespace std;` at global scope in a header dumps every std name into every file that includes it, which can silently change name lookup and cause collisions or even different overloads to be selected.
It is acceptable in a small .cpp file after all includes, but headers should use qualified names (std::vector) or scoped using-declarations (using std::string).
Example
// bad in a header: // using namespace std; // good: #include <string> void greet(const std::string& name); // scoped alias is also fine: namespace fs = std::filesystem;
How does basic input/output work in C++?
Answer
The <iostream> library provides std::cin (input), std::cout (output), std::cerr (unbuffered errors) and std::clog (buffered logging). The << operator inserts values into a stream and >> extracts them, and both return the stream so operations chain.
By default cin and cout are tied/synchronized with C stdio for mixed C/C++ I/O compatibility; disabling sync_with_stdio and untie-ing cin can speed up bulk input significantly.
Example
#include <iostream> #include <string> int main() { std::string name; std::cout << "Enter name: "; std::getline(std::cin, name); std::cout << "Hello, " << name << "!\n"; }
💡 Mention std::getline for full lines — operator>> stops at whitespace, which is the single most common beginner I/O bug.
What are the fundamental data types and their typical sizes?
Answer
Core types are bool, char, int, float, double, void, plus modifiers short, long, long long, signed, unsigned. The standard guarantees minimum ranges (e.g. int is at least 16 bits) rather than exact sizes — exact sizes are implementation-defined, so portable code checks with sizeof or uses fixed-width types from <cstdint>.
On typical 64-bit Linux/macOS/Windows toolchains: char is 1 byte, short 2, int 4, long 8 on LP64 (Linux/macOS) but 4 on Windows (LLP64), long long 8, float 4, double 8, bool 1, pointers 8.
Key points
- 1char/unsigned char/signed char — 1 byte; plain char signedness is implementation-defined.
- 2int — at least 16 bits, almost always 32 bits today.
- 3Use int32_t / uint64_t from <cstdint> when the exact width matters.
- 4sizeof(char) is always 1 by definition; everything else is measured in chars.
Example
#include <iostream> #include <cstdint> int main() { std::cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(void*) << "\n"; int32_t exact = 42; // exactly 32 bits, everywhere }
What is the difference between `const`, `constexpr` and `consteval`?
Answer
const means a value cannot be modified through that name — it may still be a runtime value. constexpr means the value can (and when used in a constant expression, must) be evaluated at compile time. consteval (C++20) is stricter: the function must always be evaluated at compile time (an immediate function).
So: const is about immutability, constexpr is about compile-time computability, consteval forces compile-time evaluation.
Example
const int runtime_n = read_input(); // const, but runtime value constexpr int compile_n = 42; // compile-time constant constexpr int square(int x) { return x * x; } static_assert(square(5) == 25); // checked at compile time consteval int force_ct(int x) { return x * 2; } // int y = force_ct(runtime_n); // error: must be constant-evaluated
💡 One line that shows mastery: const is runtime immutability, constexpr enables compile-time use, consteval mandates it.
What is the difference between declaration and definition?
Answer
A declaration introduces a name and its type without allocating storage or providing a body — it tells the compiler the entity exists. A definition additionally provides the body or allocates storage. A translation unit may declare an entity many times but must define non-inline functions and variables exactly once (the One Definition Rule).
Headers normally carry declarations; exactly one .cpp file carries the definition.
Example
// declaration (usually in a header): int add(int a, int b); extern int counter; // definition (in exactly one .cpp): int add(int a, int b) { return a + b; } int counter = 0;
💡 Connect this to linker errors: undefined reference means a declaration was seen but no definition was linked in.
What is the difference between `#include <...>` and `#include "..."`?
Answer
Both paste a file into the translation unit, but the search order differs: the quoted form searches the including file's directory first (for project headers) and then the include path, while the angle-bracket form searches only the configured include paths (for system and library headers).
Convention: use quotes for your own headers, angle brackets for standard/third-party headers. Include guards or #pragma once prevent double inclusion.
Example
#include <vector> // standard library: angle brackets #include "my_widget.h" // project header: quotes #pragma once // common guard against double inclusion
💡 Mention include guards in the same breath — interviewers often chain these two questions.
Recommended resourcesAffiliate links · may earn us a commission