Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interview questions and answers

1
What is Thread.sleep(int millisec) method in java?
  1. This function is used in a thread to sleep for that many milli seconds.
  2. The main purpose is, to give CPU time to other threads by moving our self(our thread) to sleep mode. 
    Once a thread goes to sleep mode CPU will automatically switches to other threads.
  3. option 2 is wrong because even though if a thread is sleeping still CPU will execute that thread.
  4. This function is used to send a request to other threads to sleep for that many milli seconds. So that current thread will get more CPU time.
Show Answer
Share Facebook gmail Twitter
2
What is the exception thrown by Thread.sleep(int milli) method? Why does Thread.sleep() method throws that exception?
  1. ThreadSleepException. It throws that exception because it is sleeping.
  2. InterruptedException. It throws that exception because it may be interrupted by other threads while sleeping.
  3. both option 1 and 2 are right.
  4. it won't throw any exception.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
What is the use of interrupt() function of Thread class?
  1. if we call this function on a thread, then it will be killed immediately.
  2. we call this function on a thread, to request it to stop what it is doing currently.
  3. if we call this function, then the calling thread will be suspended indefinitely.
  4. this function is used by OS to wake up a Thread which is in Sleep mode.
Show Answer
Share Facebook gmail Twitter
5
How to know if some other thread is interrupting our current thread?
  1. If your thread is sleeping, then it will throw InterruptedException to indicate that some body is interrupting your thread.
  2. We can use either Thread.interrupted() or isInterrupted() function which will return true if some other thread is interrupting our current thread.
  3. Unfortunately there is no facility for this.
  4. option 1 and option 2, both are correct.
Show Answer
Share Facebook gmail Twitter
6
What is the difference between Thread.interrputed() and isInterrupted() method of the Thread class?
  1. Both are same, there is no difference. Programmer can use any of them.
  2. Thread.interrupted() will clear the interrupt status, where as isInterrupted() will not clear the interrupt status.
  3. Thread.interrupted() will not clear the interrupt status, where as isInterrupted() will clear the interrupt status.
  4. none
Show Answer
Share Facebook gmail Twitter
7
"Extends Thread" versus "Implements Runnable",  which is preferred way to create a thread in java? And why?
  1. Extending Thread class is better, because doing that way is easy.
  2. Implementing Runnable Interface is better, because doing it is easy.
  3. Implementing Runnable Interface is better, because if you extend Thread class you can't extend other class.
  4. option 3 is wrong as Java supports multiple inheritance as well.
Show Answer
Share Facebook gmail Twitter
8
Assume that I have two threads thread1, and thread2. thread2 is sleeping and thread1 is trying to interrupt thread2. Now what will happen to thread1 which is sleeping?
  1. Nothing will happen, thread1 continues to sleep.
  2. thread2 will get suspended and will be moved to blocked state.
  3. thread1 will be interrupted and gets InterruptedException and it comes out of sleep mode.
  4. thread1 will get InterruptedException and immediately thread2 will go to sleep mode.
Show Answer
Share Facebook gmail Twitter
9
How to handle interrupts in a thread?
  1. Generally it is up to the programmer what to do when some body interrupts you, but it is quite common to terminate the thread when somebody interrupts you.
  2. if some one interrupts your thread, it is mandatory to go to sleep mode so that the thread which is interrupting will get some CPU time.
  3. if some one interrupts your thread, then you don't need to worry, you just continue what you are doing now.
  4. none of the above
