Java | Class and Object | Question 4

Predict the output of following Java program.

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
  
    }
}

chevron_right


(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



My Personal Notes arrow_drop_up