Thread/multi threading Quiz in Java - MCQ - Multiple choice questions

Thread Java - MCQ - 80 Multiple choice questions in 4 sets



This Quiz consists of Threading/multi-threading 80 Multiple choice questions in 4 sets - Java Thread quiz - MCQ

Contents of page >

Thread Java - MCQ set 1 (20 questions, 43 marks)
Thread Java - MCQ set 2 (20 questions, 43 marks)
Thread Java - MCQ set 3 (20 questions, 43 marks)
Thread Java - MCQ set 4 (20 questions, 43 marks)







Note : Each set consists of 20 questions
Each set consists of 2 EASY level difficulty questions 1 mark each.         1 * 2 = 2 marks
Each set consists of 13 MEDIUM level difficulty questions 2 mark each. 2 * 13 = 26 marks
Each set consists of 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks
So, Each set is TOTAL of 43 marks
This quiz have been designed to check beginners and experienced Java developers skills.
Scoring below 12 marks means POOR : You are Java Beginner and need to work very hard.
Scoring 12-26 marks means AVERAGE : You know Java basics, but you need more practice.
Scoring 27-39 marks means GOOD : You have good Java knowledge.
Scoring above 39 marks means EXCELLENT : You have outstanding java knowledge.




Thread Java - MCQ set 1 (20 questions, 43 marks)



    Q1 - Q2, 2 EASY level difficulty questions 1 mark each. 1 * 2 = 2 marks

  1. What is true about threads?

  2. a.Threads consumes CPU in best possible manner
    b.Threads enables multi processing.
    c.Multi threading reduces idle time of CPU
    d.All

  3. A thread can acquire a lock by using which reserved keyword?

  4. a.volatile
    b.synchronized
    c.locked
    d.none



    Q3 - Q15, 13 MEDIUM level difficulty questions 2 mark each. 2 * 13 = 26 marks

  5. How many threads can a process contain?

  6. a.1
    b.2
    c.multiple
    d.none

  7. What is sometimes also called a lightweight process?

  8. a.Thread
    b.Process
    c.JVM
    d.

  9. What are valid points about thread

  10. a.Thread are subdivision of Process.
    b.One or more Threads runs in the context of process.
    c.Threads can execute any part of process. And same part of process can be executed by multiple Threads.
    d.All

  11. What are valid point about processes

  12. a.Processes have their own copy of the data segment of the parent process
    b.Threads have direct access to the data segment of its process
    c.Processes have their own address
    d.All of these

  13. Which is thread safe?

  14. a.StringBuffer
    b.StringBuilder
    c.All
    d.None

  15. How can we create Thread

  16. a.By Extending Thread class
    b.Implementing Runnable interface
    c.By using Executor framework - which can internally form threads
    d.All

  17. Which of these is not a Thread state?

  18. a.New
    b.Runnable
    c.sleep
    d.terminated

  19. synchronized instance methods acquire lock on?

  20. a.object
    b.class
    c.All
    d.None

  21. What state does Thread enter in when it has been created and started?

  22. a.New
    b.Runnable
    c.Running
    d.Waiting

  23. Which method can be used to find whether Thread hasn't entered dead state?

  24. a.isAlive()
    b.isRunning()
    c.isNotDead
    d.All

  25. What is valid about threads

  26. a.Threads have their own heap allocated area.
    b.Threads have their own stack.
    c.Threads doesn't have own stack.
    d.None

  27. How can you ensure all threads that started from main must end in order in which they started and also main should end in last

  28. a.join() method
    b.sleep() method
    c.wait() method
    d.run() method

  29. Which of these is valid about threads in java

  30. a.Thread behaviour is unpredictable
    b.execution of Threads depends on Thread scheduler
    c.Same threading program may produce different output in subsequent executions even on same platform
    d.All



    Q16 - Q20, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  31. Which method restarts the thread

  32. a.start()
    b.restart()
    c.restartThread()
    d.none

  33. What is true about acquiring object lock before calling wait(), notify() and notifyAll()?

  34. a.it’s mandatory to acquire object lock before calling wait(), notify() and notifyAll() methods on object
    b.If we call wait(), notify() and notifyAll() methods without acquiring object lock i.e. from outside synchronize block then java.lang.IllegalMonitorStateException is thrown at runtime.
    c.wait(), notify() and notifyAll() methods are always called from Synchronized block only
    d.all

  35. What is difference between starting thread with run() and start() method?

  36. a.There is no difference
    b.When you call start() method, main thread internally calls run() method to start newly created Thread
    c.run() calls start() method internally
    d.None

  37. What are valid statements for suspend() and resume() method?

  38. a.Suspend() method is deadlock prone.
    b.If the target thread holds a lock on object when it is suspended, no thread can lock this object until the target thread is resumed.
    c.If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, it results in deadlock formation.
    d.All

  39. How can Thread go from waiting to runnable state?

  40. a.notify/notifAll
    b.When sleep time is up
    c.Using resume() method when thread was suspended
    d.All





