Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 141–150 of 212
Topics in Java Programming
Q.141 Hard Multithreading
Which class is used to synchronize the execution of multiple threads at a specific point?
A Semaphore
B CyclicBarrier
C CountDownLatch
D Condition
Correct Answer:  B. CyclicBarrier
EXPLANATION

CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.

Take Test
Q.142 Hard Multithreading
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
A Deadlock will occur
B ReentrantException will be thrown
C Code will execute without issues due to reentrant locks
D Compilation error
Correct Answer:  C. Code will execute without issues due to reentrant locks
EXPLANATION

Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.

Take Test
Q.143 Hard Multithreading
Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
A Race condition
B Starvation
C Deadlock
D Livelock
Correct Answer:  C. Deadlock
EXPLANATION

This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.

Take Test
Q.144 Hard Multithreading
Which interface allows multiple threads to access a resource with a permit system?
A Runnable
B Semaphore
C Lock
D Executor
Correct Answer:  B. Semaphore
EXPLANATION

Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.

Take Test
Q.145 Hard Multithreading
What will happen if you call sleep() inside a synchronized block?
A The lock is released during sleep
B The lock is retained during sleep, blocking other threads
C An exception is thrown
D The synchronized block is skipped
Correct Answer:  B. The lock is retained during sleep, blocking other threads
EXPLANATION

When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.

Take Test
Q.146 Hard Multithreading
How does the volatile keyword help in multithreading?
A It prevents race conditions completely
B It ensures visibility of variable changes across threads without full synchronization
C It increases thread speed
D It prevents deadlocks
Correct Answer:  B. It ensures visibility of variable changes across threads without full synchronization
EXPLANATION

The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.

Take Test
Q.147 Hard Multithreading
What happens when wait() is called from a thread that doesn't hold the lock?
A The thread waits indefinitely
B IllegalMonitorStateException is thrown
C The program exits
D The thread continues normally
Correct Answer:  B. IllegalMonitorStateException is thrown
EXPLANATION

wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.

Take Test
In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?
A WeakHashMap
B IdentityHashMap
C EnumMap
D ConcurrentHashMap
Correct Answer:  A. WeakHashMap
EXPLANATION

WeakHashMap uses weak references for keys. When a key is no longer referenced elsewhere, it becomes eligible for garbage collection and is automatically removed from the map.

Take Test
Consider this code: List list = new CopyOnWriteArrayList(); list.add("A"); list.add("B"); Iterator itr = list.iterator(); list.add("C"); System.out.println(itr.next());
A Throws ConcurrentModificationException
B Prints 'A'
C Prints 'C'
D Throws NoSuchElementException
Correct Answer:  B. Prints 'A'
EXPLANATION

CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.

Take Test
What is the main advantage of EnumSet over HashSet when working with Enum constants?
A EnumSet is thread-safe
B EnumSet uses bit vectors internally for better performance
C EnumSet allows null elements
D EnumSet maintains insertion order
Correct Answer:  B. EnumSet uses bit vectors internally for better performance
EXPLANATION

EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.

Take 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