Monday 21 December 2015

Exceptions Java - MCQ set 1 (25 questions, 50 marks)

Exceptions Java - MCQ set 1 (25 questions, 50 marks)



    Q1 - Q5, 5 EASY level difficulty questions 1 mark each. 1 * 5 = 5 marks 

  1. Exception is a class/interface/abstract class/other?

  2. a.Class
    b.Interface
    c.Abstract class
    d.Other

  3. Exception is found in which package in java

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

  5. Exception was introduced in which java version

  6. a.Java 1
    b.Java 2
    c.Java 3
    d.Java 4

  7. Which of these class is highest in hierarchy in java

  8. a.java.lang.Exception
    b.java.lang.Error
    c.java.lang.Throwable
    d.java.lang.Object

  9. What keyword is used to explicitly raise a exception?

  10. a.catch
    b.throw
    c.throws
    d.raise



    Q6 - Q20, 15 MEDIUM level difficulty questions 2 mark each. 2 * 15 = 30 marks

  11. What block is always executed, independently of a exception being raised?

  12. a.throws
    b.finally
    c.catch
    d.throw

  13. Exception and Error are direct subclasses of?

  14. a.BaseException
    b.Throwable
    c.Object
    d.RuntimeException

  15. FileNotFoundException

  16. a.Is a subclass/extends IOException
    b.Is a Compile time exception
    c.Found in java.io package
    d.All

  17. IOException

  18. a.Found in java.io package
    b.Is a Compile time exception
    c.Is a subclass/extends Exception
    d.All

  19. Which of these are java.lang.Error in exception handling in java

  20. a.VirtualMachineError
    b.IOError
    c.AssertionError
    d.ThreadDeath
    e.All

  21. In which condition will the finally block will not be executed?

  22. a.When some Error occurs
    b.When Exception is raised
    c.When System.Exit(1) is called
    d.In all the cases

  23. What type of Exceptions can be ignored at compile time?

  24. a.Runtime
    b.Checked
    c.Both
    d.None

  25. What will be output of following exception handling code in java -


  26. a.ade
    b.adb
    c.bcde
    d.adbe

  27. What will be output of following program -


  28. a.runtime exception
    b.program will compile
    c.program won't compile
    d.

  29. What will happen in this program -


  30. a.StackFlowoverError
    b.compile timeError
    c.StackOverflowException
    d.StackOverflowError

  31. What is invalid statements for automatic resource management in java?

  32. a.Allows us to catch more than one exception in one catch block
    b.We could separate different exceptions using pipe ( | )
    c.Increases developer efforts of writing multiple catch blocks.
    d.None

  33. multiple catch syntax was introduced in ?

  34. a.java 5
    b.java 6
    c.java 7
    d.java 8

  35. Which of these points will be valid If superclass method does not throw any exception

  36. a.overridden method of subclass can throw any RuntimeException
    b.overridden method of subclass cannot throw any checked exception
    c.overridden method of subclass may not throw any exception.
    d.All

  37. What will happen when catch and finally block both return value?

  38. a.method will return value returned by finally block
    b.method will return value returned by catch block
    c.finally block won't execute
    d.None

  39. can a method be overloaded on basis of exceptions ?

  40. a.Will produce runtime error
    b.Will produce compilation error - ambiguity error
    c.Yes a method be overloaded on basis of exceptions.
    d.None



    Q21 - Q25, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  41. What will be output of following program -


  42. a.runtime exception
    b.method return -> finally
    c.method return -> catch
    d.compile timeError

  43. What will be output of following program -


  44. a.abc
    b.abcd
    c.runtime exception
    d.compile timeError

  45. Which is valid about java.lang.Exceptions?

  46. a.The class Exception and all its subclasses that are not also subclasses of RuntimeException are checkedexceptions
    b.The class Error and all its subclasses are unchecked exceptions
    c.The class RuntimeException and all its subclasses are unchecked exceptions
    d.All

  47. What is disadvantage/invalid about try-with-resource

  48. a.introduced in java 7
    b.we need not to write explicit code for closing file
    c.Using multiple resources inside Try-with-resources is also allowed
    d.None

  49. FileInputStream implements which interface for closing file automatically in java7?

  50. a.java.lang.AutoClose
    b.java.lang.CloseAutomatically
    c.java.lang.AutoCloseable
    d.java.lang.Closeable




Quiz 1 - Correct answers



EASY
1) a
2) a
3) a
4) d
5) b

MEDIUM
6) b
7) b
8) d
9) d
10) e

