Object Oriented Programming using Java Questions and Answers – Inner Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Inner Class”.

1. What will be the output of the following Java code?

class MainClass
{
    int x = 15;
    class NestedClass 
    {
        int y = 15;
    }
}
class InnerClass
{
    public static void main(String[] args) 
    {
        MainClass obj1 = new MainClass();
        MainClass.NestedClass obj2 = obj1.new NestedClass();
        System.out.println(obj2.y + obj1.x);
    }
}

a) Compilation Error
b) 30
c) 15
d) Garbage Value
View Answer

Answer: b
Explanation: The concept of inner classes is used. The purpose of inner classes is to group classes that belong together, which makes your code more readable and maintainable. Objects obj1 and obj2 are used to access the data members of both the classes.
Output:

$ javac InnerClass.java
$ java InnerClass
30

2. What will be the output of the following Java code?

advertisement
advertisement
class Outer
{
    int x = 15;
    private class Nested 
    {
        int y = 15;
    }
}
class Nested
{
    public static void main(String[] args) 
    {
        Outer ob1 = new Outer();
        Outer.Nested ob2 = ob1.new Nested();
        System.out.println(ob2.y + ob1.x);
    }
}

a) Compilation Error
b) 30
c) 15
d) Garbage Value
View Answer

Answer: a
Explanation: In the above program, the concept of inner classes is used. The idea of inner classes is to group classes that belong together, which makes the code more readable and maintainable. The data members of both the classes Outer and Nested can usually be accessed by creating individual objects but as the class Nested is private, it is not possible to access the data members of that class.
Output:

$ javac Nested.java
$ java Nested
java.lang.RuntimeException: Uncompilable source code - Outer.Nested has private access

3. What will be the output of the following Java code?

advertisement
class Outer 
{
    int x = 10;
    class Inner 
    {
        public int ReturnMethod() 
        {
            return x;
        }
    }
}
class NestedClass 
{
    public static void main(String[] args) 
    {
        Outer obj1 = new Outer();
        Outer.Inner obj2 = obj1.new Inner();
        System.out.println(obj2.ReturnMethod());
    }
}

a) 10
b) Compilation error
c) Garbage Value
d) -1
View Answer

Answer: a
Explanation: An inner class or nested class is a class declared entirely within the body of another class. By using the object of inner class, the data members of both the inner and outer classes can be accessed (given that they are accessible by virtue of their access specifier). As the variable ‘x’ is returned from the inner function, it is called in the main() method.
Output:

advertisement
$ javac NestedClass.java
$ java NestedClass
10

4. What will be the output of the following Java code?

class Outer 
{
    int x = 10;
    class Inner 
    {
        public int ReturnMethod() 
        {
            return x;
        }
    }
}
class MainCall 
{
    public static void main(String[] args) 
    {
        Outer obj1 = new Outer();
        Outer.Inner obj2 = obj1.new Inner();
        System.out.println(obj1.ReturnMethod());
    }
}

a) 10
b) Compilation error
c) Garbage Value
d) -1
View Answer

Answer: b
Explanation: Java inner class or nested class is a class which is declared inside the class. Here, two objects obj1 and obj2 of classtypes Outer and Outer.Inner types are declared. The inner class can access the data members and members functions of the outer function using it’s object but vice versa is not possible. Method ReturnMethod() inside in the Inner class can’t be accessed using Outer class object.
Output:

$ javac MainCall.java
$ java MainCall
java.lang.RuntimeException: Uncompilable source code

5. What will be the output of the following Java code?

class MainClass
{
    int x = 15;
    class NestedClass 
    {
        int y = 15;
        void print()
        {
            System.out.println(x+y);
        }
    }
}
class Sum
{
    public static void main(String[] args) 
    {
        MainClass obj1 = new MainClass();
        MainClass.NestedClass obj2 = obj1.new NestedClass();
        obj2.print();
    }
}

a) 30
b) 15
c) Compilation error
d) Garbage Value
View Answer

Answer: a
Explanation: The concept of nested classes is used in the above program. Here, two objects obj1 and obj2 of classtypes MainClass and MainClass.NestedClass types are declared. The method print() of NestedClass is called using the object obj2 and hence is successfully executed.
Output:

