ProgrammingVery hardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
struct A { A(int) { } };
struct B { B(A) { } };
void f(B) { }
int main() {
f(A(1)); // L1
f(1); // L2
}- A. L2 only
- B. L1 only
- C. Both
- D. Neither
Correct answer: A. L2 only
An implicit conversion sequence may contain at most ONE user-defined conversion. A → B (via B(A)) is one; int → A → B would need two, so f(1) is rejected.
More OOP with C++ and Java questions
- In C++, a destructor throws an exception while the stack is already being unwound because of another exception. What happens?
- What happens in C++11 or later? cpp struct T { ~T() { throw 1; } }; int main() { try { T t; } catch (int) { cout << "caught"; } }
- What is printed? cpp struct B { virtual string who() const { return "B"; } }; struct D : B { string who() const override { return "D"; }…
- What is printed? cpp struct G { G() { cout << "G"; } ~G() { cout << "g"; } }; G g1; int main() { cout << "M"; static G s; cout << "m"; }
- Which lines fail to compile? cpp struct P { P(int) { } }; int main() { P a[3]; // L1 P b[2] = {1, 2}; // L2 vector<P> v(3); // L3…
- Which line fails to compile? cpp struct B { virtual B clone() { return new B; } virtual int id() { return 1; } }; struct D : B { D clone()…