Challenge your understanding of Java multithreading concepts, synchronization mechanisms, thread life cycles, and concurrent programming techniques crucial for backend development. This quiz covers core terms, methods, and safe practices essential for robust multithreaded Java applications.
Which of the following methods is used to create a new thread in Java by implementing an interface?
Explanation: The correct way to create a thread is by implementing the Runnable interface and passing it to a Thread object. Serializable and Scanner are unrelated to threads; Serializable deals with object serialization, and Scanner is for input. Math is a utility class and cannot be extended.
Which method should you call to begin execution of a thread in Java after creating it?
Explanation: Calling start() initiates a new thread and calls its run() method in a separate call stack. Calling run() directly executes the code in the current thread, not a new one. There are no init() or launch() methods for threads in Java.
In which state does a thread wait for another thread to finish executing using the join() method?
Explanation: A thread entering join() transitions to the Waiting state until the other thread completes. The New state is before start() is called, Terminated is when a thread is finished, and Blocked refers to waiting for a monitor lock.
Which keyword is used to prevent multiple threads from executing a block of code at the same time in Java?
Explanation: The synchronized keyword ensures that only one thread can access a code block at a time. Static changes method or variable context, transient is for serialization, and volatile deals with memory visibility but not atomicity.
What can happen if two threads update a shared variable without proper synchronization?
Explanation: Unsynchronized access to shared variables can cause race conditions, where outcomes depend on timing. Deadlock involves two threads waiting on each other. Thread starvation is about one thread never progressing, while an infinite loop is unrelated here.
What does the Thread.sleep(1000) statement achieve in Java?
Explanation: Thread.sleep(1000) makes the executing thread sleep for 1 second (1000 milliseconds). It does not interrupt threads, does not start a new one, and cannot terminate the JVM. The pause only affects the current thread.
Why would you declare a shared variable as volatile in Java multithreading?
Explanation: Volatile guarantees that updates to a variable are visible to all threads instantly. It does not make a variable immutable or synchronize execution, nor does it set a default value. It helps with visibility but not atomicity.
What exception may be thrown when a thread is interrupted while sleeping in Java?
Explanation: InterruptedException is thrown when a sleeping or waiting thread is interrupted. IOException is for input/output errors, NullPointerException is for null references, and ClassNotFoundException relates to class loading issues.
Which statement accurately describes a daemon thread in Java?
Explanation: Daemon threads support other threads and do not keep the JVM running after user threads finish. They do not end forcibly on start, do not necessarily have higher priority, and join() is not required for their termination.
How can you set a Java thread's execution priority using code?
Explanation: The setPriority method assigns a priority to a thread. Assigning directly with a property or via the constructor is not possible. Synchronized does not affect thread priority.
What does it mean for a class to be thread-safe in Java?
Explanation: Thread-safe classes prevent data races and ensure correct results even with concurrent access. Only allowing one instance relates to the singleton pattern. Execution in main thread or memory consumption is unrelated to thread safety.
What is a key difference between the Runnable and Callable interfaces in Java?
Explanation: Callable allows returning a value and throwing checked exceptions, unlike Runnable, which cannot do either. Callable can be run multiple times and Runnable is not required to throw exceptions.
Which Java class can be used to perform atomic operations on integer values?
Explanation: AtomicInteger provides methods for atomic integer operations suitable for concurrency. SynchronizedString is not a standard class, ThreadLocal manages thread-specific variables, and HashMap is not thread-safe by default.
Why might you use a thread pool for handling multiple concurrent tasks in Java?
Explanation: Thread pools manage threads efficiently, reducing creation overhead for each task. They do not guarantee serial execution, memory limitation, or data persistence automatically.
What is the effect of declaring a method as synchronized in Java?
Explanation: Synchronized methods allow only one thread at a time per object instance. It has no effect on speed, execution count, or memory consumption directly.
What is a ThreadGroup used for in Java multithreading?
Explanation: ThreadGroup helps structure and control collections of threads collectively. It does not control inheritance, does not directly set priorities, and is not related to running on different JVMs.
Which is true about the wait() method compared to sleep() in Java threading?
Explanation: wait() pauses a thread and releases the object's lock, allowing others access, while sleep() keeps the lock. wait() does not affect speed, sleep() can be called anywhere, and neither throws IOException.
Which method signals a thread to stop, letting it finish its current task in Java?
Explanation: interrupt() sets the thread's interrupted status, which can be checked for graceful termination. suspend() and destroy() are deprecated or nonexistent, and pause() is not a thread method.
What is the default name of the main thread created by the JVM in Java?
Explanation: The default name of the entry point thread is 'main'. The other options may seem plausible but are not used by the JVM.
What is the effect of calling Thread.yield() in Java?
Explanation: Thread.yield() suggests the thread scheduler to move the current thread to runnable and let others execute. It does not force waiting, raise priority, or terminate the thread.
What is the primary purpose of the ThreadLocal class in Java?
Explanation: ThreadLocal ensures each thread gets a separate instance of a variable. It does not synchronize access, create new threads, or pool tasks together.