Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Inner Classes in Java 

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

JAVA





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.



Last Updated : 29 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads