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 threads interview questions and answers

1
What is a Thread in java?
  1. Thread is a light weight process. (Theoretically). Practically it is part of a process.
  2. Thread is a dispatchable unit to the CPU.
  3. both are correct
  4. both are wrong
Show Answer
Share Facebook gmail Twitter
2
Is a process and thread both are same?
  1. Yes both are same.
  2. No, both are different. Threads are part of a process. A process may contain multiple threads in it.
  3. No, both are different. Process is a part of thread. A thread can contain only one process.
  4. No, both are different. In face there is no relation between a process and a thread.
Show Answer
Share Facebook gmail Twitter
3
What is multi tasking?
  1. Having more than one program or process in the memory is called as multi tasking.
  2. A CPU sharing its time among all the processes or programs in the memory, by time slicing is called as multi tasking.
  3. two CPUs running two programs simultaneously is called as multi tasking.
  4. all options are true.
Show Answer
Share Facebook gmail Twitter
4
Which of the below statements are false about a process and a thread?
  1. A process should have at least one thread to run.
  2. A process can exist with out at least one thread.
  3. A thread should exist with in a process.
  4. Even if programmer don't create any thread programmatically, Operating System will create a main thread for every application or process. Which is called as main thread.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
6
If a program is having 2 functions with 2 threads, does it mean CPU will run both the functions simultaneously in two different CPU threads?
  1. Yes. If your program is having multiple threads, then CPU will guarantee that your two functions will be running simultaneously in 2 threads.
  2. No. Even if you have created 2 threads in program, since there is always only one CPU, it can run only one thread at a time. But it uses time slicing internally to switch between the threads you have created in your program.
  3. both the answers are correct.
  4. You can't guarantee. Because the answer also depends on what cpu you use. For eg: if you are using dual core system, which has 2 cores (cpus). Now a days each core comes with 2 physical threads in it, where each thread can do one operation. 2 Cores having 2 threads each, that means your system has 4 physical threads. That means your system has the capability of doing 4 simultaneous operations. In such kind of machine CPU may allocate 2 physical threads to your 2 programmatically created threads and runs both the functions simultaneously. So the answer has dependence on which CPU you use. If you are using single core single threaded CPU, then option 2 will be correct.
Show Answer
Share Facebook gmail Twitter
7
If a program is having 2 functions with 2 threads, does it mean CPU will run both the functions simultaneously in two threads? Assuming that I have dual core system where each core is having 2 physical threads.
  1. Yes. In this case your CPU will make sure that it always runs your two functions simultaneously in 2 CPU threads.
  2. No. Still it runs in a single thread of your CPU.
  3. Answer is you can't guarantee. Even though you have created multiple threads and your system is having multi cores and multi threads, still the final decision will be taken by O.S. If O.S feels that your two functions doesn't really require 2 separate CPU threads because may be your functions are not so heavy, in that case OS scheduling algorithm will redirect both the threads( functions) to single CPU thread only.
  4. none
Show Answer
Share Facebook gmail Twitter
8
If my program is multi threaded and if my system is multi core system, and if there is no guarantee that my threads (functions) will not be run simultaneously, then what is the point of going for multi threaded programming? Is it same as writing a single threaded programming?
  1. Yes. There is no real use of multi threaded programming. You can opt to not create any multi threaded programming in java.
  2. No, your perception about threading is wrong. When ever you create multi threaded programming, your purpose of doing so was to distribute your work among multiple CPU threads so that your program will run faster. OS Scheduler will keep this in mind. If OS feels that your program is not so complicated to run simultaneously (may be because each core thread can run with super fast speed), then OS can choose to run all your program threads (functions) in single CPU thread.
  3. There is no straight forward answer for it. Since documentation says multi threaded programs run faster than single threaded, that's why you have to opt it.
  4. all are wrong
Show Answer
Share Facebook gmail Twitter
9
I have written a program which has two functions with one thread, and my friend has written same program with two functions with two java threads. Then whose program will run faster?
  1. Obviously your friends' program will run faster as he is using multi threaded programming.
  2. Answer depends on , on what system both of you are running your programs. If you are running in a multi core system, then there is a chance that your friends program may run faster. If you are using single core single threaded system, then both programs will take same time. (Note: In fact your program may run faster than your friends as creating and switching between threads takes some considerable amount of CPU time, though negligible)
  3. Your program with single thread will run faster than your friends program which has two threads.
  4. option 1 and 3 are correct.
Show Answer
Share Facebook gmail Twitter
10
How many ways are there to create threads in Java?
  1. You can create a class that extends Thread class.
  2. You can create a class that implements Runnable interface.
  3. You can use either option 1 or option 2.
  4. You have to use both option 1 and 2, to create threads in java.
Show Answer
Share Facebook gmail Twitter
11
Sample thread program in java:
What is the output of below Java program?
class MyThreadClass extends Thread{
        public void run(){
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo3 {
        public static void main(String[] args) {
                MyThreadClass mtc = new MyThreadClass();
                mtc.start();
        }
}
  1. it prints from 0 to 9
  2. it prints from 0 to 10
  3. It gives a compile time error in main method mtc.start(). Because There is no function with start() in our MyThreadClass.
  4. run time exception and crash. for loop will run infinite number of times and crashes.
Show Answer
Share Facebook gmail Twitter
12
How many threads are there(created) in the below Java program?
class MyThreadClass extends Thread{
        public void run(){
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo3 {
        public static void main(String[] args) {
                MyThreadClass mtc = new MyThreadClass();
                mtc.start();
        }
}
  1. Only one thread mtc.
  2. There are two threads MyThreadClass and mtc.
  3. There are two threads in our program. 1. Main thread created for every application by O.S 2. And second one is what we are creating programmatically. that is mtc.
  4. there are no threads in our program
Show Answer
Share Facebook gmail Twitter
13
What code (which lines of below program) will run in the background thread created by programmer?
class MyThreadClass extends Thread{
        public void run(){
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo3 {
        public static void main(String[] args) {
                MyThreadClass mtc = new MyThreadClass();
                mtc.start();
        }
}
  1. The complete programs runs in main thread only
  2. The complete program runs in thread created by programmer(mtc).
  3. We can't predict. The code will be shared between the two threads. Main thread and worker thread(mtc) created by programmer.
  4. what ever the logic you put in run() function of MyThreadClass, i.e the for loop runs in the worker thread (mtc) created by the programmer. main() function logic will run in main thread.
Show Answer
Share Facebook gmail Twitter
14
Is it possible to call run() method of a thread directly, in java?
What is the problem with below Java thread program?
class MyThreadClass extends Thread{
        public void run(){
                for(int i=0; i<10; i++){
                        System.out.println(i);
                }
        }
}
public class ThreadDemo3 {
        public static void main(String[] args) {
                MyThreadClass mtc = new MyThreadClass();
                mtc.run();
        }
}
  1. It gives compile time error. We are not supposed to call run() method of a thread directly. We have to call only start().
  2. There is no error, it prints 0 to 9. You can call start() or run() method. Both works find and both are correct also.
  3. Though there is no error, and prints properly from 0 to 9. But that is not a correct way, because when you call run() method directly it will not create a separate thread, rather run() function also runs in the main thread only. So it is like your having only one thread in your program.
  4. This gives a run time exception as we are not supposed to call run() method of a thread directly. It should be always called from frame work layer, so this will be detected at run time and throws run time exception.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
19
By default what is the priority of a thread created in Java?
  1. NORMAL_PRIORITY (5)
  2. MIN_PRIORITY(1)
  3. MAX_PRIORITY(10)
  4. NO_PRIORITY(0)
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :