Predict the output of following C++ program.
(A) 0 0
(B) 5 5
(C) 0 5
(D) Compiler Error
Explanation: Static functions can be called without any object. So the call “Test::getX()” is fine.
Since x is initialized as 0, the first call to getX() returns 0. Note the statement x++ in constructor. When an array of 5 objects is created, the constructor is called 5 times. So x is incremented to 5 before the next call to getX().
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 4
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 6
- Comparison of static keyword in C++ and Java
- Static Keyword in C++
- Output of Java Programs | Set 48 (Static keyword)
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Internal static variable vs. External static variable with Examples in C
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Understanding "static" in "public static void main" in Java
- Class Loading and Static Blocks Execution Using Static Modifier in Java
- C++ | friend keyword | Question 1
- C++ | friend keyword | Question 2
- C++ | const keyword | Question 1
- C++ | const keyword | Question 2
- C++ | const keyword | Question 3