ProgrammingVery hardProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
struct A { A(int) { } };
struct B { explicit B(int) { } };
A f() { return 5; }      // L1
B g() { return 5; }      // L2
B h() { return B(5); }   // L3
  1. A. L2 only
  2. B. L1 and L2
  3. C. L3 only
  4. D. None

Correct answer: A. L2 only

return expr copy-initialises the return value, so it can use a converting constructor (L1) but not an explicit one (L2). Constructing B explicitly (L3) is fine.

More OOP with C++ and Java questions

Exams that ask this topic