Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 571–580 of 958
Topics in Java Programming
Q.571 Hard Multithreading
Consider a scenario with 3 threads updating a shared counter. Which synchronization mechanism is MOST efficient?
A synchronized block on counter
B AtomicInteger
C ReentrantLock
D Semaphore with permits=1
Correct Answer:  B. AtomicInteger
EXPLANATION

AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.

Test
Q.572 Medium Multithreading
What exception does CyclicBarrier throw when a thread is interrupted while waiting?
A InterruptedException
B BrokenBarrierException
C CyclicBarrierException
D ThreadInterruptedException
Correct Answer:  A. InterruptedException
EXPLANATION

When a thread waiting at a CyclicBarrier is interrupted, it throws InterruptedException. This breaks the barrier and causes other waiting threads to receive BrokenBarrierException.

Test
Q.573 Medium Multithreading
What is the output of this code snippet?
ExecutorService es = Executors.newFixedThreadPool(2);
es.execute(() -> System.out.println("Task 1"));
es.shutdown();
A Task 1 will be executed before the program terminates
B Task 1 may or may not be executed
C Compilation error
D Task 1 will never execute
Correct Answer:  A. Task 1 will be executed before the program terminates
EXPLANATION

shutdown() gracefully shuts down the executor service by rejecting new tasks but allowing submitted tasks to complete. Task 1 is already submitted before shutdown(), so it will execute.

Test
Q.574 Medium Multithreading
In Java 21, which new feature was introduced for concurrent programming?
A Virtual Threads (Project Loom)
B ReentrantLock
C CountDownLatch
D Phaser
Correct Answer:  A. Virtual Threads (Project Loom)
EXPLANATION

Java 21 introduced Virtual Threads as part of Project Loom, which are lightweight threads that make it easier to write scalable concurrent applications.

Test
Q.575 Medium Multithreading
What does the volatile keyword guarantee in multithreading?
A Prevents thread access completely
B Visibility of changes across threads and prevents instruction reordering
C Makes operations atomic
D Automatically synchronizes all methods
Correct Answer:  B. Visibility of changes across threads and prevents instruction reordering
EXPLANATION

volatile ensures that changes to a variable are immediately visible to all threads and prevents the JVM from reordering instructions. However, it doesn't make operations atomic.

Test
Q.576 Medium Multithreading
Which Java class provides thread-safe operations using Compare-And-Swap (CAS)?
A Vector
B Collections.synchronizedList()
C AtomicInteger
D ConcurrentHashMap
Correct Answer:  C. AtomicInteger
EXPLANATION

AtomicInteger and other Atomic* classes use CAS operations for lock-free thread-safe operations. ConcurrentHashMap uses segment-based locking, not CAS.

Test
Q.577 Medium Multithreading
What is the purpose of the yield() method in Java threading?
A To permanently stop the current thread
B To suggest that the current thread should yield CPU to other threads of equal priority
C To make a thread wait indefinitely
D To increase thread priority
Correct Answer:  B. To suggest that the current thread should yield CPU to other threads of equal priority
EXPLANATION

yield() is a hint to the thread scheduler that the current thread is willing to yield its current use of CPU. It doesn't guarantee the thread will yield, as scheduling is JVM-dependent.

Test
Q.578 Medium Multithreading
Consider the code:
synchronized void method1() { wait(); }
If wait() is called without a lock, what happens?
A The thread waits indefinitely
B IllegalMonitorStateException is thrown
C The method returns normally
D Compilation error occurs
Correct Answer:  B. IllegalMonitorStateException is thrown
EXPLANATION

wait() must be called from within a synchronized block or method. If called outside synchronized context, it throws IllegalMonitorStateException at runtime.

Test
Q.579 Medium Multithreading
What will happen if you try to call start() on a thread that has already completed execution?
A The thread will start again
B IllegalThreadStateException will be thrown
C The program will hang
D No error, start() will be ignored
Correct Answer:  B. IllegalThreadStateException will be thrown
EXPLANATION

Once a thread completes (reaches TERMINATED state), calling start() again throws IllegalThreadStateException. A thread can only be started once.

Test
Q.580 Easy Multithreading
Which of the following thread states is NOT a valid Java thread state?
A RUNNABLE
B BLOCKED
C SUSPENDED
D TERMINATED
Correct Answer:  C. SUSPENDED
EXPLANATION

Java thread states are: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SUSPENDED is not a valid state in Java's threading model.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips