ProgrammingModerateProduct-company placement · C++
Which line fails to compile (C++11 or later)?
Which line fails to compile (C++11 or later)?
int a(3.7); // L1
int b = 3.7; // L2
int c{3.7}; // L3- A. L1 and L3
- B. L2 and L3
- C. L3 only
- D. None; all three store 3
Correct answer: C. L3 only
Brace (list) initialisation forbids narrowing conversions such as double to int, so L3 is ill-formed. Parentheses and = silently truncate to 3.
More OOP with C++ and Java questions
- What is printed? cpp struct A { int v = 1; }; struct B : A { }; struct C : A { }; struct D : B, C { }; int main() { D d; cout << d.v; }
- Which line fails to compile? cpp class B { protected: int x = 1; }; class D : public B { public: int f(D& d) { return d.x; } // L1 int…
- What is printed? cpp struct B { int v = 7; }; class D1 : B { }; struct D2 : B { }; int main() { D1 a; D2 b; cout << b.v << a.v; }
- What is printed? cpp template <typename T> void f(T) { cout << "T"; } void f(int) { cout << "I"; } int main() { f(1); f(1.0); f('a')…
- What is printed? cpp template <typename T> struct Is { static const int v = 0; }; template <> struct Is<int> { static const int v = 1; }…
- What is printed? cpp struct L { L() { cout << "L"; } }; void f() { static L l; cout << "f"; } int main() { f(); f(); f(); }