Java OCA OCP Practice Question 2991

Question

Consider the following program:

class Outer {/*w  w w .  j  a v  a 2s.  com*/
    class Inner {
        public void print() {
            System.out.println("Inner: print");
        }
    }
 }

class Main {
    public static void main(String []args) {
        // Stmt#1
        inner.print();
    }
 }

Which one of the following statements will you replace with //Stmt#1 to make the program compile and run successfully to print "Inner: print" in console?.

  • a) Outer.Inner inner = new Outer.Inner();
  • b) Inner inner = new Outer.Inner();
  • c) Outer.Inner inner = new Outer().Inner();
  • d) Outer.Inner inner = new Outer().new Inner();


d)

Note

option d) uses the correct syntax for instantiating outer and Inner classes.

the other three options will result in compiler error(s).




PreviousNext

Related