ProgrammingVery hardProduct-company placement · C++
What is printed when compiled as C++17?
What is printed when compiled as C++17?
struct A {
A() { cout << "D"; }
A(const A&) { cout << "C"; }
};
int main() { A a = A(A()); }- A. DC
- B. D
- C. DCC
- D. Compile-time error: most vexing parse
Correct answer: B. D
Since C++17, initialising an object from a prvalue of the same type is guaranteed to construct it directly (mandatory copy elision); the nested temporaries are never materialised, so only the default constructor runs.
More OOP with C++ and Java questions
- What is printed? cpp struct B { B& operator=(int) { cout << "B"; return this; } }; struct D : B { }; int main() { D d; d = 5; }
- What is printed? cpp template <typename T> struct Box { static int count; Box() { count++; } }; template <typename T> int Box<T>::count =…
- What is printed? cpp struct N { int v; N(int x = 7) : v(x) { } }; void show(N n) { cout << n.v; } int main() { N a; N b = 3; show(4)…
- What is printed? java class P { void f(int x) { System.out.print("P-int "); } } class C extends P { void f(long x) {…
- What is printed? java BigDecimal a = new BigDecimal("1.0"), b = new BigDecimal("1.00"); Set<BigDecimal> h = new HashSet<>(List.of(a, b))…
- What is printed? java Stream.of("a", "bb", "ccc") .filter(s -> { System.out.print("f" + s.length()); return s.length() > 1; }) .map(s -> {…