Inner Classes Questions and Answers
Practice ModeShowing 10 of 27 questions
Q1
Which is true about an anonymous inner class?
Answer: Option C
Q2
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?
Answer: Option B
Q3
Which is true about a method-local inner class?
Answer: Option B
Q4
Which statement is true about a static nested class?
Answer: Option B
Q5
Which constructs an anonymous inner class instance?
Answer: Option D
Q6
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?
Answer: Option B
Q7
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?
Answer: Option A
Q8
What is the main advantage of using inner classes in Java?
Answer: Option C
Explanation: Inner classes can access private members of outer class and help in logical grouping of classes.
Q9
Which type of inner class can have static members?
Answer: Option C
Explanation: Static nested classes can have static members, while other inner classes cannot.
Q10
What is an anonymous inner class?
Answer: Option A
Explanation: Anonymous inner classes are declared and instantiated at the same time without a name.