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
}
  1. A. L1 only
  2. B. L1 and L3
  3. C. L1, L2 and L3
  4. 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

Exams that ask this topic