Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

Prerequisite : Inner Classes in Java 

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

JAVA




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? 

JAVA




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:

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? 

JAVA




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 :

The answer 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? 

JAVA




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. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Sep, 2022
Like Article
Save Article
Previous
Next
Similar Reads