Show Answer
Share Facebook gmail Twitter
10
What is the use of join() method in Thread class?
  1. join() method is used to club two threads into a single thread.
  2. if you call join() method on a thread, then the current thread will be paused until the other thread is finished.
  3. if you call join() method on a thread, then you are sending a request to other thread to get paused until current thread is finished.
  4. none of the above
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
12
Example program for Java threads join() method:
What is below Java program doing?
class Thread1 extends Thread{
        public void run(){
                for(int i=0; i<1000; i++){
                        System.out.println("Thread 1.."+i);
                }
        }
}
class Thread2 implements Runnable{
        Thread temp;
        public Thread2(Thread temp){
                this.temp = temp;
        }
        @Override
        public void run() {
                for(int i=1000; i>=0; i--){
                        System.out.println("Thread 2.."+i);
                        if(i==800){
                                try {
                                        temp.join();
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }                
        }
}
public class ThreadDemo {
        public static void main(String[] args) {
                Thread1 t = new Thread1();
                Thread2 u = new Thread2(t);
                Thread z = new Thread(u);
                t.start();
                z.start();
        }
}
  1. This program will not run, it gives compile time error.
  2. In this program first thread is printing from 0 to 999 and second thread is printing from 1000 to 1. The order in which these two threads will be printing can't be predictable. But once thread 2 reaches 800, CPU will allocate its complete time to thread 2 only. Once thread 2 finishes its work, then only thread 1 will start printing remaining numbers.
  3. In this program first thread is printing from 0 to 999 and second thread is printing from 1000 to 1. The order in which these two threads will be printing can't be predictable. But once thread 2 reaches 800, it is requesting CPU to first go and finish thread1 and then thread2. So CPU will allocate its complete time to thread 1 only. Once thread 1 finishes its work, then only thread 2 will start printing remaining numbers.
  4. In this program first thread is printing from 0 to 999 and second thread is printing from 1000 to 1. The order in which these two threads will be printing can't be predictable. But once thread 2 reaches 800, it is requesting CPU to join both the threads into a single thread. So from here, both thread 1 and thread 2 runs in same thread.
Show Answer
Share Facebook gmail Twitter
13
Sample program on java threads sleep() method and interruptedexception:
What is the output below Java program?
class MyThread1 extends Thread{
        public void run(){
                for(int i=0; i<1000; i++){
                        if(i%100 == 0 && i != 0){
                                try {
                                        Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        System.out.println("Thread1.."+i);
                }
        }
}
public class ThreadDemo1 {
        public static void main(String[] args) {
                MyThread1 m = new MyThread1();
                m.start();
                for(int j=1000; j>0; j--){
                        System.out.println("Thread2.."+j);
                }
        }
}
  1. The program is misleading as we are printing Thread2..in main function. But there is no thread in main function. So we can't predict the behavior.
  2. Thread1 is printing from 0 to 999, where as main thread(thread2) is printing from 1000 to 1. We can't predict the order till thread1 reaches 100. Once thread1 reaches 100, it will go to sleep for 1000 seconds. So automatically Cpu will go to thread2 ( mainthread) and finishes rest of the functionality of thread2. Once thread1 comes out of sleep mode, cpu will do the rest of the work of thread1 as well. Thread1 will sleep for 1000 seconds only once.
  3. Thread1 is printing from 0 to 999, where as main thread(thread2) is printing from 1000 to 1. We can't predict the order till thread1 reaches 100. Once thread1 reaches 100, it will go to sleep for 1 second. So autmoatically Cpu will go to thread2 ( mainthread) and finishes rest of the functionality of thread2. Once thread1 comes out of sleep mode, cpu will do the rest of the work of thread1 as well. Thread1 will sleep for 1 second only once.
  4. Thread1 is printing from 0 to 999, where as main thread(thread2) is printing from 1000 to 1. We can't predict the order till thread1 reaches 100. Once thread1 reaches 100, it will go to sleep for 1 second. So autmoatically Cpu will go to thread2 ( mainthread) and finishes rest of the functionality of thread2. Once thread1 comes out of sleep mode, cpu will do the rest of the work of thread1 as well. Thread1 will sleep for 1 second at every 100 intervals once.
Show Answer
Share Facebook gmail Twitter
14
How many times below thread will go to sleep mode?
class MyThread1 extends Thread{
        public void run(){
                for(int i=0; i<1000; i++){
                        if(i%100 == 0 && i != 0){
                                try {
                                        Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        System.out.println("Thread1.."+i);
                }
        }
}
  1. This thread will go to sleep for only 1 time, for the first time when it starts its for loop.
  2. we can't predict.
  3. This thread will go to sleep for 9 times, at every 100 intervals once till it reaches 999.
  4. This thread will go to sleep for 10 times, at every 100 intervals once till it reaches 999.
Show Answer
Share Facebook gmail Twitter
15
Sample java program for Thread interrupt() and interrupted() methods:
What is the output of below Java program?
class MyThread2 extends Thread{
        public void run(){
                for(int i=0; i<1000; i++){
                        System.out.println("Thread2.."+i);
                        if(Thread.interrupted() == true){
                                System.out.println("Interrupted");
                                if(i>=900){
                                        return;
                                }
                        }
                }
        }
}

public class ThreadDemo2 {
        public static void main(String[] args) {
                MyThread2 m = new MyThread2();
                m.start();
                for(int j=1000; j>0; j--){
                        System.out.println("Main thread.."+j);
                        if(j==500){
                                m.interrupt();
                        }
                }
        }
}
  1. class Thread2 is printing from 0 to 999. Main method or main thread is printing from 1000 to 1. Once main thread reaches 500, it is interrupting thread2.  In each for loop thread2 is checking if some one is interrupting him or not. If some one is interrupting him and if he has finished his 90% of work, then he is closing his thread, else he is continuing his work (irrespective of whether others interrupts him or not).
  2. class Thread2 is printing from 0 to 999. Main method or main thread is printing from 1000 to 1. Once main thread reaches 500, it is interrupting thread2.  In each for loop thread2 is checking if some one is interrupting him or not. If some one is interrupting him and if he has finished his 90% of work, then he is continuing his work, else he is closing his thread (irrespective of whether others interrupts him or not).
  3. class Thread2 is printing from 0 to 999. Main method or main thread is printing from 1000 to 1. Once main thread reaches 500, it is interrupting himself until thread2 finishes its work.  In each for loop thread2 is checking if some one is interrupting him or not. If some one is interrupting him and if he has finished his 90% of work, then he is going to blocked state, else he is continuing his work (irrespective of whether others interrupts him or not).
  4. class Thread2 is printing from 0 to 999. Main method or main thread is printing from 1000 to 1. Once main thread reaches 500, it is interrupting himself until thread2 finishes its work.  In each for loop thread2 is checking if some one is interrupting him or not. If some one is interrupting him and if he has finished his 90% of work, then he is closing his thread, else he is going to blocked state(irrespective of whether others interrupts him or not).
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
17
Example on how to set priority to java threads:
What is the output of Java program?
class MyThrd implements Runnable{
        @Override
        public void run() {
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo4 {
        public static void main(String[] args) {
                MyThrd m = new MyThrd();
                Thread t = new Thread(m);
                t.setPriority(11);
                t.start();
        }
}
  1. it prints from 0 to 9
  2. compile time error, you can't set a thread priority more than 10 and less than 1.
  3. run time exception. IllegalargumentException as we are supposed to pass any value between 1 to 10  (including) to setpriority function of thread.
  4. it prints from 0 to 10
Show Answer
Share Facebook gmail Twitter
18
Java Thread priorities example:
How many threads are running in below Java program, and what are their priorities?
class MyThrd implements Runnable{
        @Override
        public void run() {
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo4 {
        public static void main(String[] args) {
                MyThrd m = new MyThrd();
                Thread t = new Thread(m);
                t.start();
        }
}
  1. only one thread is running t, and its priority is normal(5)
  2. two threads are running m,t. Their priorities are normal(5)
  3. two threads are running t, and main thread. their priorities are normal(5).
  4. two threads are running t, and main thread. their priorities are normal(5) for t, and for main thread it is max_priority(10).
Show Answer
Share Facebook gmail Twitter
19
What is the difference between wait and sleep of Thread class in Java?
  1. sleep(int millisec) method is used to move a thread into sleep mode (suspended) for that many milli seconds, so that other threads will get CPU time to get executed. Wait() method will also be used to move a thread to suspended state, But the thread which is moved to suspended state by calling wait() will not resume unless some other thread calls notify() or notifyall() on that thread.
  2. wait() releases the lock or monitor where as sleep() doesn't releases any lock or monitor while waiting. 
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
20
How to stop a thread?
  1. Use stop() method of Thread class to kill or stop a thread.
  2. The 3 methods provided by the Thread class for interrupting a thread
    public void interrupt()
    public static boolean interrupted()
    public boolean isInterrupted()
    
    Example of interrupting a thread that stops working
    
    In this example, after interrupting the thread, we are propagating it,
     so it will stop working. If we don't want to stop the thread, we can handle it where sleep()
     or wait() method is invoked. Let's first see the example where we are propagating the exception. 
    
    class A extends Thread{  
    public void run(){  
    try{  
    Thread.sleep(1000);  
    System.out.println("task");  
    }catch(InterruptedException e){  
    throw new RuntimeException("Thread interrupted..."+e);  
    }  
      
    }  
      
    public static void main(String args[]){  
    A t1=new A();  
    t1.start();  
    try{  
    t1.interrupt();  
    }catch(Exception e){System.out.println("Exception handled "+e);}  
      
    }  
    }  
    
  3. Use kill() method of Thread class to kill or stop a thread which is currently running.
  4. just return from run() method of thread class, to kill or stop that currently running thread.
Show Answer
Share Facebook gmail Twitter
Read more papers :