Viva & Theory Questions
Language basics
- C vs C++? C++ adds classes, templates, references, exceptions, namespaces, overloading, the STL; C is procedural only.
- Why is C++ called multi-paradigm? Procedural, object-oriented, generic and functional styles.
structvsclass? Only default access (public vs private).- What is a namespace? A named scope to avoid name clashes (
std::). - Reference vs pointer? Reference: alias, never null, can't be re-seated. Pointer: address, nullable, re-pointable.
constvsconstexpr? Read-only vs compile-time constant.#definevsconst/inline? Text replacement (no type/scope) vs real typed language entities.inlinefunction? May be defined in several translation units; compiler decides actual inlining.
Classes
- Constructor types? Default, parameterized, copy, move, delegating, converting.
- Initializer list — why? Initializes directly; required for const, references, members without default constructors.
thispointer? Points to the current object.staticmember? One per class, not per object.explicit? Prevents implicit conversions through a constructor.mutable? Member changeable in const functions.
Polymorphism & inheritance
- Compile-time (overloading, templates) vs runtime (virtual functions)
- Pure virtual function, abstract class, virtual destructor, vtable, slicing
Memory
new/deletevsmalloc/free; stack vs heap; RAII; smart pointers; memory leak
Templates & STL
- Template? Generic code instantiated per type at compile time.
- STL components? Containers, iterators, algorithms (+ function objects).
vectorvslist? Contiguous/random access vs linked/O(1) middle insert.mapvsunordered_map? Tree (sorted, O(log n)) vs hash (unordered, O(1) avg).
Errors & modern C++
- Exception handling keywords? try, throw, catch;
noexcept. - What is
auto? Type deduced from the initializer. - Lambda? Anonymous function object with optional captures.
- Move semantics? Transfer resources from temporaries instead of copying.
- Undefined behaviour? Code with no defined meaning — avoid it.
Example
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#define SQUARE_MACRO(x) ((x) * (x))
constexpr int square(int x) { return x * x; }
struct S { int x = 1; }; // public by default
class C { int x = 1; public: int get() const { return x; } }; // private by default
namespace budget { int total = 100; }
namespace savings { int total = 500; }
class Meters {
public:
explicit Meters(double m) : m_(m) {}
double value() const { return m_; }
private:
double m_;
};
class Counter {
public:
void hit() const { ++hits_; } // allowed: hits_ is mutable
int hits() const { return hits_; }
private:
mutable int hits_ = 0;
};
template <typename T>
T biggest(const std::vector<T>& v) { T b = v[0]; for (const T& x : v) if (x > b) b = x; return b; }
int main() {
S s; C c;
std::cout << "struct member public: " << s.x << ", class needs getter: " << c.get() << '\n';
std::cout << "namespaces: " << budget::total << " vs " << savings::total << '\n';
int a = 10;
int& ref = a;
int* ptr = &a;
ref += 5;
*ptr += 5;
std::cout << "reference and pointer both changed a: " << a << '\n';
std::cout << "macro vs constexpr: " << SQUARE_MACRO(3) << ' ' << square(3) << '\n';
Meters m(5.0); // Meters m = 5.0; would not compile: explicit
std::cout << "explicit constructor: " << m.value() << " m\n";
const Counter counter;
counter.hit(); counter.hit();
std::cout << "mutable in const object: " << counter.hits() << " hits\n";
std::cout << "template: " << biggest(std::vector<int>{ 3, 9, 2 }) << ' '
<< biggest(std::vector<std::string>{ "apple", "mango", "banana" }) << '\n';
std::map<std::string, int> ordered = { { "c", 3 }, { "a", 1 }, { "b", 2 } };
std::cout << "map keeps order:";
for (const auto& [k, v] : ordered) std::cout << ' ' << k;
std::unordered_map<std::string, int> hashed(ordered.begin(), ordered.end());
std::cout << ", unordered_map has " << hashed.size() << " items (order unspecified)\n";
auto add = [offset = 10](int x) { return x + offset; };
std::cout << "lambda with capture: " << add(5) << '\n';
auto owner = std::make_unique<std::string>("RAII frees me automatically");
std::string moved = std::move(*owner);
std::cout << "moved string: " << moved << ", source now empty? " << std::boolalpha << owner->empty() << '\n';
try { throw std::runtime_error("demo"); } catch (const std::exception& e) { std::cout << "caught: " << e.what() << '\n'; }
return 0;
}
/* Output:
struct member public: 1, class needs getter: 1
namespaces: 100 vs 500
reference and pointer both changed a: 20
macro vs constexpr: 9 9
explicit constructor: 5 m
mutable in const object: 2 hits
template: 9 mango
map keeps order: a b c, unordered_map has 3 items (order unspecified)
lambda with capture: 15
moved string: RAII frees me automatically, source now empty? true
caught: demo
*/