Tricky C++ output questions

Difficulty: Advanced

Question

Predict the output of these classic C++ snippets: an empty struct's sizeof, a virtual function with different default arguments, negative integer division and modulus, char arithmetic and floating-point equality.

Answer

Output-prediction questions are the staple of written tests at TCS, Infosys and Samsung, and they sneak into live rounds as warm-ups. The goal isn't cleverness but knowing a small set of rules cold. Let's go through the classics and the rule behind each.

Snippet one: what is sizeof of an empty struct? It is 1, not 0. Every object must have a distinct address, so the compiler adds a byte of padding. (When used as a base class, the empty base optimisation can remove even that byte.) Rule: no zero-sized objects.

Snippet two: the default-argument trap. A base class declares 'virtual void f(int x = 1)' and the derived class overrides it with 'void f(int x = 2)'. You call f() through a Base reference bound to a Derived object. Which function runs, and with which x? The function is chosen dynamically, so the Derived body runs. But default arguments are resolved statically, using the type of the expression you called through, so x is 1, the base's default. The output is 'D 1'. Rule: virtual dispatch applies to the function, never to default arguments. Never change defaults in overrides.

Snippet three: integer division and remainder with negatives. Since C++11, division truncates toward zero, and the remainder satisfies (a/b)*b + a%b == a. So -7 / 2 is -3 and -7 % 2 is -1, the sign of the remainder follows the dividend. This differs from Python, where -7 // 2 is -4. Rule: truncation toward zero.

Snippet four: 'cout << 'a' + 1'. The char is promoted to int in arithmetic, so the result is the int 98 (assuming ASCII), and that is what prints, not the character 'b'. If you want the letter, cast: static_cast<char>('a' + 1). Rule: integral promotion turns char arithmetic into int.

Snippet five: floating point. '0.1 + 0.2 == 0.3' evaluates to false, because 0.1 and 0.2 have no exact binary representation, and the sum is 0.30000000000000004 in double. Printing with default cout precision shows 0.3, which can fool you. Compare with a tolerance instead. Rule: never test floating-point numbers for exact equality.

There are more favourites that you should recognise even when not shown here. 'i = i++ + ++i' modifies i twice without a sequence: avoid. 'int a[3]; sizeof(a)/sizeof(a[0])' gives 3, but the same expression on an array parameter (which has decayed to a pointer) gives pointer size divided by element size. 'unsigned u = 0; u - 1' wraps to a large positive number, so 'if (u - 1 > 0)' is always true. A comparison of a signed and unsigned value converts the signed one to unsigned. And macros like '#define SQ(x) xx' with SQ(2+3) expand to 2+32+3 = 11, not 25.

The way to answer these in an interview is to say the rule aloud before the result. It shows the interviewer that you're reasoning, and if you slip on the final value, you still earn credit for the principle.

Code examples

Five classic outputs in one program

#include <iostream>

struct Empty {};

struct B {
    virtual void f(int x = 1) { std::cout << "B " << x << "\n"; }
    virtual ~B() = default;
};
struct D : B {
    void f(int x = 2) override { std::cout << "D " << x << "\n"; }
};

#define SQ(x) x * x

int main() {
    std::cout << sizeof(Empty) << "\n";
    D d;
    B& b = d;
    b.f();                                    // D body, but B's default argument
    std::cout << -7 / 2 << " " << -7 % 2 << "\n";
    std::cout << 'a' + 1 << "\n";
    std::cout << (0.1 + 0.2 == 0.3) << "\n";
    std::cout << SQ(2 + 3) << "\n";          // 2 + 3 * 2 + 3
}

Each line follows a specific rule: no zero-sized objects; static default arguments with dynamic dispatch; truncation toward zero; integral promotion; binary floating point inexactness; macro text substitution.

Key points

Common follow-up questions

Interview tips

Concepts covered

output prediction, default arguments, integer division, sizeof

More C++ & STL questions