Tuesday, 1 May 2012

JAVA interview questions 33

Q321.Explain about Thread group? 
Ans. Every Java thread is a member of a thread group. Thread groups provide a mechanism for 
collecting multiple threads into a single object and manipulating those threads all at once, rather 
than individually. For example, you can start or suspend all the threads within a group with a 
single method call. Java thread groups are implemented by the ThreadGroup api class in the java.lang package. 

Q322.What is the Thread Local? 
Ans. It's a way for each thread in multi-threaded code to keep its own copy of an instance 
variable. Generally, instance variable are shared between all threads that use an object; ThreadLocal is a way for each thread to keep its own copy of such a variable. The purpose might be that each thread keeps different data in that variable, or that the developer wants to avoid the overhead of synchronizing access to it. 

Q323. What is inner class and when we should go for inner classes? 
Some times we can declare a class inside another class such type of classes are called inner classes 
Example 
Class Car{ 
//more code here 
Class Engine{ 
//more code here } 

Without existing Car object there is no chance of existing Engine object, hence Engine class has declared inside Car class. 

Q324.How many types of inner classes are present? 
There are four types of inner classes are present 
o Normal or regular inner class o Method local inner class o Anonymous inner class o Static nested class 

Q325.What is method local inner class? 
Sometimes we can declare a class inside a method such type of classes are called method local 
inner classes 
The main purpose of method local inner classes is to define method specific functionality The scope of method local inner classes is the scope of the method where it is declared. This is the mostly rarely used type of inner classes. 
Example 
class Test{ 
public void m1(){ 
class Inner { 
public void sum(int I,int j){
System.out.println(i+J);
}//sum
}//inner 
Inner i=new Inner(); 
i.sum(10,20); 
//more code here 
I.sum(100,303); 
//more code here 
i.sum(102,84); 
}//m1() 
Public static void main(){ 
New Test().m1(); 



Q326.What is anonymous inner class? 
Some times we can declare a inner class without name such type of inner classes are called 
Anonymous inner classes 
Anonymous inner classes can be divided into 3 categories 
§ Anonymous inner class that extends a class 
§ Anonymous inner class that implements an interface 
§ Anonymous inner class that defines inside a method argument 
ANONYMOUS INNER CLASS THAT EXTENDS A CLASS Example 
Class popcorn{ 
Public void taste(){ 
System.out.println(“it is salty”);
}
//more code here
}
Class Test{ 
Public static void main(String[] args) 

Popcorn p=new Popcorn() 
{ // here we are creating child class for popcorn 
Public void taste(){
System.out.println(“it is sweet”);
}
};//here semicolon indicates we r creating child class object with parent 
// class reference here child class dosent contain name 
p.taste()// it is sweet 
Popcorn p=new Popcorn(); 
p1.taste() //it is salty 


ANONYMOUS INNER CLASS THAT IMPLEMENTS AN INTERFACE example 
class Test{ 
Public static void main(String[] args){ 
Runnable r=new Runnable(){ 
Public void run(){ 
for(int i=0;i<10;i++){ 
System.out.printin(“child thread”); 
}
}
}; 
Thread t=new Thread(r); 
t.start(); 
for(int i=0;i<10;i++){ 
System.out.printin(“main thread”); } 


Don’t become fool that here we are creating object of interface Runnable.Here we are actually 
creating an object of class that is implemented Runnable interface. 

Q327.What is static nested calss?why the term nested instead of inner in static nested class? 
Some times we can declare inner class with static modifier such type of inner class are called static 
nested classes.the term nested instead of static because without existing outer class object inner 
class object can exist. 
Example 
Class outer{ 
Static class Nested{ 
Public static void main(String[] args){ 
System.out.println(“nested class main()”); } 

Public static void main(String[] args){ 
System.out.println(“outer class main()”); } 

Java Outer 
O/P 
Outer class main() Java Outer$Nested 
O/P 
Nested class main() 

Q328. Inside inner class is it possible to declare main()? 
No it is not possible to declare main () inside inner class but in static nested class it is possible for 
Example refer above code 

Q329. What are limitations of object Arrays? 
The main limitations of Object arrays are 
* These are fixed in size ie once we created an array object there is no chance of increasing or decreasing size based on our requirement. Hence If we don’t know size in advance , arrays are not recommended to use 
* Arrays can hold only homogeneous elements. 
* There is no underlying data structure for arrays and hence no readymade method support for arrays. Hence for every requirement programmer has to code explicitly 
To over come these problems collections are recommended to use. 

Q330. What is Collection API ? 
It defines set of classes and interfaces which can be used for representing a group of objects as single entity 

No comments:

Post a Comment