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 

JAVA interview questions 32

Q311. What is race condition? 
Ans. Multiple Threads are accessing simultaneously and causing data inconsistency problem is called race condition, we can resolve this by using synchronized keyword. 

Q312. What is Daemon Thread? And give an example? 
Ans. The Threads which are running in the background are called Daemon Thread. 
Example: Garbage collector. 

Q313. What is the purpose of a Daemon Thread? 
Ans. The main purpose of Daemon Threads is to provide support for non-daemon Threads. 

Q314. How we can check Daemon nature of a Thread? 
Ans. We can check Daemon nature of a Thread by using isDaemon() method. 

Q315. Is it possible to change a Daemon nature of a Thread? 
Ans. Yes, we can change Daemon nature of a Thread by using setDaemon() method. 

Q316. Is main thread is Daemon or non-daemon? 
Ans. By default main thread is always non-daemon nature. 

Q317. Once we created a new thread is it daemon or non-daemon. 
Ans. Once we created a new Thread, The Daemon nature will be inheriting from parent to child. If the parent is Daemon the child is also Daemon and if the parent is non-daemon then child is also non-daemon. 

Q318. After starting a thread is it possible to change Daemon nature? 
Ans. We can change the Daemon nature before starting the Thread only. Once Thread started we are not allow to change Daemon nature otherwise we will get RuntimeException sying IllegalThreadStateException. 

Q319. When the Daemon thread will be terminated? 
Ans. Once last non-daemon Thread terminates automatically every Daemon Thread will be terminated. 

Q320. What is green Thread? 
Ans. A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent 
threads, the JVM manages multi-threading internally rather than using other operating system threads. 
There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations.

JAVA interview questions 31

Q301.If a waiting thread got notification then it will entered into which state? 
Ans. It will entered into another waiting state to get lock. 

Q302.In which method threads can release the lock? 
Ans. Once a Thread calls wait() method it immediately releases the lock of that object and then entered into waiting state similarly after calling notify() method Thread releases the lock but may not immediately. Except these three methods( wait(), notify(), notifyAll() ) method Thread neverreleases the lock anywhere else. 

Q303. Explain wait(), notify(), notifyAll() method uses. 
Ans. Two Threads will communicate with each other by using wait(), notify() or notifyAll() methods. 
These methods are defined in Object class but not in Thread because Threads are calling this method. 

Q304. What is the difference between notify() and notifyAll()? 
Ans. To give notification to the single waiting Thread. We use notify() method and to give notification to all waiting thread we use notifyAll() method. 

Q305. Once a Thread got the notification then which waiting thread will get chance? 
Ans. It is depends on the Thread Scheduler. 

Q306. How a thread can interrupt another thread? 
Ans. A Thread can interrupt another Thread by using interrupt() method. 

Q307. Which keyword causes DeadLock situation? 
Ans. Synchronized keyword is the thing to causes of DeadLock. If we are not using properly synchronized keyword the program will entered into DeadLock situation. 

Q308. How we can stop a thread explacitly? 
Ans. Thread class defines stop() method by using this method we can stop a Thread. But it is deprecated. And hence not recommended to use. 

Q309. Explain about suspend() and resume() method? 
Ans. A Thread can suspend another Thread by using suspend() method. 
A Thread can resume a suspended Thread by using resume() method. 

Q310.What is Starvation()? And Explain the difference between Deadlock and Starvation? 
Ans. A long waiting Thread is said to be in starvation (because of least priority) but after certain time defiantly it will get the chance for execution. But in the case of Deadlock two Threads will wait for each other forever. It will never get the chance for execution.