ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
int main() {
set<int, greater<int>> s{3, 1, 3, 2};
for (int x : s) cout << x;
cout << s.size();
}- A. 3213
- B. 31323
- C. 1233
- D. 3214
Correct answer: A. 3213
A set keeps unique keys ordered by its comparator; greater<int> gives descending order and the duplicate 3 is discarded.
More OOP with C++ and Java questions
- 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); //…
- Which lines fail to compile? cpp struct H { static H make() { return new H; } void destroy() { delete this; } private: ~H() { } }; int…