Adv Java | Unit 4 Multithreading | Multiple Choice Questions With Answer

 







 

 

SUBJECT CODE: 602 SUBJECT NAME: ADVANCED JAVA

Unit 4 : Multithreading

 

 

 

 

 

 

 

 

1) In java multi-threading, a thread can be created by

a. Extending Thread class

b. Implementing Runnable interface

c. Using both

d. None

Answer:C

 

 

 

 

 

 

 

2) Which method is called internally by Thread start() method?

a. execute()

b. run()

c. launch()

d. main()

Answer:B

 

 

 

 

 

 

 

 

3) What is maximum thread priority in Java

a. 10

b. 12

c. 5

d. 8

Answer:A

 

 

 

 

 

 

     

 

 

4) Which method must be implemented by a Java thread?

a. run()

b. execute()

c. start()

d. None

Answer:A

 

 

 

 

 

 

 

 

Q5) Number of threads in below java program is

public class ThreadExtended extends Thread {

public void run() {

System.out.println("\nThread is running now\n");

}

public static void main(String[] args) {

ThreadExtended threadE = new ThreadExtended();

threadE.start();

}}

a) 0                             b) 1

c) 2                              d) 3

Answer:C

Q6) Execution of a java thread begins on which method call?

a) Start ()

b) Run ()

c) Execute ()

d) Launch ()

Answer:A

 

 

 

 

 

 

 

 

 

Q7) How many ways a thread can be created in Java multithreading?

a) 1

b) 2

c) 3

d) 4

Answer:B

 

 

 

 

 

 

 

 

Q8) Which statements is/are correct

a) On calling Thread start () method a new thread get createD)

b) Thread start () method call run () method internally

c) Thread run () method can also be called directly to create threaD)

d) All correct

Answer:A & B

 

 

 

 

 

 

Q9) Which method is used to make main thread to wait for all child threads

a) Join ()

b) Sleep ()

c) Wait ()

d) Stop ()

Answer:A

     

 

 

 

 

 

 

 

Q10) Default value of a java thread is

a) 0

b) 1

c) 5

d) 10

Answer:C

 

 

 

 

 

 

 

 

Q11) If a priority of a java thread is 3 then the default priority of its child thread

will be

a) 0

b) 1

c) 5

d) 3

Answer:D

 

 

 

 

 

 

 

Q12) Which method is used to check if a thread is running?

a) isAlive()

b) run ()

c) alive ()

d) keepAlive()

Answer:A

 

 

 

 

 

 

 

 

Q13) True statement about process and thread is/are

a) If a child process crashes all main process will also be crashed

b) If a child thread is crash entire process will crash

c) Threads have their own memory stack

d) Each process has different virtual space

Answer:B,C,D

 

 

 

 

 

 

     

Q14) Min and Max priority of a thread in Java multithreading are

A) 1, 10

B) 0,10

C) 0,255

D) 1,256

Answer:A

 

 

 

 

 

 

 

Q15) Which method we implement from Runnable interface?

a) Run ()

b) Start ()

c) Execute ()

d) call ()

Answer:A

 

 

 

 

 

 

 

 

Q16) Daemon thread runs in

a) Background

b) Foreground

c) Both

d) None

Answer:A

 

 

 

 

 

 

 

 

 

Q17) Which method is used to create a daemon thread?

a) setDaemon(boolean value)

b) setThread(boolean value)

c) createDaemon(boolean value)

d) makeDeamon(boolean value);

     

 

Answer:A

 

 

 

 

 

 

Q18) To create a daemon thread

a) First thread setDaemon() is called then start()

b) First thread start() is called then setDaemon()

c) Call thread start() and setDaemon() in any order

d) All correct

Answer:A

 

 

 

 

 

 

 

Q19) Which method is used to wait for child threads to finish in Java?

a) Wait ()

b) Sleep ()

c) Join ()

d) isAlive()

Answer:C

 

 

 

 

 

 

 

 

Q20) Which will contain the body of the thread in Java?

a) Start()

b) Run()

c) Main()

d) Execute()

Answer:B

 

 

 

 

 

     

 

 

21) What requires less resources?

A) Thread

B) Process

C) Thread and Process

D) Neither Thread nor Process

Answer: a

Explanation: Thread is a lightweight and requires less resources to create and

exist in the process. Thread shares the process resources.

 

 

 

 

 

 

22) What does not prevent JVM from terminating?

A) Process

B) Daemon Thread

C) User Thread

D) JVM Thread

Answer: B

Explanation: Daemon thread runs in the background and does not prevent JVM

from terminating. Child of daemon thread is also daemon thread.

 

 

 

 

 

 

23) What decides thread priority?

A) Process

B) Process scheduler

C) Thread

D) Thread scheduler

Answer: D

Explanation: Thread scheduler decides the priority of the thread execution.

This cannot guarantee that higher priority thread will be executed first, it

depends on thread scheduler implementation that is OS dependent.

 

 

 

24) What is true about time slicing?

