Multiple choice technology programming languages

Given: public class Foo { Foo() {System.out.print("foo");} class Bar{ Bar() {System.out.print("bar");} public void go() {System.out.print("hi");} } public static void main(String[] args) { Foo f = new Foo (); f.makeBar(); } void makeBar() { (new Bar() {}).go(); } } What is the result?

  1. Compilation fails.

  2. An error occurs at runtime.

  3. foobarhi

  4. barhi

  5. hi

  6. foohi

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

new Foo() prints 'foo'. makeBar() instantiates an anonymous subclass of inner class Bar using new Bar() {}, which executes Bar's constructor and prints 'bar'. It then calls go() which prints 'hi', resulting in 'foobarhi'.