Declarations and Access Control

Q1: 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);
    }
}

A Compilation fails.

B i = 5

C i = 3

D A ClassCastException will occur.

ANS:A - i = 3

Object o=(Object)new Foo();-->Here object of Class Foo is Created and Converted it into Super Class Object.

Foo foo=(Foo)o;-->Here the object is Converted into Subclass i.e. Foo and Assign to reference foo.
And finally the Value of i=3 is displayed by foo.i



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.