$ javac Sum.java
$ java Sum
30

6. What will be the output of the following Java code?

class Outer
{
    int x = 15;
    class Inner 
    {
        int y = 15;
        int print()
        {
            return(x+y);
        }
    }
}
class SumReturn
{
    public static void main(String[] args) 
    {
        Outer obj1 = new Outer();
        Outer.Inner obj2 = obj1.new Inner();
        System.out.println(obj1.print());
    }
}

a) 15
b) Compilation error
c) Garbage Value
d) 30
View Answer

Answer: b
Explanation: An inner class or nested class is a class declared entirely within the body of another class. Here, Inner class is placed inside the Outer clas, hence it is a nested class. As the Inner class can access the data members and members functions of the Outer class, the return statement works if it is called using the object of the Inner class but it is called using the outer class. Therefore an error is thrown.
Output:

$ javac SumReturn.java
$ java SumReturn
Uncompilable source code

7. What will be the output of the following Java code?

class MainClass
{
    int x = 15;
    final int z=7;
    class NestedClass 
    {
        int y = 10;
        int Calc()
        {
            return(x*y*z);
        }
    }
}
class Product
{
    public static void main(String[] args) 
    {
        MainClass obj1 = new MainClass();
        MainClass.NestedClass obj2 = obj1.new NestedClass();
        System.out.println(obj2.Calc());
    }
}

a) 1050
b) 1500
c) Compilation error
d) Null
View Answer

Answer: a
Explanation: Inner class or nested class is a class which is declared inside the class. Here, NestedClass is declared inside the MainClass. The NestedClass method ‘Calc()’ is called using the object of NestedClass. Hence, the return statement is executed perfectly and a value is returned.
Output:

$ javac Product.java
$ java Product
1050

8. What will be the output of the following Java code?

class MainClass
{
    int x=5;
    protected int z=4;
    class NestedClass 
    { 
        int y = 10;
        void Calculate()
        {
            System.out.println(x*y*z);
        }
    }
}
class ProductCalc
{
    public static void main(String[] args) 
    {
        MainClass obj1 = new MainClass();
        MainClass.NestedClass obj2 = obj1.new NestedClass();
        obj2.Calculate();
    }
}

a) 1050
b) 1500
c) Compilation error
d) 200
View Answer

Answer: d
Explanation: The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable. An inner class can be private or protected. Protected access specifier allows the data members and member functions to be accessed in the classes present in the same package.
Output:

$ javac ProductCalc.java
$ java ProductCalc
200

9. What will be the output of the following Java code?

class Outer 
{
    protected int x = 10;
    class Inner 
    {
        final int y = 15;
    }
}
class Final
{
    public static void main(String[] args) 
    {
        Outer myOuter = new Outer();
        Outer.Inner myInner = myOuter.new Inner();
        System.out.println(myInner.y + myOuter.x);
    }
}

a) 25
b) 10
c) Compilation error
d) 150
View Answer

Answer: a
Explanation: The concept of nested classes is used in the above program. Here, two objects myOuter and myInner of classtypes Outer and Outer.Inner types are declared. Protected access specifier allows the data members and member functions to be accessed in the classes present in the same package.
Output:

$ javac Final.java
$ java Final
25

10. What will be the output of the following Java code?

class Outer 
{
    int x = 10;
    class Inner 
    {
        int y = 15;
        public void update()
        {
            y = y*11;
        }
    }
}
class Updation
{
    public static void main(String[] args) 
    {
        Outer myOuter = new Outer();
        Outer.Inner myInner = myOuter.new Inner();
        myInner.update();
        System.out.println(myInner.y + myOuter.x);
    }
}

a) 185
b) 175
c) Compilation error
d) 150
View Answer

Answer: b
Explanation: An inner class or nested class is a class declared entirely within the body of another class. The inner class Inner can access the data members and member functions of the outer class Outer in case of this program. The object myInner is used to call the update() function inside the Inner class. The value is updated and the value is printed.
Output:

$ javac Updation.java
$ java Updation
175

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.