Quiz 1 - Correct answers



EASY
1) d
2) b

MEDIUM
3) c
4) a
5) d
6) d
7) a
8) d
9) c
10) a
11) c
12) a
13) b
14) a
15) d

HARD
16) d
17) d
18) b
19) d
20) d

Go to TOP of the page




Thread Java - MCQ set 2 (20 questions, 43 marks)



    Q1 - Q2, 2 EASY level difficulty questions 1 mark each. 1 * 2 = 2 marks

  1. wait(), notify() & notifyAll() are methods of which class or interface?

  2. a.Thread class
    b.Runnable interface
    c.Object
    d.None

  3. When start() method is called on thread it enters ______ state.

  4. a.running
    b.runnable
    c.new
    d.waiting



    Q3 - Q15, 13 MEDIUM level difficulty questions 2 mark each. 2 * 13 = 26 marks

  5. What is time slicing in threads

  6. a.In time slicing, a thread executes for a certain predefined time and then enters runnable pool.
    b.Thread in runnable pool can enter running state when selected by thread scheduler.
    c.All
    d.None

  7. Significance of Volatile keyword in java

  8. a.If a field is declared volatile, in that case the Java memory model ensures that all threads see a consistent value for the variable.
    b.Volatile caches methods
    c.All of these
    d.None

  9. What is name of thread which calls main method

  10. a.mainThread
    b.Thread
    c.Thread-0
    d.main

  11. Significance of synchronized variable

  12. a.synchronized variable doesn't exist
    b.synchronized variable are used in multi threading environment
    c.Prevents concurrent access to variables
    d.None

  13. What is pre-emptive scheduling in threads

  14. a.In pre-emptive scheduling, the highest priority thread executes until it enters into the waiting or dead state
    b.In pre-emptive scheduling, the low priority thread executes until it enters into the waiting or dead state
    c.In pre-emptive scheduling, the medium priority thread executes until it enters into the waiting or dead state
    d.Anyone may happen

  15. What will happen if two threads try to read same resource without synchronization in java

  16. a.It is not allowed in java
    b.It doesn't create any race condition
    c.Will create race condition
    d.None

  17. What are valid statements for daemon threads?

  18. a.User created threads are non daemon threads.
    b.JVM can exit when only daemon threads exist in system.
    c.Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.
    d.All of these

  19. What will happen if two threads try to write to same resource without synchronization in java

  20. a.It is not allowed in java
    b.It doesn't create any race condition
    c.It will create race condition
    d.None

  21. How to use volatile methods in java?

  22. a.Prevents concurrent access to shared resources
    b.volatile methods are used in non-multithreading environment
    c.volatile methods doesn't exist
    d.None

  23. Which tools could be used to analyse thread dumps

  24. a.VisualVM
    b.jstack
    c.All
    d.none

  25. Which method can make Thread to go from running to waiting state

  26. a.wait()
    b.resume()
    c.notify()
    d.alive()

  27. How Can we acquire lock on class?

  28. a.By acquiring lock on variables.
    b.By acquiring lock on instance variables.
    c.By acquiring lock on static method
    d.By acquiring lock on instance method

  29. What are valid statements for sleep method?

  30. a.when sleep() is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up.
    b.sleep() is a static method, causes the currently executing thread to sleep for the specified number of milliseconds.
    c.thread need not to to acquire object lock before calling sleep() method
    d.All



    Q16 - Q20, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  31. Which method can make Thread to go from running to waiting state?

  32. a.wait()
    b.sleep()
    c.suspend()
    d.All

  33. What are valid statements for yield method?

  34. a.yield() method when called on thread gives a hint to the thread scheduler that the current thread is willing to yield its current use of a processor. The thread scheduler is free to ignore this hint.
    b.yield() method stops thread for unpredictable time.
    c.yield() is a static method, hence calling Thread.yield() causes currently executing thread to yield.
    d.All

  35. What are valid statements about Constructor and synchronization in java?

  36. a.We can enclose constructor in try-catch block
    b.We can use use synchronized code in constructor
    c.Constructor can be synchronized
    d.None

  37. Which method can be used to find that thread holds lock

  38. a.holdLock()
    b.lockHold()
    c.holdsLock(object)
    d.lockHold(object)

  39. What is addShutdownHook method in java

  40. a.addShutdownHook method registers a new virtual-machine shutdown hook.
    b.A shut down hook is a uninitialized and unstated thread.
    c.When JVM initialises it will start all registered shutdown hooks
    d.None





