ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
int main() {
    map<string, int> m;
    if (m["x"] == 0) cout << m.size();
    cout << m.count("y") << m.size();
}
  1. A. 101
  2. B. 000
  3. C. 001
  4. D. 111

Correct answer: A. 101

map::operator[] inserts a value-initialised element when the key is missing, so merely reading m["x"] adds it (size 1). count() only looks up and inserts nothing.

More OOP with C++ and Java questions

Exams that ask this topic