ProgrammingModerateProduct-company placement · C++
What happens when this is compiled?
What happens when this is compiled?
struct A { A(int) { } };
int main() { A a; }- A. Compile-time error: A has no default constructor
- B. It compiles; a is zero-initialised
- C. It compiles; A(int) is called with 0
- D. It compiles with a warning only
Correct answer: A. Compile-time error: A has no default constructor
The compiler generates a default constructor only if the class declares no constructors at all. Declaring A(int) suppresses it; add A() = default; to get it back.
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() {…
- What is printed? cpp template <class T> struct Base { void hi() { staticcast<T>(this)->impl(); } void impl() { cout << "B"; } }; struct X…
- What happens? cpp template <class T> T mx(T a, T b) { return a > b ? a : b; } int main() { cout << mx(3, 7); // L1 cout << mx(1, 2.5); //…
- What is printed? cpp struct A { A() { cout << "A"; } ~A() { cout << "a"; } }; struct B { B() { cout << "B"; } ~B() { cout << "b"; } }…
- Which line fails to compile? cpp string a = "a" + "b"; // L1 string b = string("a") + "b"; // L2 string c = "a" + string("b"); // L3…
- What is printed? cpp int main() { string s = "hi"; try { cout << s.at(1); s.at(5); cout << "X"; } catch (const outofrange&) { cout << "R"…