Object Oriented Programming using Java Questions and Answers – Nested Class

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

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

class Outer
{
    int y = 10;
    class Nested
    {    
        int y = 15;
    }
}
class Prog
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        System.out.println(i.y);
        System.out.println(j.y);
 
    }
}

a)

15
15

b)

advertisement
advertisement
10
10

c)

10
15

d)

15
10
View Answer
Answer: c
Explanation: The object ‘i’ is an object of the outer class, hence it fetches the value of y from the outer class, which is 10. The object j is an object of the nested class, therefore it fetches the value of y from the nested class, which is 15.
Output:

$ javac Prog.java
$ java Prog
10
15

advertisement

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

advertisement
class Outer
{
    void print()
    {
        System.out.println("Outer");
    }
    class Nested
    {
        void print()
        {
            System.out.println("Nested");
        }
    }
}
class MainCall
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        i.print();
        j.print();        
    }
}

a)

Outer    
Inner

b)

Inner    
Outer

c)

Outer 
Outer

d)

Inner 
Inner
View Answer
Answer: a
Explanation: In the main function, the objects i and j are the objects of the classes Outer and Nested respectively. They call their respective print functions in order as mentioned in the above program.
Output:

$ javac MainCall.java
$ java MainCall
Outer
Inner

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

class Outer
{
    int x = 98;
    class Nested
    {
        void Display()
        {
            System.out.println((char)x);
        }
    }
}
class ProgCall
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        j.Display();
    }
}

a) 98
b) B
c) b
d) Compilation Error
View Answer

Answer: c
Explanation: The function Display() is called by the object j of the nested class. Since it is an object of the inner class, it can access members of the outer class. Since the ASCII equivalent of 98 is the character ‘b’, it is printed in the Display() function.
Output:

$ javac ProgCall.java
$ java ProgCall
b

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

class Outer
{
    int x = 98;
    class Nested
    {
        void print()
        {
            System.out.println(this);
        }
    }
}
class MainExecution
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        j.print();
    }
}

a) x=98
b) x = 98
c) Outer$Nested@2a139a55
d) Nested@2a139a55
View Answer

Answer: c
Explanation: The ‘this’ keyword refers to the object calling the function. When it is printed, it is printed in the format ClassName@HashCode. Although here the class name is Nested, it is a nested class of the Outer class, hence it is referred to as Outer$Nested.
Output:

$ javac MainExecution.java
$ java MainExecution
Outer$Nested@2a139a55

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

class Outer
{
    int x = 10;
    class Nested
    {
        int y = 20;
        class Nested2
        {
            int z = 30;
            void multiply()
            {
                System.out.println(x*y*z);
            }
        }
    }
}
class Call
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        Outer.Nested.Nested2 k = j.new Nested2();
        k.multiply();
    }
}

a) Runtime Error
b) Compilation Error
c) 3000
d) 6000
View Answer

Answer: d
Explanation: The innermost class Nested2, can access all elements of the outer classes, and hence the multiply function multiplies all the three values ‘x’, ‘y’ and ‘z’. Hence, the value of 6000(20*30*10) is printed.
Output:

$ javac Call.java
$ java Call
6000

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

class Outer
{
    class Nested
    {
        int y;
        void factorial()
        {
            int fact=1;
            for(int i=1;i<=y;i++)
            {
                fact*=i;
            }
            System.out.println(fact);
        }
    }
}
class Factorial
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        j.factorial();
    }
}

a) Garbage value
b) 1
c) 0
d) Compilation Error
View Answer

Answer: b
Explanation: Here there is no separate value given to the variable ‘y’ by the user and when an object is being created, the default constructor initializes all the integer values to 0, hence the value of y is set to 0. The factorial of 0 is 1 mathematically.
Output:

$ javac Factorial.java
$ java Factorial
1

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

class Outer
{
    class Nested
    {
        int y;
        void fact()
        {
            int fact=1;
            for(int i=1;i<=y;i++)
            {
                fact*=i;
            }
            System.out.println(fact);
        }
        Nested(int y)
        {
            this.y=y;
        }
    }
}
class OuterCall
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested(5);
        j.fact();
    }
}

a) 120
b) 240
c) 24
d) 1
View Answer

Answer: b
Explanation: When the object j is created, it calls the constructor, which sets the value of y to 5. The for loop inside the Nested class is executed as the condition is true until ‘i’ becomes 5. Factorial of 5 is 120.
Output:

$ javac OuterCall.java
$ java OuterCall
120

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

class Outer
{
    int x=10;
    static class Nested
    {
        int y = 10;
        static void Calc()
        {
            System.out.println(x+y);
        }
    }
}
class Calculation
{
    public static void main(String[] args) 
    {
        Outer.Nested.Calc();
    }
}

a) 10
b) 20
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: The class Nested and the function Calc are static as indicated by the ‘static’ keyword, therefore they cannot use non-static variables x and y. Static functions must use static variables only. Therefore, a compilation error.
Output:

$ javac Calculation.java
$ java Calculation
error: non-static variable x cannot be referenced from a static context
error: non-static variable y cannot be referenced from a static context

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

class Outer
{
    String s = "Sanfoundry";
    class Nested
    {
        void pos(int a)
        {
            System.out.println(s.charAt(a));
        }
    }
}
class NestedCall
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        j.pos(3);
    }
}

a) n
b) a
c) f
d) Compilation Error
View Answer

Answer: a
Explanation: The charAt function returns the character at the given index in the string that calls it. As indexing starts from 0, 3 corresponds to the 4th character in the string and therefore the character ‘n’ is printed after execution.
Output:

$ javac NestedCall.java
$ java NestedCall
f

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

class Outer
{
    class Nested
    {
        String s = "Sanfoundry";
        void index(char c)
        {
            System.out.println(s.indexOf(c));
            System.out.println(s.indexOf(c,3));
        }
    }
}
class Index
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested j = i.new Nested();
        j.index('n');
    }
}

a)

2
2

b)

3
7

c)

3
3

d)

2
6
View Answer
Answer: d
Explanation: The indexOf function returns the index of the character passed in the string calling it. If an integer is passed along with the character, then the character is searched starting from that index.
Output:

$ javac Index.java
$ java Index
2
6

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

class Outer
{
    class Nested
    {
        String s = "Dravid";
        void display()
        {
            System.out.println(s.charAt(2));
        }
    }
}
class CharAt
{
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Nested obj = i.new Nested();
        obj.display();
    }
}

a) a
b) d
c) r
d) Compilation error
View Answer

Answer: a
Explanation: Here, Nested is an inner class of the class Outer which has a string that is used for string manipulation. charAt() function is used to find a particular character present in the index provided by the user.
Output:

$ javac CharAt.java
$ java CharAt
a

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

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

a) 40
b) 10
c) 80
d) Compilation error
View Answer

Answer: a
Explanation: In Java, inner class or nested class is a class which is declared inside the class or interface. In the above program, NestedClass is inside the MainClass and NestedClass uses the data members of the outerclass. Hence the output is printed without any errors.
Output:

$ javac Product.java
$ java Product
40

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.