Output of Java Programs | Set 51 (Regular Inner class)

3

Prerequisite : Inner Classes in Java

1. What will be the .class file name of the Inner class?

class Outer {
    class Inner {
    public
        void m1()
        {
            System.out.println("Hello Geeks");
        }
    }
}

Options:
1.Outer.Inner.class
2.Inner.class
3.Outer.class
4.Outer$Inner.class

Output:

The answer is option (4)

Explanation : The java compiler creates two class files in case of inner class. The class file name of inner class is “Outer$Inner”. $ symbol is used to represent inner classes.

2. What will be the output of the following program if we use java Outer$Inner in command prompt?

class Outer {
    class Inner {
    public
        void m1()
        {
            System.out.println("Hii");
        }
    } public static void main(String[] args)
    {
        Outer o = new Outer();
        o.m1();
    }
}

Options:
1.Hii
2.Compile time error
3.Run time error
4.No Output

Output:

The answer is option (3)

Explanation : As we know that compiler is not responsible to check whether class contains main() method or not. But during the time of execution JVM check whether class contains main() method or not. If the class does not contain main() method then we will get Run time error saying NoSuchMethodError:main.

3. What will be the output of the following program?

class Outer {
    class Inner {
    public
        static void main()
        {
            System.out.println("Hii");
        }
    } Outer o = new Outer();
    o.main();
}

Options:
1.Hii
2.Run time
3.Compile time error
4.No Output

Output:

The answe is option (3)

Explanation : In the above program, we will get compile time error saying “Inner classes can’t have static declaration” and here in the program we declare a static method.

4.What will be the option is suitable to replace with Line-1 to access m1() method of the Inner class?

class Outer {
    class Inner {
    public
        void m1()
        {
            System.out.println("Hii");
        }
    } public static void main(String[] args)
    {
        Line - 1
    }
}

Options:
1.Outer o=new Outer();
Outer.Inner i=o.new Inner();
i.m1();
2.Outer.Inner i=new Outer().new Inner();
i.m1();
3.new Outer().new Inner().m1();
4.None

Output:

The answer is option (1), (2), (3)

Explanation : In the concept of Inner classes, we first have to create object of Outer class then with the help of Outer class Object we can create Object of Inner class. With the help of Inner class Object we can access instance method of Inner class.

5.Which is true about a Normal/Regular inner class?

1.It must be marked final.
2.It can be marked native.
3.It must be marked public.
4.It can be marked static.

Output:

The answer is option (4)

Explanation : The applicable modifiers for Regular/Normal Inner classes are public, final, default, strictfp, abstract, private, protected, static. But it is not mandatory to use.

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.