Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 651–660 of 958
Topics in Java Programming
Q.651 Medium Multithreading
What is a race condition in multithreading?
A When threads compete for CPU resources
B When the outcome of a program depends on the timing of thread execution
C When a thread runs faster than others
D When threads communicate with each other
Correct Answer:  B. When the outcome of a program depends on the timing of thread execution
EXPLANATION

A race condition occurs when multiple threads access shared data and the final result depends on the order of execution, which is unpredictable.

Take Test
Q.652 Easy Multithreading
Which method is called to make a thread wait until another thread completes?
A wait()
B join()
C sleep()
D yield()
Correct Answer:  B. join()
EXPLANATION

The join() method causes the current thread to wait until the thread on which it is called completes its execution.

Take Test
Q.653 Medium Multithreading
What will be the output of calling start() multiple times on the same thread object?
A Thread will run multiple times
B IllegalThreadStateException will be thrown
C Thread will run once and ignore subsequent calls
D It depends on the JVM implementation
Correct Answer:  B. IllegalThreadStateException will be thrown
EXPLANATION

Calling start() on an already started thread throws IllegalThreadStateException because a thread can only be started once.

Take Test
Q.654 Medium Multithreading
Which of the following is NOT a thread state in Java?
A NEW
B RUNNABLE
C WAITING
D SLEEPING
Correct Answer:  D. SLEEPING
EXPLANATION

The thread states are NEW, RUNNABLE, RUNNING, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SLEEPING is not an official state.

Take Test
Q.655 Medium Multithreading
What is the purpose of the synchronized keyword in Java multithreading?
A To increase thread speed
B To prevent thread interference and memory consistency errors
C To create new threads automatically
D To pause all threads
Correct Answer:  B. To prevent thread interference and memory consistency errors
EXPLANATION

The synchronized keyword provides mutual exclusion to ensure that only one thread can access a resource at a time, preventing race conditions.

Take Test
Q.656 Easy Multithreading
Which method is used to stop a thread in Java?
A stop()
B interrupt()
C terminate()
D kill()
Correct Answer:  B. interrupt()
EXPLANATION

The interrupt() method is the proper way to stop a thread in Java. The stop() method is deprecated and dangerous.

Take Test
Q.657 Medium Multithreading
What is the difference between Thread.sleep() and Thread.yield()?
A sleep() pauses the thread, yield() makes it runnable again
B yield() pauses the thread, sleep() makes it runnable again
C sleep() causes the thread to sleep for a specified time, yield() hints to scheduler to give chance to other threads
D Both are identical in functionality
Correct Answer:  C. sleep() causes the thread to sleep for a specified time, yield() hints to scheduler to give chance to other threads
EXPLANATION

Thread.sleep() pauses execution for a specified milliseconds, while yield() is a hint to the scheduler that the current thread is willing to yield its turn.

Take Test
Q.658 Easy Multithreading
Which interface must be implemented to create a thread in Java?
A Runnable
B Threadable
C Executable
D Processable
Correct Answer:  A. Runnable
EXPLANATION

The Runnable interface is the correct interface to implement for creating threads in Java. It has a single abstract method run().

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
What will be printed? Queue queue = new LinkedList(); queue.add(10); queue.add(20); queue.add(30); System.out.println(queue.poll()); System.out.println(queue.peek());
A 10 then 20
B 10 then 10
C 30 then 20
D 20 then 30
Correct Answer:  A. 10 then 20
EXPLANATION

LinkedList implements Queue with FIFO behavior. poll() removes and returns 10. peek() returns 20 without removing it. Queue now contains [20, 30].

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