144k views
2 votes
Which line in the following program will cause a compiler error?

1 #include
2 using namespace std;
3
4 int main()
5 {
6 const int MY_VAL;
7 MY_VAL = 77;
8 cout << MY_VAL << endl;
9 return 0;
10 }

A) 6
B) 8
C) 9
D) 7

1 Answer

6 votes

Final answer:

The line that will cause a compiler error is line 7, where an attempt is made to assign a new value to a constant variable.

Step-by-step explanation:

The line in the given program that will cause a compiler error is line 7. The error occurs because the variable MY_VAL is declared as a constant (const) on line 6, but on line 7, an attempt is made to assign a new value to it, which is not allowed for const variables.