Aspire Faculty ID #12785 · Topic: JECA MCA 2023 · Just now
JECA MCA 2023

30.What is the output of the following program ?
#include <iostream>
using namespace std;

class Demo {
public:
    static int count;
    Demo() { count++; }
};

int Demo::count = 0;

int main() {
    Demo var1;
    Demo var2[5];
    cout << var1.count;
    return 0;
}

Solution

  • Explanation:

    • int Demo::count = 0; initializes the static variable count to 0.

    • Demo var1; → constructor is called once → count = 1.

    • Demo var2[5]; → constructor is called 5 times (for each element in the array) → count = 6.

    • Since count is static, it is shared among all objects.

    • Finally, cout << var1.count; prints 6.

    ? Correct answer: (A) 6

Previous 10 Questions — JECA MCA 2023

Nearest first

Next 10 Questions — JECA MCA 2023

Ascending by ID
Ask Your Question or Put Your Review.

loading...