Quiz 2 - Correct answers



EASY
1) c
2) b

MEDIUM
3) c
4) a
5) d
6) a
7) a
8) b
9) d
10) c
11) c
12) c
13) a
14) c
15) d

HARD
16) d
17) d
18) b
19) c
20) a

Go to TOP of the page




Thread Java - MCQ set 3 (20 questions, 43 marks)



    Q1 - Q2, 2 EASY level difficulty questions 1 mark each. 1 * 2 = 2 marks

  1. Can multiple threads exist on one object?

  2. a.Multiple threads can exist on same object
    b.Multiple threads cannot exist on same object
    c.Only 2 threads can exist on same object
    d.None

  3. What is deadlock in java?

  4. a.Deadlock is a situation where two threads on same object are waiting for each other to release lock holded by them on resources
    b.Deadlock is a situation where threads on different object are waiting for each other to release lock holded by them on resources
    c.None



    Q3 - Q15, 13 MEDIUM level difficulty questions 2 mark each. 2 * 13 = 26 marks

  5. Which method can be used to handle uncaught runtime exception generated in run method?

  6. a.exceptionHandler()
    b.defaultExceptionHandler()
    c.setDefaultUncaughtExceptionHandler()
    d.catchException()

  7. What is valid statement about ThreadGroup

  8. a.When program starts JVM creates a ThreadGroup named main
    b.Unless specified, all newly created threads become members of the main thread group
    c.All
    d.None

  9. What is minimum thread priority in java?

  10. a.-1
    b.0
    c.1
    d.5

  11. What is maximum thread priority

  12. a.10
    b.12
    c.5
    d.8

  13. What will be output of following code -


  14. a.start main() method
            end main() method
            i=0 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=1 ,ThreadName=Thread-0
            i=2 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-1
            (Output may vary)
    b.start main() method
            i=0 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=1 ,ThreadName=Thread-0
            i=2 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-1
            (Output may vary)
    c.end main() method
            start main() method
            i=0 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=1 ,ThreadName=Thread-0
            i=2 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-1
            (Output may vary)
    d.None

  15. What will be output of following code -


  16. a.In main() method
            i=0 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-1
            end main() method
            i=2 ,ThreadName=Thread-1
    b.In main() method
            i=0 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-1
            end main() method
    c.In main() method
            i=0 ,ThreadName=Thread-0
            i=1 ,ThreadName=Thread-0
            i=2 ,ThreadName=Thread-0
            i=0 ,ThreadName=Thread-1
            i=1 ,ThreadName=Thread-1
            i=2 ,ThreadName=Thread-1
            end main() method
    d.None

  17. What will be output of following code -


  18. a.Thread-1 ENDED
            x
            press enter
           
    b.press enter
            x
            x
            x
            x
           
            Thread-1 ENDED
            ( thread1 will keep on printing x until enter is pressed)
    c.Thread-1 ENDED
            press enter
            x
    d.None

  19. What will be output of following code -


  20. a.1
    b.2
    c.Exception will be thrown
    d.None

  21. What will be output of following code -


  22. a.Consumer waiting for production to get over.
            Consumed : 1
            Producer is still Producing, Produced : 1
            Producer is still Producing, Produced : 2
            Producer is still Producing, Produced : 3
            Production is over, consumer can consume.
           
            Consumed : 2
            Consumed : 3
    b.Consumer waiting for production to get over.
            Producer is still Producing, Produced : 1
            Producer is still Producing, Produced : 2
            Producer is still Producing, Produced : 3
            Production is over, consumer can consume.
            Consumed : 1
            Consumed : 2
            Consumed : 3
    c.None

  23. What will be output of following code -


  24. a.1
    b.Nothing will be printed
    c.Runtime exception will be thrown
    d.1 2

  25. What will be output of following code -


  26. a.1 3 2 4
    b.1 2 3 4
    c.Runtime exception
    d.(None of these)

  27. What will be output of following code -


  28. a.1 2
    b.2 3
    c.1 3
    d.Compilation error

  29. What will be output of following code -


  30. a.Thread-1
            Thread-1
            Thread-2
            Thread-2
    b.Thread-1
            Thread-2
            Thread-1
            Thread-2
    c.Thread-1
            Thread-2
            Thread-2
            Thread-1
    d.None



    Q16 - Q20, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  31. What will be output of following code -


  32. a.1
            4
    b.1
            2
            3
            4
            5
    c.1
            2
            4
            5
            3
    d.4
            1

  33. What will be output of following code -


  34. a.main has started
            Thread-1 has started
            Thread-1 has ended
    b.main has started
            Thread-1 has started
            main has ended
            Thread-1 has ended
           
    c.main has started
            Thread-1 has started
            Thread-1 has ended
            main has ended
    d.None

  35. What will be output of following code -


  36. a.RuntimeException will be thrown
    b.1
    c.2
    d.Nothing will be printed

  37. What will be output of following code -


  38. a.Program will face compile time Exception
    b.Thread-1 Thread-1 Thread-0 Thread-0
    c.Program will face NullPointerException
    d.None

  39. What will be output of following code -


  40. a.Thread-1 in synchronized void method1() started
            Thread-2 in synchronized void method2() started
            Thread-2 in synchronized void method2() ended
            Thread-1 in synchronized void method1() ended
    b.Thread-1 in synchronized void method1() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() started
            Thread-2 in synchronized void method2() ended
    c.Thread-1 in synchronized void method1() started
            Thread-2 in synchronized void method2() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() ended
    d.None





