`cpp class A { int a = 3; public: void f() { cout << +a << " " << "ABC" << " "; } } A(); {...
Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.
class A
{
int a = 3;
public:
void f()
{
cout << +a << " " << "ABC" << " ";
}
}
A();
{
cout << "ABCi" << " ";
}
};
A a;
Show answer & explanation
Answer: D. Program generates compile time error.
The code contains multiple syntax errors: the class definition is closed prematurely before the constructor declaration, the constructor is declared outside the class without a scope resolution operator, and there is a stray brace after the constructor declaration. These issues prevent the program from compiling.
Step-by-step Derivation:
- The class A opens with
class A {and defines memberint a = 3;and methodf(). - After
f()'s closing brace, there is an extra}that closes the class definition prematurely. - The line
A();is then outside the class, which is not a valid constructor declaration. - A stray
{follows, containingcout << "ABCi" << " ";, which is not part of any function or class. - The final
};attempts to close a class that is already closed, causing a syntax error. - Since the code has multiple syntax errors, it will not compile, so the correct answer is that the program generates a compile time error.