Advertisement

Udemy WW Udemy WW

Friday 2 December 2011

Interview Question On Core Java part-1

Q1) What is an inner class?
Ans:class defined inside class or interfaces 
Q2) What are the different types of inner classes?
Ans:
static nested class
non static inner class
local inner class
anonymous inner class
Q3) can a static nested class have access to  non-static methods or instance variables of outer class?
Ans:
No,it can access only static member of  outer class
Q4)What are the advantages of Inner classes?
Ans:
Advantage of inner class is that they can be hidden from the other classes in the same package and still have the access to all the members (private also) of the enclosing class.
The advantage of using static nested class is that to instantiate a static nested class we need not create an instance of the enclosing class(outer class) which reduces the number of objects the application creates at run time.
Q5) If you compile java program containing inner  class how many .class files are created ?
Ans)  one .class file for each inner class an a .class file for the outer class is created. e.g.
class Outer {
    class Inner{ }
 }
 If we compile the above code with command
% javac Outer.java
Two files
Outer.class 
Outer$Inner.class
will be created.
Q6) what is the output if we compile and execute following code in java?
 int i=0;
while(1)
{
i++;
System.out.println("helllo");
if(i==5)
break;
}


Ans:
 compile time Error
Incompatible type
found: int
required: boolean

Q6) by default what access a method in interface have?
Ans:
A method in an interface by default have public access.


Q7) What would be the name of local inner class defined inside Outer class?
Ans:
outer$Ninner
where N can be 1,2,3,4.......
for example
outer$1inner

Q8) What would be the name of  anonymous inner class?
Ans:
$1,$2 
Q9) If Two method have the local inner class with the same name (eq Inner) then what will be the name of .class file correspond to above two inner classes?
Ans: Outer$1Inner and Outer$2Inner






1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Advertisement

Udemy WW