To determine the output of the program, let's go through the code step by step:
- The program defines a class named
Foo.
- Inside
Foo, a constructor Foo() is defined, which prints "foo" when an object of Foo is created.
- Inside
Foo, another class named Bar is defined.
- Inside
Bar, a constructor Bar() is defined, which prints "bat" when an object of Bar is created.
- Inside
Bar, a method go() is defined, which prints "hi".
- The
main method is defined inside Foo and is the starting point of the program.
- In the
main method, an object f of Foo is created, which prints "foo" when created.
- Next, the
makeBar() method is called on f.
- Inside the
makeBar() method, a new anonymous object of Bar is created using the syntax (new Bar() {}). This object is created and its constructor is called, printing "bat".
- Then, the
go() method is called on this new object, printing "hi".
Therefore, the output of the program will be "foobarhi", which corresponds to option C.