ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
template <typename T> struct Box { static int count; Box() { count++; } };
template <typename T> int Box<T>::count = 0;
int main() {
    Box<int> a, b;
    Box<double> c;
    cout << Box<int>::count << Box<double>::count;
}
  1. A. 33
  2. B. 30
  3. C. 21
  4. D. 11

Correct answer: C. 21

Each template instantiation is a separate class with its own static member, so Box<int>::count and Box<double>::count are independent. (Contrast Java, where erasure leaves one shared static.)

More OOP with C++ and Java questions

Exams that ask this topic