December Code

C++ for placements: interview questions by topic

C++ reaches placement rounds from two directions: as the language most competitive programmers write their coding round in, and as a subject in its own right, asked as a few lines of code and 'what does this print?' or 'does this compile?'. The questions rarely stop at syntax. They sit where C++ gives you control that other languages hide: a reference that can never be re-bound, a constructor the compiler silently removes, a copy it is allowed to skip, and an object that is destroyed at a precise, predictable moment.

Interviews for product roles then test modern C++: which smart pointer owns what, why std::move does not move anything, how a template differs from a Java generic, what an iterator invalidation bug looks like, and which STL container fits a problem. The object-oriented core, virtual functions, vtables, slicing and the diamond, is answered on the OOP pages, which cover C++ alongside Java.

The pages below take one C++ topic each, with answers whose code was compiled and run with g++, and one question exactly as the diagnostic asks it. The diagnostic draws from every C++ topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

C++ topics, one page each

8 of the 8 C++ topics in the bank have a page so far; the diagnostic draws from all 8.

  1. C++ references and const interview questions

    C++ references and const as interviews test them: references versus pointers, passing by const reference, dangling references, constexpr.

  2. C++ classes and constructors interview questions

    C++ classes as interviews test them: the rules of three, five and zero, = default and = delete, explicit, delegating constructors, copy elision.

  3. C++ inheritance interview questions

    C++ inheritance beyond virtual: base constructors with arguments, name hiding and using, static_cast versus dynamic_cast, typeid, virtual clone.

  4. C++ operator overloading interview questions

    C++ overloading as interviews test it: member versus free operators, printing with <<, copy-and-swap, function objects, const overloads and <=>.

  5. C++ smart pointers and memory interview questions

    C++ memory as interviews test it: new and delete[], RAII, unique_ptr, shared_ptr and weak_ptr, make_unique and make_shared, custom deleters.

  6. C++ templates interview questions

    C++ templates as interviews test them: instantiation versus Java generics, headers, specialisation, non-type parameters, variadic templates, concepts.

  7. C++ STL interview questions

    The C++ STL as interviews test it: choosing a container, vector capacity, iterator invalidation, map versus unordered_map, emplace and custom sorts.

  8. C++ lambdas and move semantics interview questions

    Modern C++ as interviews test it: lambda captures and mutable, lvalues and rvalues, std::move, move constructors, perfect forwarding and noexcept.

One question, exactly as the diagnostic asks it

From the C++ bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Memory & Smart Pointers · mediumCPP-016

What does this C++ code print?

auto a = std::make_shared<int>(1);
auto b = a;
{
    auto c = a;
    std::cout << a.use_count();
}
std::cout << a.use_count();
  1. 132correct
  2. 233
  3. 311
  4. 421

use_count() reports how many shared_ptr objects currently share ownership of the int. Inside the block, a, b and c all own it, so it prints 3. When the block ends, c is destroyed and the count drops to 2, giving 32. 33 forgets that c's destructor gives up its share. 11 treats copying a shared_ptr as copying the int, so that each pointer has its own; copies share one object and one count. 21 counts only the other owners, not a itself. When the last owner goes, the count reaches 0 and the int is deleted.

More on this topic: C++ smart pointers and memory interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 C++ questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated