0

Java Multithreading and Exception Handling

Description: Java Multithreading and Exception Handling
Number of Questions: 28
Created by:
Tags: Java Multithreading and Exception Handling Java Basics Multithreading and Garbage Collection
Attempted 0/28 Correct 0 Score 0

Which of the following methods is/are correct for making a new thread? A. Declare a class to be a subclass of Thread class. B. Declare a class that implements the Runnable class.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: A
Explanation:

We can create a new thread using two ways:

  1. Declare a class to be a subclass of Thread class.
  2. Declare a class that implements the Runnable interface.

The code we want to execute has to be placed in a method ________ of the class which extends class Thread.

  1. start()

  2. run()

  3. first()

  4. exec()


Correct Option: B
Explanation:

The code we want to execute has to be place in a method run() of the class which extends class Thread. This function will automatically call by using start().

Which one of the following is the correct structure to create and run a thread?

  1. class MyClass extends Runnable { public void run() { } }

  2. class MyClass implements Thread { public void run() { } }

  3. class Thread MyClass { public void run() { } }

  4. class MyClass extends Thread { public void run() { } }


Correct Option: D
Explanation:

The correct structure is: class MyClass extends Thread {       public void run()       {       } }

Which one of the following is not a correct state for life cycle of a thread?

  1. Ready

  2. Blocked

  3. Return

  4. Dead


Correct Option: C
Explanation:

The correct state sequence for life cycle of a thread is:

  1. New
  2. Ready
  3. Running
  4. Blocked
  5. Dead There is no return state.

When an allocated time of a thread is over, JRE puts it back in the _____ state.

  1. new

  2. ready

  3. running

  4. dead


Correct Option: B
Explanation:

When an allocated time of a thread is over, JRE puts it back in the ready state for giving the chance to another thread.

Which one of the following is not a valid thread priority constant?

  1. MAX_PRIORITY

  2. MIN_PRIORITY

  3. NORM_PRIORITY

  4. AVG_PRIORITY


Correct Option: D
Explanation:

There is no thread priority constant as AVG_PRIORITY.

FirstTh th=new FirstTh(); Thread sm=new Thread(th).start(); Which of the following statements is correct with respect to the above code?

  1. FirstTh is a simple class.

  2. FirstTh is a class that inherits from Thread class.

  3. FirstTh is a class that implements from Thread class.

  4. FirstTh is a class that implements Runnable interface.


Correct Option: D
Explanation:

Here FirstTh must be a class which implements Runnable interface. Because the statements FirstTh th=new FirstTh(); Thread sm=new Thread(th).start(); Are used to create a thread by implementing Runnable interface.

Which of the following methods makes the current thread to wait until other specified thread terminates?

  1. wait()

  2. join()

  3. notify()

  4. delay()


Correct Option: B
Explanation:

The join() method of thread class makes the current thread to wait until other specified thread terminates.

The methods wait(), notify() and notifyAll() must be called only when they own the monitor of that object, otherwise they throw __________________.

  1. IllegalMonitorException

  2. IllegalStateException

  3. IllegalMonitorStateException

  4. IllegalMonitorOwnStateException


Correct Option: C
Explanation:

All these methods must be called only when they own the monitor of that object, otherwise they throw IllegalMonitorStateException exception. These methods are used for proper coordination among the threads.

The class Timer and TimerTask are available in the package __________.

  1. java.awt

  2. java.swing

  3. java.math

  4. java.util


Correct Option: D
Explanation:

The package java.util gives two very useful classes, i.e. Timer and TimerTask. These classes are used for conjunction.

Which one of the following is not a valid constructor of Timer class?

  1. Timer()

  2. Timer(Boolean isDaemon)

  3. Timer(String name)

  4. Timer(char[] name)


Correct Option: D
Explanation:

There are four constructors for a Timer class:

  1. Timer()
  2. Timer(Boolean isDaemon)
  3. Timer(String name)
  4. Timer(String name,Boolean isDaemon)

What is the use of purge() method in Timer class?

  1. Terminate this Timer

  2. Schedule the specified task for execution at the specified time

  3. Remove all cancelled tasks from this timer’s task queue

  4. Remove all running tasks from this timer’s task queue


Correct Option: C
Explanation:

The purge() method of Timer class removes all cancelled tasks from this timer’s task queue.

