Open In App

Java | Class and Object | Question 4

Predict the output of following Java program.




class demoClass
{
    int a = 1;
  
    void func()
    {
        demo obj = new demo();
        obj.display();
    }
  
  
    class demo
    {
        int b = 2;
  
        void display()
        {
            System.out.println("\na = " + a);
        }
    }
  
    void get()
    {
        System.out.println("\nb = " + b);
    }
}
  
  
class Test
{
    public static void main(String[] args)
    {
        demoClass obj = new demoClass();
        obj.func();
        obj.get();
  
    }
}

(A)

a = 1
b = 2

(B) Compilation error



(C)

b = 2
a = 1

Answer: (B)
Explanation:
Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer class can not access variable ‘b’ of inner class.
Quiz of this Question



Article Tags :