OA. free
Free
Micron Core Computer Science Core Computer Science Medium

`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;
Choose one option.
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:

  1. The class A opens with class A { and defines member int a = 3; and method f().
  2. After f()'s closing brace, there is an extra } that closes the class definition prematurely.
  3. The line A(); is then outside the class, which is not a valid constructor declaration.
  4. A stray { follows, containing cout << "ABCi" << " ";, which is not part of any function or class.
  5. The final }; attempts to close a class that is already closed, causing a syntax error.
  6. Since the code has multiple syntax errors, it will not compile, so the correct answer is that the program generates a compile time error.