Quiz 3 - Correct answers



EASY
1) a
2) a

MEDIUM
3) c
4) c
5) c
6) a
7) a
8) c
9) b
10) a
11) b
12) d
13) b
14) c
15) a

HARD
16) d
17) c
18) b
19) c
20) b

Go to TOP of the page




Thread Java - MCQ set 4 (20 questions, 43 marks)



    Q1 - Q2, 2 EASY level difficulty questions 1 mark each. 1 * 2 = 2 marks

  1. What is race condition in java

  2. a.Two threads try to execute earlier form sort of race.
    b.Race condition doesn't allow two threads to access same resource
    c.When more than one thread try to access same resource without synchronization causes race condition.
    d.None

  3. Which package consists of all inbuilt java exceptions?

  4. a.java.lang.Thread
    b.java.io.Thread
    c.java.thread.Thread
    d.java.util.Thread



    Q3 - Q15, 13 MEDIUM level difficulty questions 2 mark each. 2 * 13 = 26 marks

  5. How Can we acquire lock on object?

  6. a.By acquiring lock on instance variables.
    b.By acquiring lock on instance method
    c.By acquiring lock on static method
    d.By acquiring lock on static variables.

  7. What is valid about ThreadLocal

  8. a.Every thread has its own ThreadLocal value that makes ThreadLocal value thread safe as well.
    b.Thread holds ThreadLocal value till it hasn’t entered dead state.
    c.ThreadLocal variables are thread safe and thread can see only it’s ThreadLocal value.
    d.All

  9. What is default priority of thread?

  10. a.5
    b.0
    c.1
    d.10

  11. What is busy spin in java?

  12. a.Applications must promote busy spin usage
    b.When one thread loops continuously waiting for another thread to signal.
    c.Busy spin is very good from performance point of view
    d.None

  13. What will be output of following code -


  14. a.Compile time exception
    b.1 will be printed and IllegalThreadStateException will be thrown
    c.IllegalThreadStateException will be thrown
    d.None of these

  15. What will be output of following code -


  16. a.Run time exception as neither of run() and start() are not overridden
    b.start will be printed
    c.Nothing will be printed in output.
    d.Compile time error as neither of run() and start() are not overridden

  17. What will be output of following code -


  18. a.2
    b.1 2
    c.Compile time error
    d.Runtime Exception

  19. synchronized static methods acquire lock on?

  20. a.class
    b.object
    c.Interface
    d.None

  21. What is ThreadPool in java

  22. a.ThreadPool is a pool of threads which reuses a fixed number of threads to execute tasks.
    b.At any point, at most n number of threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.
    c.ThreadPool implementation can internally use LinkedBlockingQueue for adding and removing tasks.
    d.All

  23. What will be output of following code -



  24. a.Thread-1 in synchronized void method1() started
            Thread-2 in synchronized void method2() started
            Thread-2 in synchronized void method2() ended
            Thread-1 in synchronized void method1() ended
    b.Thread-1 in synchronized void method1() started
            Thread-2 in synchronized void method2() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() ended
    c.Thread-1 in synchronized void method1() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() started
            Thread-2 in synchronized void method2() ended
    d.None

  25. What will be output of following code -


  26. a.1 will be present in output and runtime exception will be thrown.
    b.1 and 2 will be present in output and runtime exception will be thrown.
    c.run time exception
    d.None

  27. Green threads are not at all dependent on OS thread scheduler

  28. a.true
    b.false
  29. What will be output of following code -


  30. a.Thread-1 in synchronized void method1() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() started
            Thread-2 in synchronized void method2() ended
    b.Compilation error
    c.Thread-1 in synchronized void method1() started
            Thread-2 in synchronized void method2() started
            Thread-1 in synchronized void method1() ended
            Thread-2 in synchronized void method2() ended
    d.None



    Q16 - Q20, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  31. What will be output of following code -


  32. a.IllegalMonitorStateException is thrown at runtime
    b.compileTime exception
    c.1 2
    d.None

  33. What will be output of following code -


  34. a.Compilation error
    b.2
    c.run time exception
    d.None of these

  35. Can we use this keyword in static method?

  36. a.this keyword can be used in static method to access static variables only
    b.this keyword can't be used in static method
    c.this keyword can be used in static method
    d.None

  37. What are green threads in java?

  38. a.Green threads are user-level threads
    b.Green threads are high level threads
    c.Green threads are OS level threads
    d.None

  39. What is true about green threads in java?

  40. a.Green threads were used till Java 2. Green threads are not at all dependent on OS thread scheduler
    b.Green threads provides multi-threading capabilities on platforms that don't provide multi-threading capability.
    c.Non-green threads have their own stack, but for Green threads memory is allocated from heap area.
    d.All





Quiz 4 - Correct answers



EASY
1) c
2) a

MEDIUM
3) b
4) d
5) a
6) b
7) b
8) c
9) a
10) a
11)
12) c
13) b
14) a
15) c

HARD
16) a
17) a
18) b
19) a
20) d

Go to TOP of the page
eEdit
Must read for you :