What happens when the following C++ code is compiled?

#include <iostream>
using namespace std;
class Student {
    int marks;
};
int main() {
    Student s;
    s.marks = 90;
    cout << s.marks;
    return 0;
}
                        

Options

  • A. It prints 90
  • B. It prints 0
  • C. A compile-time access error occurs
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. A compile-time access error occurs

Detailed Explanation

Correct Answer: Option C

Explanation: Class members are private by default. Therefore, marks cannot be accessed directly from main(). The code fails to compile because both the assignment and the read access violate access control.