WBJECA 2025 Computer PYQ — What will be the output of this program? #include <iostream> usin… | Mathem Solvex | Mathem Solvex
Tip:A–D to answerE for explanationV for videoS to reveal answer
WBJECA 2025 — Computer PYQ
WBJECA | Computer | 2025
What will be the output of this program?
#include <iostream>
using namespace std;
class Test {
public:
static int x;
Test () { x++; }
void display() { cout << x << " "; }
};
int Test :: x = 5;
int main() {
Test t1;
t1.display();
Test t2;
t2.display();
return 0;
}
Choose the correct answer:
A.
67
(Correct Answer)
B.
66
C.
77
D.
55
Correct Answer:
67
Explanation
To find the output, let's trace the execution step-by-step:
Initialization: The static member x is defined and initialized outside the class:
x=5
Object t1 creation: When Test t1; is executed, the constructor Test() is called. Inside the constructor, the code executes x++.
x=5+1=6
First display() call:t1.display() prints the current value of x, which is 6.
Object t2 creation: When Test t2; is executed, the constructor Test() is called again. Because x is static, it maintains its value from the previous operation.
x=6+1=7
Second display() call:t2.display() prints the current value of x, which is 7.
The final output of the program is: 6 7
Correct Answer:(A) 6 7
Explanation
To find the output, let's trace the execution step-by-step:
Initialization: The static member x is defined and initialized outside the class:
x=5
Object t1 creation: When Test t1; is executed, the constructor Test() is called. Inside the constructor, the code executes x++.
x=5+1=6
First display() call:t1.display() prints the current value of x, which is 6.
Object t2 creation: When Test t2; is executed, the constructor Test() is called again. Because x is static, it maintains its value from the previous operation.
x=6+1=7
Second display() call:t2.display() prints the current value of x, which is 7.