Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

class Foo { class Bar { } } class Test { public static void main (String [] args

ID: 3544960 • Letter: C

Question

 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?


A. Foo.Bar b = new Foo.Bar(); B. Foo.Bar b = f.new Bar(); C. Bar b = new f.Bar(); D. Bar b = f.new Bar();
  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?


A. Foo.Bar b = new Foo.Bar(); B. Foo.Bar b = f.new Bar(); C. Bar b = new f.Bar(); D. Bar b = f.new Bar();

Explanation / Answer

Option B


Explanation: Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class.


Option A, C and D all use incorrect syntax.


A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new.


C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong.


D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.