ProgrammingHardProduct-company placement · C++
Which lines fail to compile?
Which lines fail to compile?
struct P { P(int) { } };
int main() {
P a[3]; // L1
P b[2] = {1, 2}; // L2
vector<P> v(3); // L3
vector<P> w(3, P(0)); // L4
}- A. L1 only
- B. L1 and L3
- C. L1, L2 and L3
- D. L3 only
Correct answer: B. L1 and L3
P has no default constructor, so an array or vector of 3 default-constructed P's is impossible. Initialising each element from an int (L2) or copying a given P (L4) is fine.
More OOP with C++ and Java questions
- Which line fails to compile? cpp struct B { virtual B clone() { return new B; } virtual int id() { return 1; } }; struct D : B { D clone()…
- What is printed when compiled as C++17? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } }; int main() { A a = A(A()); }
- What is printed? cpp struct B { B& operator=(int) { cout << "B"; return this; } }; struct D : B { }; int main() { D d; d = 5; }
- What is printed? cpp template <typename T> struct Box { static int count; Box() { count++; } }; template <typename T> int Box<T>::count =…
- What is printed? cpp struct N { int v; N(int x = 7) : v(x) { } }; void show(N n) { cout << n.v; } int main() { N a; N b = 3; show(4)…
- What is printed? java class P { void f(int x) { System.out.print("P-int "); } } class C extends P { void f(long x) {…