Multiple choice

What will be the output of the program? public class Test {
public static void main(String args[]) { class Foo { public int i = 3; } Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println("i = " + foo.i); } }

  1. i = 3

  2. Compilation fails.

  3. i = 5

  4. A ClassCastException will occur.

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

The code compiles and runs successfully. A local class Foo is defined inside main(), instantiated, cast to Object (upcasting, always allowed), then cast back to Foo (downcasting, valid here since the actual object is a Foo). The instance variable i is initialized to 3, so the output is "i = 3". Local classes can be used within their scope.