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();
}- A. 101
- B. 000
- C. 001
- 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
- What is printed? cpp int main() { set<int, greater<int>> s{3, 1, 3, 2}; for (int x : s) cout << x; cout << s.size(); }
- What is printed? cpp int main() { vector<int> v{1, 2, 3, 2, 4}; v.erase(remove(v.begin(), v.end(), 2), v.end()); cout << v.size() << ":"…
- What is wrong with this loop? cpp vector<int> v{1, 2, 3}; for (int x : v) if (x == 1) v.pushback(1);
- What is printed? cpp void log(int) { cout << "G"; } struct S { void log() { cout << "M"; } void run() { log(5); } }; int main() { S s…
- What happens (C++14)? cpp struct S { static int n; }; int main() { S::n = 1; cout << S::n; }
- Which lines fail to compile? cpp void f(int) { cout << "I"; } void f(double) = delete; int main() { f(5); // L1 f('a'); // L2 f(2.5); //…