ProgrammingHardProduct-company placement · C++

Which lines fail to compile?

Which lines fail to compile?
struct S {
    int x = 1;
    static int y;
    static int f() { return y; }         // L1
    static int g() { return x; }         // L2
    int h() { return x + y; }            // L3
    static int k() { return this->x; }   // L4
};
int S::y = 0;
  1. A. L2 and L4
  2. B. L2 only
  3. C. L4 only
  4. D. L1, L2 and L4

Correct answer: A. L2 and L4

A static member function has no this pointer, so it cannot use non-static members (L2) or this (L4). It can use static members (L1), and non-static functions can use both (L3).

More OOP with C++ and Java questions

Exams that ask this topic