11) c
12) a
13) b
14) c
15) d
16) c
17) c
18) d
19) a
20) c

HARD
21) b
22) c
23) d
24) d
25) c

Go to TOP of the page




Exceptions Java - MCQ set 2 (25 questions, 50 marks)



    Q1 - Q5, 5 EASY level difficulty questions 1 mark each. 1 * 5 = 5 marks 

  1. What type of Exceptions can NOT be ignored at compile time?

  2. a.Checked Exceptions
    b.Unchecked Exceptions
    c.Uncompiled Exceptions
    d.Error

  3. java.lang.NullPointerException is a

  4. a.Error
    b.runtime exception
    c.Compile time exception
    d.None

  5. What will be output of following program -


  6. a.compile timeError
    b.UserDefinedException will be thrown
    c.UserDefinedException will be thrown and end will be present in output
    d.None

  7. Which of these is valid code snippet in exception handling in java?

  8. a. catch{ }
    b. finally{ }
    c. try{ }finally{ }
    d. try{ }

  9. Which of these will be invalid declaration

  10. a.try-catch block.
    b.
    c.try-catch-finally block.
    d.catch-finally block.



    Q6 - Q20, 15 MEDIUM level difficulty questions 2 mark each. 2 * 15 = 30 marks

  11. What will be output of following program -


  12. a.runtime exception
    b.compilation error
    c.superClass method
    d.SubClass method

  13. What will be output of following exception handling code in java -


  14. a.SubClass method
    b.compilation error
    c.runtime exception
    d.superClass method

  15. What will be output of following exception handling program -


  16. a.after calling m()
    b.runtime exception
    c.program won't compile
    d.None

  17. What will be output of following exception handling code in java -


  18. a.superClass method
    b.runtime exception
    c.compilation error
    d.SubClass method

  19. What will be output of following exception handling program -


  20. a.superClass method
    b.runtime exception
    c.SubClass method
    d.compilation error

  21. What will be output of following exception handling code in java -


  22. a.superClass method
    b.SubClass method
    c.runtime exception
    d.None of these

  23. If superclass method throws unchecked exception then overridden method of subclass can throw

  24. a.Only subclasses of exception thrown by superclass method
    b.Can't throw any exception
    c.Any checked exception
    d.Any runtime exception

  25. What will be output of following program -


  26. a.runtime exception
    b.superClass method
    c.compilation error
    d.SubClass method

  27. If superclass method throws checked exception than

  28. a.overridden method of subclass can declare any RuntimeException
    b.overridden method of subclass can declare narrower checked exception
    c.overridden method of subclass cannot throw broader checked exception
    d.All

  29. Which of these is not a one of the exception handling keyword?

  30. a.finally
    b.exception
    c.try
    d.catch

    e.throw

  31. What is invalid about java.lang.Error in java?

  32. a.Errors are abnormal conditions in application
    b.Error indicates some serious problems that our application should not try to catch
    c.Error is unchecked Exception
    d.Error is a subclass of Throwable

  33. Which of these not a Error?

  34. a.VirtualMachineError
    b.ClassNotFoundError
    c.OutOfMemoryError
    d.IOError

  35. Which is invalid about throws in Exception handling in java?

  36. a.throws can propagate exception to calling method
    b.throws can be used to throw multiple exception at time.
    c.throws is used inside method to throw exception
    d.None of these

  37. What is true about exception handling keyword throw in java

  38. a.throw is a keyword
    b.throw can be used to throw only one exception at time.
    c.throw is used inside method
    d.All

  39. Automatic resource management was introduced in which java version?

  40. a.java 5
    b.java 6
    c.java 7
    d.java 8



    Q21 - Q25, 5 HARD level difficulty questions 3 mark each.       3 * 5 = 15 marks

  41. Which of these is not a good exception handling practice?

  42. a.Throw exceptions when the method cannot handle the exception, and more importantly, must be handled by the caller
    b.log the exception and bubble it
    c.Bubble the exception if the method cannot handle it.
    d.Throw the exception right away

  43. Which are valid statements in Exception handling in java?

  44. a.Java exception handling allows us to use multiple catch block.
    b.Exception class handled in starting catch block must be subclass of Exception class handled in following catch blocks
    c.Either one of the multiple catch block will handle exception at time
    d.All

  45. Which is invalid statement in Exception handling in java?

  46. a.finally block can't throw exception
    b.try block can throw exception
    c.catch block can throw exception
    d.finally block can throw exception

  47. Which of these exceptions are propagated automatically in java?

  48. a.IOException
    b.NullPointerException
    c.ClassNotFoundException
    d.SQLException

  49. Which of these exceptions are always needed to be manually propagated?

  50. a.Error
    b.unchecked exceptions
    c.checked exceptions
    d.None




