Quiz about Java Quiz Day 45

Java Quiz Day 45

Last Updated :
Discuss
Comments

Question 1

What is the purpose of thread synchronization in Java?

  • A

    To make threads execute independently

  • B

    To prevent thread interference and ensure thread safety

  • C

    To make threads run faster

  • D

    To allow threads to execute in parallel

Question 2

Which keyword is used in Java to implement synchronization?

  • A

    synchronized

  • B

    lock

  • C

    synchronizedBlock

  • D

    threadSafe

Question 3

What is a volatile variable in Java?

  • A

    A variable that is not affected by thread synchronization

  • B

    A variable that ensures changes are immediately visible to all threads

  • C

    A variable that can only be accessed by one thread at a time

  • D

    A variable that is stored in a cache for faster access

Question 4

Which of the following statements is true about synchronized methods?

  • A

    Only one thread can access a synchronized method at a time

  • B

    Synchronized methods allow multiple threads to access at the same time



  • C

    Synchronized methods are faster than non-synchronized methods

  • D

    Synchronization is not necessary for methods

Question 5

What is the difference between volatile and synchronized in Java?

  • A

    volatile ensures that only one thread can access a variable, while synchronized makes the entire method thread-safe

  • B

    volatile ensures visibility of changes across threads, while synchronized ensures mutual exclusion

  • C

    volatile is used for locking threads, while synchronized is used for creating thread-safe variables

  • D

    volatile is used in low-level memory management, while synchronized controls thread execution order

Question 6

Which of the following is a potential risk of thread synchronization?

  • A

    Deadlock

  • B

    Thread starvation

  • C

    Race conditions

  • D

    All of the above

Question 7

Which method is used to synchronize a block of code in Java?

  • A

    synchronized()

  • B

    synchronized block

  • C

    synchronized

  • D

    lock()

Question 8

What is the primary advantage of using the ReentrantLock over synchronized blocks?

  • A

    It does not support interrupting thread execution

  • B

    It allows more advanced lock management, such as try-lock

  • C

    It is faster than synchronized blocks

  • D

    It is not thread-safe

Question 9

Which of the following is NOT an advantage of using synchronization?

  • A

    It prevents race conditions

  • B

    It improves performance by limiting thread access

  • C

    It ensures mutual exclusion

  • D

    It makes code thread-safe

Question 10

What is the purpose of the wait() method in synchronization?

  • A

    To stop a thread from executing

  • B

    To pause the current thread and allow other threads to execute

  • C

    To block all threads from executing

  • D

    To execute a thread at a specified time

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion