Time Remaining :00:00:00

Questions to be answered 15

Java Fundamental Programming Puzzles - Inner Classes



Question No : 1

What is the output of following program?

public class Outer {
    private int a = 7;
    class Inner {
        public void displayValue() {
            System.out.println("Value of a is " + a);
        }
    }
}

public class Test {
    public static void main(String[] args) throws Exception {
        Outer mo = new Outer();
        Outer.Inner inner = mo.new Inner();
        inner.displayValue();
    }
}

Question No : 2

What is the output of following program?

public class Tech {
    public void tech() {
        System.out.println("Tech");
    }
}

public class Atech {
    Tech a = new Tech() {
        public void tech() {
            System.out.println("anonymous tech");
        }
    };
    public void dothis() {
        a.tech();
    }
    public static void main(String... args) {
        Atech atech = new Atech();
        atech.dothis();
    }
}

Question No : 3

What is the output of following program?

public class Outer {
	private String x = "Outer variable";
	void doStuff() {
	    String z = "local variable";
	    class Inner {
	        public void seeOuter() {
	            System.out.println("Outer x is " + x);
	            System.out.println("Local variable z is " + z);  
	        } 
	    }   
	}  
}

Question No : 4

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 class MyInner?

Question No : 5

class Foo 
{
    class Bar{ }
}
class Test 
{
    public static void main (String [] args) 
    {
        Foo f = new Foo();
        /* Line missing */
    }
}

Which statement if inserted as line 10, creates an instance of Bar?

Question No : 6

Which constructs an anonymous inner class instance?

Question No : 7

Which is true about an anonymous inner class?

Question No : 8

Which will create an anonymous inner class from within Bar?

class Boo 
{
    Boo(String s) { }
    Boo() { }
}
class Bar extends Boo 
{
    Bar() { }
    Bar(String s) {super(s);}
    void zoo() 
    {
    // insert code here
    }
}

Question No : 9

public class TestObj {
    public static void main(String[] args) {
        Object o = new Object() {
            public boolean equals(Object obj) {
                return true;
            }
        }
        System.out.println(o.equals("Fred"));
    }
}

What is the result?

Question No : 10

public class HorseTest {
    public static void main(String[] args) {
        class Horse {
            public String name;
            public Horse(String s) {
                name = s;
            }
        }
        Object obj = new Horse("Zippo");
        System.out.println(obj.name);
    }
}

What is the result?

Question No : 11

public abstract class AbstractTest {
    public int getNum() {
        return 45;
    }
    public abstract class Bar {
        public int getNum() {
            return 38;
        }
    }
    public static void main(String[] args) {
        AbstractTest t = new AbstractTest() {
            public int getNum() {
                return 22;
            }
        };
        AbstractTest.Bar f = t.new Bar() {
            public int getNum() {
                return 57;
            }
        };
        System.out.println(f.getNum() + " " + t.getNum());
    }
}

What is the result?

Question No : 12

public class Tour {
    public static void main(String[] args) {
        Cathedral c = new Cathedral();
        // insert code here
        s.go();
    }
}
class Cathedral {
    class Sanctum {
        void go() { System.out.println("spooky"); }
    }
}

Which, inserted independently at line 4, compile and produce the output "spooky"? (Choose all that apply.)

Question No : 13

class A { void m() { System.out.println("outer"); } }
public class TestInners {
    public static void main(String[] args) {
        new TestInners().go();
    }
    void go() {
    new A().m();
    class A { void m() { System.out.println("inner"); } }
}
class A { void m() { System.out.println("middle"); } }
}

What is the result?

Question No : 14

public class City {
    class Manhattan {
        void doStuff() throws Exception { System.out.print("x "); }
    }
    class TimesSquare extends Manhattan {
        void doStuff() throws Exception { }
    }
    public static void main(String[] args) throws Exception {
        new City().go();
    }
    void go() throws Exception { new TimesSquare().doStuff(); }
}

What is the result?

Question No : 15

public class Navel {
    private int size = 7;
    private static int length = 3;
    public static void main(String[] args) {
        new Navel().go();
    }
    void go() {
        int size = 5;
        System.out.println(new Gazer().adder());
    }
    class Gazer {
        int adder() { return size * length; }
    }
}

What is the result?


Planning to do an MBA?
A quick all-in-one MBA entry manual for MBA Aspirants. Book covers
  • Possible MBA Tests & Exam Preparation
  • Tips to choose right MBA Program
  • Essay, Resume & Letter of Recommendation
  • MBA Interview Preparation
  • MBA Financial Planning
Price - 6.99$
 

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know