Quiz 2 - Correct answers



EASY
1) a
2) b
3) b
4) c
5) d

MEDIUM
6) d
7) b
8) b
9) d
10) d

11) b
12) d
13) c
14) d
15) b
16) c
17) b
18) c
19) d
20) c

HARD
21) b
22) d
23) a
24) b
25) c

Thread Concurrency Java - MCQ set 1 (30 questions, 68 marks)

Thread Concurrency Java - MCQ set 1 (30 questions, 68 marks)



    Q1 - Q22, 22 MEDIUM level difficulty questions 2 mark each. 2 * 22 = 44 marks 

  1. Semaphore is found in which package in thread concurrency in java?

  2. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  3. Semaphore was introduced in which java version

  4. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  5. Semaphore is a class/interface/abstract class/other?

  6. a.Class
    b.Interface
    c.Abstract class
    d.Other

  7. Callable is found in which package in thread concurrency in java?

  8. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  9. Callable was introduced in which java version

  10. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  11. Callable is a class/interface/abstract class/other?

  12. a.Class
    b.Interface
    c.Abstract class
    d.Other

  13. Future is found in which package in thread concurrency in java?

  14. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  15. Future was introduced in which java version

  16. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  17. Future is a class/interface/abstract class/other?

  18. a.Class
    b.Interface
    c.Abstract class
    d.Other

  19. RecursiveTask is found in which package in thread concurrency in java?

  20. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  21. RecursiveTask was introduced in which java version

  22. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  23. RecursiveTask is a class/interface/abstract class/other?

  24. a.Class
    b.Interface
    c.Abstract class
    d.Other

  25. ForkJoinTask is found in which package in thread concurrency in java?

  26. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  27. ForkJoinTask was introduced in which java version

  28. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  29. ForkJoinTask is a class/interface/abstract class/other?

  30. a.Class
    b.Interface
    c.Abstract class
    d.Other

  31. RecursiveAction is found in which package in thread concurrency in java?

  32. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  33. RecursiveAction was introduced in which java version

  34. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  35. RecursiveAction is a class/interface/abstract class/other?

  36. a.Class
    b.Interface
    c.Abstract class
    d.Other

  37. ForkJoinPool is found in which package in thread concurrency in java?

  38. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  39. ForkJoinPool was introduced in which java version

  40. a.java 5
    b.Java 6
    c.java 7
    d.java 8

  41. ForkJoinPool is a class/interface/abstract class/other?

  42. a.Class
    b.Interface
    c.Abstract class
    d.Other

  43. What method of java.util.concurrent.Callable interface returns value

  44. a.calling() method
    b.run() method
    c.runnable() method
    d.call() method



    Q23 - Q30, 8 HARD level difficulty questions 3 mark each.       3 * 8 = 24 marks

  45. What is significance of using Fork/Join Framework in thread concurrency in java?

  46. a.Fork/Join Framework has been added in JDK 7
    b.Fork/Join framework enables parallel programming. Parallel programming improves program performance
    c.Parallel programming means taking advantage two or more processors (multicore) in computers.
    d.All

  47. What approach does java.util.concurrent.ForkJoinPool uses for managing tasks?

  48. a.Data modelling approach
    b.Observing approach
    c.Behavioural approach
    d.work-stealing approach

  49. What is true about java.util.concurrent.RecursiveTask in thread concurrency in java?

  50. a.It provides calculate() for submitting a task but it does not return a result
    b.It provides compute() for submitting a task and returning a result
    c.It provides calculate() for submitting a task and returning a result
    d.It provides compute() for submitting a task but it does not return a result

  51. What is valid about synchronized and java.util.concurrent.locks.ReentrantLock in thread concurrency in java

  52. a.synchronized Does not provide any fair locks.
    b.ReentrantLock provides fair lock
    c.ReentrantLock provides getQueueLength() method to return number of threads that may be waiting to acquire this lock.
    d.All

  53. What is valid about call method of Callable interface in thread concurrency in java?

  54. a.call() method can only throw an exception if unable to calculate result.
    b.call() method returns computed result or throws an exception if unable to do so
    c.call() method returns computed result but cannot throw an exception if unable to do so
    d.call() method cannot return result

  55. Which method can be used to find whether lock is held by current thread?

  56. a.findLockHold()
    b.isLockHeld()
    c.holdsLock()
    d.lockHold()

  57. What is valid about java.util.concurrent.CountDownLatch in thread concurrency in java?

  58. a.java.util.concurrent.CountDownLatch can be used to launch event
    b.for completing wait in java.util.concurrent.CountDownLatch specified number of threads must call countDown() method
    c.java.util.concurrent.CountDownLatch can be awaited repeatedly
    d.None of these

  59. What is valid about arrive() and arriveAndAwaitAdvance() method of java.util.concurrent.Phaser in thread concurrency in java?

  60. a.arrive() method of java.util.concurrent.Phaser does not cause current thread to wait for other registered threads to complete current phase
    b.arriveAndAwaitAdvance() method causes current thread to wait for other registered threads to completecurrent phase
    c.All
    d.None




