ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
void f(long) { cout << "L"; }
void f(double) { cout << "D"; }
int main() { f(5L); f(2.5f); f(5); }- A. LDL
- B. Compile-time error: the call f(5) is ambiguous
- C. LDD
- D. LLL
Correct answer: B. Compile-time error: the call f(5) is ambiguous
5L and 2.5f have an exact match and a promotion (float → double). For an int, both int → long and int → double are standard conversions of the same rank, so f(5) is ambiguous and the program does not compile.
More OOP with C++ and Java questions
- What is printed? cpp struct B { int b; B(int x) : b(x) { } }; struct D : B { int d; D(int x, int y) : B(x), d(y) { } }; int main() { D…
- How many of these statements about C++ dynamic memory are correct? (i) Memory obtained with new[] must be released with delete[]. (ii)…
- What happens when this is compiled? cpp struct K { const int c; int& r; K(int& x) { c = 1; r = x; } };
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { B(A) { } }; void f(B) { } int main() { f(A(1)); // L1 f(1); // L2 }
- 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"; } }