ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS
Which line fails to compile?
Which line fails to compile?
struct S {
int v = 1;
static int count;
static int get() { return v; } // line 1
static int all() { return count; } // line 2
int both() { return v + count; } // line 3
};
int S::count = 0;- A. Line 2
- B. Line 3
- C. None of them
- D. Line 1
Correct answer: D. Line 1
A static member function has no this pointer, so it cannot use the non-static member v. It can use static members (line 2), and a non-static member function can use both (line 3).
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual void f() { cout << "B"; } void g() { f(); } }; struct D : B { void f() override { cout << "D"; }…
- Which statement about this class is correct? cpp struct Buf { int p; Buf() : p(new int[8]) { } ~Buf() { delete[] p; } }; void use() { Buf…
- What is printed? cpp struct A { int x; A(int v) : x(v) { } }; struct B : A { int y; B(int v) : A(v 2), y(x + 1) { } }; int main() { B…
- What is printed? cpp struct B { B() { cout << "b"; } ~B() { cout << "~b"; } }; struct D : B { D() { cout << "d"; } ~D() { cout << "~d"; }…
- What is printed? cpp struct X { int v; X(int a) : v(a) { } bool operator==(const X& o) const { return v == o.v; } }; int main() { X a(1)…
- What happens? cpp class Shape { public: virtual double area() const = 0; }; class Square : public Shape { double s = 2; double area()…