A) Time slicing is OS service that allocates CPU time to available runnable

thread

B) Time slicing is the process to divide the available CPU time to available

runnable thread

C) Time slicing depends on its implementation in OS

D) Time slicing allocates more resources to thread

Answer: B

Explanation: Time slicing is the process to divide the available CPU time to

available runnable thread.

    

25) Deadlock is a situation when thread is waiting for other thread to release acquired object.

A) True

B) False

Answer: a

Explanation: Deadlock is java programming situation where one thread waits

for an object lock that is acquired by other thread and vice-versa.

 

 

 

 

 

 

 

26) What should not be done to avoid deadlock?

A) Avoid using multiple threads

B) Avoid hold several locks at once

C) Execute foreign code while holding a lock

D) Use interruptible locks

Answer: c

Explanation: To avoid deadlock situation in Java programming do not execute

foreign code while holding a lock.

 

 

 

27) What is true about threading?

A) run() method calls start() method and runs the code

B) run() method creates new thread

C) run() method can be called directly without start() method being called

D) start() method creates new thread and calls code written in run() method

Answer: D

Explanation: start() eventually calls run() method. Start() method creates thread

and calls the code written inside run method.

 

 

28) Which of the following is a correct constructor for thread?

A) Thread(Runnable a, String str)

B) Thread(int priority)

C) Thread(Runnable a, int priority)

D) Thread(Runnable a, ThreadGroup t)

Answer: a

Explanation: Thread(Runnable a, String str) is a valid constructor for thread.

Thread() is also a valid constructor.

 

 

 

 

 

29) Which of the following stops execution of a thread?

A) Calling SetPriority() method on a Thread object

B) Calling notify() method on an object

C) Calling wait() method on an object

D) Calling read() method on an InputStream object

     

Answer: B

Explanation: notify() wakes up a single thread which is waiting for this object.

 

 

 

30) Which of the following will ensure the thread will be in running state?

A) yield()

B) notify()

C) wait()

D) Thread.killThread()

Answer: c

Explanation: wait() always causes the current thread to go into the object’s wait

pool. Hence, using this in a thread will keep it in running state.

 

 

 

 

 

31) What is multithreaded programming?

A) It’s a process in which two different processes run simultaneously

B) It’s a process in which two or more parts of same process run

simultaneously

C) It’s a process in which many different process are able to access same

information

D) It’s a process in which a single process can access information from many

sources

Answer: B

Explanation: Multithreaded programming a process in which two or more parts

of the same process run simultaneously.

32) Which of these are types of multitasking?

A) Process based

B) Thread based

C) Process and Thread based

D) None of the mentioned

Answer: c

Explanation: There are two types of multitasking: Process based multitasking

and Thread based multitasking.

 

 

 

 

 

 

33) Thread priority in Java is?

A) Integer

B) Float     

C) double

D) long

Answer: a

Explanation: Java assigns to each thread a priority that determines hoe that

thread should be treated with respect to others. Thread priority is integers that

specify relative priority of one thread to another.

 

 

 

34) What will happen if two thread of the same priority are called to be processed

simultaneously?

A) Anyone will be executed first lexographically

B) Both of them will be executed simultaneously

C) None of them will be executed

D) It is dependent on the operating system

Answer: D

Explanation: In cases where two or more thread with same priority are competing for CPU cycles, different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.

 

 

35) Which of these statements is incorrect?

A) By multithreading CPU idle time is minimized, and we can take maximum

use of it

B) By multitasking CPU idle time is minimized, and we can take maximum use

of it

C) Two thread in Java can have the same priority

D) A thread can exist only in two states, running and blocked

Answer: D

Explanation: Thread exist in several states, a thread can be running, suspended,

blocked, terminated & ready to run.

36) What will be the output of the following Java code?

1. class multithreaded_programing

2. {

3. public static void main(String args[])

4. {

5. Thread t = Thread.currentThread();

6. System.out.println(t);

7. } }

A) Thread[5,main]

B) Thread[main,5]

C) Thread[main,0]

D) Thread[main,5,main]

Answer: D

Explanation: None.

37) What is the priority of the thread in the following Java Program?

1. class multithreaded_programing

2. {

3. public static void main(String args[])

4. {

5. Thread t = Thread.currentThread();

6. System.out.println(t);

7. }

8. }

A) 4

B) 5

C) 0

D) 1

Answer: B

38) What is the name of the thread in the following Java Program?

1. class multithreaded_programing

2. {     

3. public static void main(String args[])

4. {

5. Thread t = Thread.currentThread();

6. System.out.println(t);

7. }

8. }

A) main

B) Thread

C) System

D) None of the mentioned

Answer: a

39) Which method is used to wait for child threads to finish in Java?

e) Wait ()

f) Sleep ()

g) Join ()

h) isAlive()

Answer:C

 

 

 

 

 

 

 

40) Which will contain the body of the thread in Java?

e) Start()

f) Run()

g) Main()

h) Execute()

Answer:B

 




Post a Comment

0 Comments