Inner Classes Questions and Answers
In Java Programming, Inner Classes allow you to define a class within another class, enhancing encapsulation and code readability. This topic frequently appears in TCS, Accenture, and Infosys Java interview questions. In this section on Inner Classes questions with answers, you’ll explore member inner classes, static nested classes, anonymous inner classes, and local inner classes. Practicing these Java programming MCQs helps you understand how nested classes interact with outer classes and how they improve object-oriented design. Detailed explanations provided with each question will strengthen your grasp on Java scope, access modifiers, and real-world use cases — making it perfect for technical interview preparation.
Inner Classes
Showing 10 of
27 questions
1. Which is true about an anonymous inner class?
- It can extend exactly one class and implement exactly one interface.
- It can extend exactly one class and can implement multiple interfaces.
- It can extend exactly one class or implement exactly one interface.
- It can implement multiple interfaces regardless of whether it also extends a class.
2. class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } which one create an anonymous inner class from within class Bar?
- Boo f = new Boo(24) { };
- Boo f = new Bar() { };
- Bar f = new Boo(String s) { };
- Boo f = new Boo.Bar(String s) { };
3. Which is true about a method-local inner class?
- It must be marked final.
- It can be marked abstract.
- It can be marked public.
- It can be marked static.
4. Which statement is true about a static nested class?
- You must have a reference to an instance of the enclosing class in order to instantiate it.
- It does not have access to nonstatic members of the enclosing class.
- It's variables and methods must be static.
- It must extend the enclosing class.
5. Which constructs an anonymous inner class instance?
- Runnable r = new Runnable() { };
- Runnable r = new Runnable(public void run() { });
- Runnable r = new Runnable { public void run(){}};
- System.out.println(new Runnable() {public void run() { }});
6. class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ } } which statement, inserted at line 10, creates an instance of Bar?
- Foo.Bar b = new Foo.Bar();
- Foo.Bar b = f.new Bar();
- Bar b = new f.Bar();
- Bar b = f.new Bar();
7. public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?
- MyOuter.MyInner m = new MyOuter.MyInner();
- MyOuter.MyInner mi = new MyInner();
- MyOuter m = new MyOuter(); MyOuter.MyInner mi = m.new MyOuter.MyInner();
- MyInner mi = new MyOuter.MyInner();
8. What is the main advantage of using inner classes in Java?
- They reduce memory usage
- They make code run faster
- They allow access to private members of outer class and help in logical grouping
- They prevent method overriding
9. Which type of inner class can have static members?
- Local inner class
- Anonymous inner class
- Static nested class
- Member inner class
10. What is an anonymous inner class?
- A class with no name declared and instantiated at the same time
- A class that cannot be instantiated
- A class that has only private methods
- A class that extends Object class only