Multiple choice

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { public: mutable int a; /line1/ Test() /line2/ { a=10; /line3/ } void func() const /line4/ { a++; /line5/ cout<<a; } }; int main() { const Test obj; obj.func(); }

  1. Error at line5

  2. Error at line4

  3. Error at line3

  4. 11

  5. Error at line1

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

 This program works fine & there are no compile time errors. Mutable keyword is used to change the values of member variables eventhough the objects are declared as constant. In the program we declared constant object as follows: const Test obj; Now the obj is a constant object where its data members can't be changed. Only if data members are declared with mutable keyword are eligible for manipulation or changes. Hence value is incremented using a++ & output is displayed as 11.