ProgrammingHardProduct-company placement · C++
For the call f(g(), h()) in C++17, which statement is correct?
- A. g() is always evaluated before h()
- B. h() is always evaluated before g()
- C. Their executions may interleave instruction by instruction
- D. g() and h() are both evaluated before f runs, but which of them runs first is unspecified
Correct answer: D. g() and h() are both evaluated before f runs, but which of them runs first is unspecified
Function arguments are indeterminately sequenced: each is evaluated completely, without interleaving (C++17), but the order among them is up to the implementation. Side effects must not depend on it.
More OOP with C++ and Java questions
- What happens when this is compiled? cpp struct P { int a; private: int b; }; int main() { P p{1, 2}; }
- Which line fails to compile? cpp struct A { int x = 1; }; struct B : protected A { }; struct C : B { int get() { return x; } }; int main()…
- std::sort(v.begin(), v.end(), [](int a, int b) { return a <= b; }); What is wrong with this call?
- What is printed? cpp int main() { priorityqueue<int> a; priorityqueue<int, vector<int>, greater<int>> b; for (int x : {3, 1, 4}) {…
- What happens when this is compiled? cpp struct Pt { int x, y; }; int main() { map<Pt, int> m; m[{1, 2}] = 5; }
- What is printed? cpp int main() { vector<int> a(3, 5), b{3, 5}; cout << a.size() << a[0] << b.size() << b[0]; }