Which of the following statements is/are correct? A. The sleep() method is used to permanently stop the execution of the thread. B. The join() method is used to wait for this thread to die.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: B
Explanation:

A. The sleep() method is used to temporarily stop the execution of the thread. B. The join() method is used to wait for this thread to die.

Suppose a thread wants some input data. It cannot continue unless it is made available by the OS. Such a state is called a _________ state.

  1. ready

  2. blocked

  3. running

  4. dead


Correct Option: B
Explanation:

Suppose a thread wants some input data. It cannot continue unless it is made available by the OS. Such a state is called a blocked state.

What is the use of destroy() method of thread class?

  1. Destroy this thread, without any clean-up

  2. Destroy this thread, with clean-up

  3. Destroy all threads, without any clean-up

  4. Destroy all threads, with clean-up


Correct Option: A
Explanation:

The destroy() method of thread class destroys this thread, without any clean-up.

Which of the following correctly represents the exception?

  1. Compile error

  2. Syntax error

  3. Runtime error

  4. Not an error


Correct Option: C
Explanation:

Exception is a runtime error which terminates the program abruptly.

Which of the following packages defines a class Throwable?

  1. java.awt

  2. java.swing

  3. java.lang

  4. java.util


Correct Option: C
Explanation:

The package java.lang defines a class Throwable.

Throwable class has two sub-classes, ________ and __________.

  1. Errors and Exceptions

  2. Error and Exceptions

  3. Errors and Exception

  4. Error and Exception


Correct Option: D
Explanation:

There are two sub-classes of a Throwable class:

  1. Errors
  2. Exceptions

Java has defined a _______________ class which is a super-class for all runtime error classes.

  1. RuntimeExceptions

  2. RuntimeException

  3. Exception

  4. Throwable


Correct Option: B
Explanation:

Java has defined a RuntimeException class which is a super-class for all runtime error classes.

The code which is likely to produce exception is kept within the ___________ block.

  1. catch

  2. try

  3. throw

  4. main()


Correct Option: B
Explanation:

The code which is likely to produce an exception must be kept within the try block.

A block labelled as finally will _________.

  1. never execute

  2. always execute

  3. execute if catch block will execute

  4. execute if catch block will not execute


Correct Option: B
Explanation:

A block labelled as finally, just after the catch statements, will always be executed.

The throw statement is used to throw the _____ type exception.

  1. class

  2. data

  3. object

  4. methods


Correct Option: C
Explanation:

The throw statement is used to throw the object type exception.

Which of the following exceptions occurs, if one is trying to access an element of array with an index that is larger than the range?

  1. ArrayIndexOutOfBoundsException

  2. ArrayIndexOutOfBoundsExceptions

  3. ArrayIndexOutOfBoundException

  4. ArrayOutOfBoundsException


Correct Option: A
Explanation:

The ArrayIndexOutOfBoundsException occurs if one is trying to access an element of array with an index that is larger than the range. For example: int Num[]={0,2,3}; Num[5]=0; /This statement throws the exception/

Which of the following exceptions is not a sub-class of RuntimeException?

  1. ArithmeticException

  2. ArrayStoreException

  3. SecurityException

  4. ClassCastException


Correct Option: D
Explanation:

The exception ClassCastException is not a sub-class of RuntimeException.

An instance of ThreadDeath is thrown in the victim thread when the stop method is called with ____ arguments.

  1. 0

  2. 1

  3. 2

  4. 3


Correct Option: A
Explanation:

An instance of ThreadDeath is thrown in the victim thread when the stop method is called with zero arguments in class Thread.

Which of the following statements is/are correct? A. A finally block is optional. B. throw and throws are the same.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: A
Explanation:

A finally block is optional. The keyword throw and throws are not the same. The keyword throws is used in method definition to represent that a method may possibly throw an exception.

What is the return type of start() method of class Thread?

  1. int

  2. void

  3. char

  4. static void


Correct Option: B
Explanation:

The return type of start() method is void, i.e. void start().

In Java, the concept of synchronisation is based on the concept of ___________.

  1. thread

  2. explicit lock

  3. intrinsic lock

  4. deadlock


Correct Option: C
Explanation:

In Java, the concept of synchronisation is based on the concept of intrinsic lock or monitor lock.

- Hide questions