Static Members Interview Questions

    Question 1C++ OOPS Static Data Member

    Which statement is true about static data members in C++?

    Question 2C++ OOPS Static Member Function

    Which of the following is true about static member functions?

    Question 3C++ OOPS Static Variable in Class

    What will be the output?

    #include <iostream> using namespace std; class Test { static int count; public: Test() { count++; } static int getCount() { return count; } }; int Test::count = 0; int main() { Test a, b, c; cout << Test::getCount(); }

    Question 4C++ OOPS Static Member Initialization

    Where must static data members of a class be defined?

    Question 5C++ OOPS Static Member Function Call

    How can a static member function be called?

    Question 6C++ OOPS Static Member and Object Lifetime

    What happens to static members when all objects of the class are destroyed?

    Question 7C++ OOPS Static Function Limitation

    Which of the following is NOT allowed in static member functions?

    Question 8C++ OOPS Static Variable Behavior

    What will be the output?

    #include <iostream> using namespace std; class Demo { static int x; public: Demo() { x++; } ~Demo() { x--; } static int getX() { return x; } }; int Demo::x = 0; int main() { Demo d1, d2; cout << Demo::getX(); }

    Question 9C++ OOPS Static Member Access

    Which of the following is the correct way to access a static variable of a class `Sample` named `count`?

    Question 10C++ OOPS Static vs Non-Static Member

    What will be the output?

    #include <iostream> using namespace std; class Alpha { static int x; int y; public: Alpha() { y = 5; } static void printX() { cout << x; } void printY() { cout << y; } }; int Alpha::x = 100; int main() { Alpha::printX(); Alpha a; a.printY(); }