Quiz 1 - Correct answers



MEDIUM
1) a
2) a
3) a
4) a
5) a
6) b
7) a
8) a
9) b
10) a

11) c
12) c
13) a
14) c
15) c
16) a
17) c
18) c
19) a
20) c

21) a
22) d

HARD
23) d
24) d
25) b
26) d
27) b
28) c
29) b
30) c

Go to TOP of the page




Thread Concurrency Java - MCQ set 2 (30 questions, 68 marks)



    Q1 - Q22, 22 MEDIUM level difficulty questions 2 mark each. 2 * 22 = 44 marks 

  1. Lock is found in which package in thread concurrency in java?

  2. a.java.util.concurrent.locks
    b.java.util
    c.java.util.locks
    d.java.util.concurrent

  3. Lock was introduced in which java version

  4. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  5. Lock is a class/interface/abstract class/other?

  6. a.Class
    b.Interface
    c.Abstract class
    d.Other

  7. ReentrantLock is found in which package in thread concurrency in java?

  8. a.java.util.concurrent.locks
    b.java.util
    c.java.util.locks
    d.java.util.concurrent

  9. ReentrantLock was introduced in which java version

  10. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  11. ReentrantLock is a class/interface/abstract class/other?

  12. a.Class
    b.Interface
    c.Abstract class
    d.Other

  13. ReadWriteLock is found in which package in thread concurrency in java?

  14. a.java.util.concurrent.locks
    b.java.util
    c.java.util.locks
    d.java.util.concurrent

  15. ReadWriteLock was introduced in which java version

  16. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  17. ReadWriteLock is a class/interface/abstract class/other?

  18. a.Class
    b.Interface
    c.Abstract class
    d.Other

  19. ReentrantReadWriteLock is found in which package in thread concurrency in java?

  20. a.java.util.concurrent.locks
    b.java.util
    c.java.util.locks
    d.java.util.concurrent

  21. ReentrantReadWriteLock was introduced in which java version

  22. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  23. ReentrantReadWriteLock is a class/interface/abstract class/other?

  24. a.Class
    b.Interface
    c.Abstract class
    d.Other

  25. CountDownLatch is found in which package in thread concurrency in java?

  26. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  27. CountDownLatch was introduced in which java version

  28. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  29. CountDownLatch is a class/interface/abstract class/other?

  30. a.Class
    b.Interface
    c.Abstract class
    d.Other

  31. CyclicBarrier is found in which package in thread concurrency in java?

  32. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  33. CyclicBarrier was introduced in which java version

  34. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  35. CyclicBarrier is a class/interface/abstract class/other?

  36. a.Class
    b.Interface
    c.Abstract class
    d.Other

  37. Phaser is found in which package in thread concurrency in java?

  38. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang

  39. Phaser was introduced in which java version

  40. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  41. Phaser is a class/interface/abstract class/other?

  42. a.Class
    b.Interface
    c.Abstract class
    d.Other

  43. Exchanger is found in which package in thread concurrency in java?

  44. a.java.util.concurrent
    b.java.util
    c.java.io
    d.java.lang



    Q23 - Q30, 8 HARD level difficulty questions 3 mark each.       3 * 8 = 24 marks

  45. What are important methods of java.util.concurrent.atomic.AtomicInteger

  46. a.get()
    b.set()
    c.getAndSet(int newValue)
    d.all()

  47. What is valid about run method of java.lang.Runnable interface?

  48. a.run() method can only throw an exception if unable to calculate result.
    b.run() method returns computed result or throws an exception if unable to do so
    c.run() method does not return a result and can neither throw a checked exception
    d.run() method returns computed result but cannot throw an exception if unable to do so

  49. What does CountDownLatch(int count) constructor specifies?

  50. a.CountDownLatch is initialized with given count.
    b.count specifies the number of events that must occur before latch is released.
    c.Once count reaches 0 latch is released.
    d.All of these

  51. What happens in java.util.concurrent.CyclicBarrier class?

  52. a.2 or more threads wait for each other to reach a common barrier point.
    b.When all threads have reached common barrier point (i.e. when all threads have called await() method) all waiting threads are released
    c.When all threads have reached common barrier point (i.e. when all threads have called await() method) event can be triggered as well
    d.All

  53. What is valid about java.util.concurrent.Phaser class?

  54. a.Phaser provides us flexibility of registering and deRegistering parties at any time
    b.Phaser allows us registering and deRegistering parties in constructor only
    c.Phaser can't be awaited repeatedly
    d.All

  55. java.util.concurrent.Phaser provides which of the following for registering parties/thread?

  56. a.register() method
    b.bulkRegister(int parties) method
    c.constructor = Phaser(int parties)
    d.All

  57. We could override onAdvance() method of java.util.concurrent.Phaser to?

  58. a.advance to previous phase
    b.register the parties/threads for next phase
    c.control number of phases which we want to execute.
    d.All

  59. When Phaser is terminated?

  60. a.When calling arriveAndDeregister() method of Phaser has caused the number of registered parties to become 0.
    b.Termination can also be triggered when an onAdvance() method returns true.
    c.All
    d.None of these




