ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int f() { std::cout << "f "; return 1; }
int g() { std::cout << "g "; return 2; }
int a = f();
int b = g();
int main() { }
  1. A. Unspecified order: could be either
  2. B. f g
  3. C. g f
  4. D. Nothing, since a and b are never used

Correct answer: B. f g

Globals in the same translation unit are guaranteed to initialize in the order they are declared, so f() runs (printing 'f') before g() (printing 'g') — unlike the unspecified order across different translation units.

More OOP with C++ and Java questions

Exams that ask this topic