Java Programming - Threads

Why Java Programming Threads?

In this section you can learn and practice Java Programming Questions based on "Threads" and improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.

Where can I get Java Programming Threads questions and answers with explanation?

IndiaBIX provides you lots of fully solved Java Programming (Threads) questions and answers with Explanation. Solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Java Programming Threads quiz questions with answers as PDF files and eBooks.

Where can I get Java Programming Threads Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type Java Programming Threads questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve Java Programming Threads problems?

You can easily solve all kind of Java Programming questions based on Threads by practicing the objective type exercises given below, also get shortcut methods to solve Java Programming Threads problems.

Exercise :: Threads - General Questions

1. 

What is the name of the method used to start a thread execution?

A. init();
B. start();
C. run();
D. resume();

Answer: Option B

Explanation:

Option B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Option A is wrong. There is no init() method in the Thread class.

Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.

Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.


2. 

Which two are valid constructors for Thread?

  1. Thread(Runnable r, String name)
  2. Thread()
  3. Thread(int priority)
  4. Thread(Runnable r, ThreadGroup g)
  5. Thread(Runnable r, int priority)

A. 1 and 3
B. 2 and 4
C. 1 and 2
D. 2 and 5

Answer: Option C

Explanation:

(1) and (2) are both valid constructors for Thread.

(3), (4), and (5) are not legal Thread constructors, although (4) is close. If you reverse the arguments in (4), you'd have a valid constructor.


3. 

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();

A. 1, 2, 4
B. 2, 4, 5
C. 1, 2, 6
D. 2, 3, 4

Answer: Option C

Explanation:

(1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object.

(3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() and interrupt() are instance methods of Thread.

The methods sleep() and yield() are static methods of Thread.

D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language.


4. 

class X implements Runnable 
{ 
    public static void main(String args[]) 
    {
        /* Missing code? */
    } 
    public void run() {} 
}
Which of the following line of code is suitable to start a thread ?

A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.run();

Answer: Option C

Explanation:

Option C is suitable to start a thread.


5. 

Which cannot directly cause a thread to stop executing?

A. Calling the SetPriority() method on a Thread object.
B. Calling the wait() method on an object.
C. Calling notify() method on an object.
D. Calling read() method on an InputStream object.

Answer: Option C

Explanation:

Option C is correct. notify() - wakes up a single thread that is waiting on this object's monitor.




Have a question?

Ask your question now !

Create your own website ?

Web Hosting Services in India