Quiz 2 - Correct answers



MEDIUM
1) a
2) a
3) b
4) a
5) a
6) a
7) a
8) a
9) b
10) a

11) a
12) a
13) a
14) a
15) a
16) a
17) a
18) a
19) a
20) c

21) a
22) a

HARD
23) d
24) c
25) d
26) d
27) a
28) d
29) c
30) c

Go to TOP of the page




Thread Concurrency Java - MCQ set 3 (30 questions, 68 marks)



    Q1 - Q22, 22 MEDIUM level difficulty questions 2 mark each. 2 * 22 = 44 marks 

  1. What is java.util.concurrent.semaphore class

  2. a.provides parallel programming
    b.cannot controls access to a shared resource
    c.controls access to a shared resource by using permits
    d.None

  3. What are important methods provided by Semaphore in maintaining concurrency

  4. a.acquire()/release() methods
    b.conquer()/release() methods
    c.acquire()/drop() methods
    d.conquer()/drop() methods

  5. Exchanger was introduced in which java version

  6. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8

  7. Exchanger is a class/interface/abstract class/other?

  8. a.Class
    b.Interface
    c.Abstract class
    d.Other

  9. Which concurrency class/Interface/framework uses Divide-and-conquer strategy?

  10. a.Semaphore
    b.Phaser
    c.CyclicBarrier
    d.Fork/Join framework

  11. What is valid about Executor service framework in java

  12. a.creates thread
    b.starts threads
    c.manages whole life cycle of Threads.
    d.All

  13. AtomicInteger is found in which package?

  14. a.java.util.concurrent.atomic
    b.java.util
    c.java.util.atomic
    d.java.util.concurrent
    e.It doesn't exists till Java 8

  15. AtomicInteger was introduced in which java version

  16. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8
    e.It doesn't exists till Java 8

  17. AtomicInteger is a class/interface/abstract class/other?

  18. a.Class
    b.Interface
    c.Abstract class
    d.Other
    e.It doesn't exists till Java 8

  19. AtomicLong is found in which package?

  20. a.java.util.concurrent.atomic
    b.java.util
    c.java.util.atomic
    d.java.util.concurrent
    e.It doesn't exists till Java 8

  21. AtomicLong was introduced in which java version

  22. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8
    e.It doesn't exists till Java 8

  23. AtomicLong is a class/interface/abstract class/other?

  24. a.Class
    b.Interface
    c.Abstract class
    d.Other
    e.It doesn't exists till Java 8

  25. AtomicDouble is found in which package?

  26. a.java.util.concurrent.atomic
    b.java.util
    c.java.util.atomic
    d.java.util.concurrent
    e.It doesn't exists till Java 8

  27. AtomicDouble was introduced in which java version

  28. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8
    e.It doesn't exists till Java 8

  29. AtomicDouble is a class/interface/abstract class/other?

  30. a.Class
    b.Interface
    c.Abstract class
    d.Other
    e.It doesn't exists till Java 8

  31. AtomicBoolean is found in which package?

  32. a.java.util.concurrent.atomic
    b.java.util
    c.java.util.atomic
    d.java.util.concurrent
    e.It doesn't exists till Java 8

  33. AtomicBoolean was introduced in which java version

  34. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8
    e.It doesn't exists till Java 8

  35. AtomicBoolean is a class/interface/abstract class/other?

  36. a.Class
    b.Interface
    c.Abstract class
    d.Other
    e.It doesn't exists till Java 8

  37. AtomicFloat is found in which package?

  38. a.java.util.concurrent.atomic
    b.java.util
    c.java.util.atomic
    d.java.util.concurrent
    e.It doesn't exists till Java 8

  39. AtomicFloat was introduced in which java version

  40. a.Java 5
    b.Java 6
    c.Java 7
    d.Java 8
    e.It doesn't exists till Java 8

  41. AtomicFloat is a class/interface/abstract class/other?

  42. a.Class
    b.Interface
    c.Abstract class
    d.Other
    e.It doesn't exists till Java 8

  43. Which is valid statement

  44. a.Callable interface provides method for computing and returning result
    b.submit() method is defined in ExecutorService interface and does not return result of computation.
    c.execute() method is defined in Executor interface.
    d.All



    Q23 - Q30, 8 HARD level difficulty questions 3 mark each.       3 * 8 = 24 marks

  45. Difference between java.util.concurrent.Callable and java.lang.Runnable

  46. a.Class implementing Callable interface must override call() method. call() method returns computed result or throws an exception if unable to do so.
    b.Class implementing Runnable interface must override run() method.
    c.A Runnable does not return a result and can neither throw a checked exception.
    d.All

  47. which of these class provide support for atomic operations till java 8?

  48. a.AtomicString
    b.AtomicDouble
    c.AtomicFloat
    d.AtomicBoolean

  49. What is maximum number of parties that could be registered with phaser at a time ?

  50. a.6500
    b.65535
    c.1500
    d.100

  51. What is java.util.concurrent.Exchanger class?

  52. a.Exchanger enables two methods to exchange their data between each other
    b.Exchanger enables remote method invocation RMI
    c.Exchanger enables two threads to exchange their data between each other
    d.

  53. What are key method of Lock interface?

  54. a.lock()/unLock() methods
    b.getLock()/ setLock() methods
    c.setLock()/getUnLock() methods
    d.None

  55. What will be output of following program -


  56. a.Thread-2 is Waiting to acquire lock Thread-1 is Waiting to acquire lock Thread-1 has acquired lock. Thread-2 didn't got lock.
    b.Thread-1 is Waiting to acquire lock Thread-2 is Waiting to acquire lock Thread-1 has acquired lock. Thread-2 didn't got lock.
    c.Thread-1 is Waiting to acquire lock Thread-1 has acquired lock. Thread-2 is Waiting to acquire lock Thread-2 didn't got lock.
    d.None

  57. What is valid about java.util.concurrent.CyclicBarrier?

  58. a.CyclicBarrier can't be used to trigger event
    b.CyclicBarrier can be awaited repeatedly
    c.CyclicBarrier can't be awaited repeatedly
    d.None

  59. What will be output of following code -


  60. a.parentPhaser isTerminated : true
            childPhaser isTerminated : true
           
            --childPhaser has called arriveAndDeregister()--
           
            parentPhaser isTerminated : false
            childPhaser isTerminated : true
    b.parentPhaser isTerminated : false
            childPhaser isTerminated : true
           
            --childPhaser has called arriveAndDeregister()--
           
            parentPhaser isTerminated : true
            childPhaser isTerminated : true
    c.parentPhaser isTerminated : false
            childPhaser isTerminated : false
           
            --childPhaser has called arriveAndDeregister()--
           
            parentPhaser isTerminated : true
            childPhaser isTerminated : true
    d.parentPhaser isTerminated : true
            childPhaser isTerminated : false
            --childPhaser has called arriveAndDeregister()--
            parentPhaser isTerminated : false
            childPhaser isTerminated : true




Quiz 3 - Correct answers



MEDIUM
1) c
2) a
3) a
4) a
5) d
6) d
7) a
8) a
9) a
10) a

11) a
12) a
13) e
14) e
15) e
16) a
17) a
18) a
19) e
20) e

21) e
22) d

HARD
23) d
24) d
25) b
26) c
27) a
28) b
29) b
30) c

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

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