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;- A. L2 and L4
- B. L2 only
- C. L4 only
- 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
- Which lines fail to compile? cpp struct B { virtual void f(int) { } virtual void g() const { } void h() { } }; struct D : B { void f(int)…
- Which lines fail to compile? cpp struct B { virtual void f() final { } virtual void g() { } }; struct D : B { void f() { } // L1 void g()…
- What is printed? cpp struct B { virtual ~B() { } }; struct D : B { }; struct E : B { }; int main() { B p = new D; cout <<…
- What is printed? cpp struct A { }; struct B : A { }; struct P { virtual ~P() { } }; struct Q : P { }; int main() { B b; Q q; A& ra = b; P&…
- What is printed? cpp struct A { A(int x) { cout << x; } A() { cout << 0; } }; struct B : virtual A { B() : A(1) { cout << "B"; } }; struct…
- What is printed? cpp void f(long) { cout << "L"; } void f(double) { cout << "D"; } int main() { f(5L); f(2.5f); f(5); }