What is the output of the following C++ program?

#include <iostream>
using namespace std;
class Test {
    int x;
public:
    Test() : x(10) {}
    int getValue() const {
        return x;
    }
};
int main() {
    const Test obj;
    cout << obj.getValue();
    return 0;
}
                        

Options

  • B. 10
  • C. Compilation error because const objects cannot call member functions
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 10

Detailed Explanation

Correct Answer: Option B

Explanation:
The object obj is const, so it can call a const-qualified member function. The constructor initializes x to 10, and getValue() returns that value. Output: 10.