What is the difference between start() and run() methods in threading?
Astart() creates a new thread, run() executes in the same thread
Brun() creates a new thread, start() executes in the same thread
CBoth are identical
Dstart() is deprecated in Java 21
Correct Answer:
A. start() creates a new thread, run() executes in the same thread
EXPLANATION
start() creates a new thread and calls run() in that new thread, while directly calling run() executes it in the current thread without creating a new thread.
Which method must be implemented to create a thread in Java?
Astart()
Brun()
Cexecute()
Dbegin()
Correct Answer:
B. run()
EXPLANATION
The run() method must be implemented when creating a thread either by extending Thread class or implementing Runnable interface. The start() method calls run() internally.
What is the output of the following code?
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()));
t.start();
AThread-0
Bmain
CCompilation error
DRuntime exception
Correct Answer:
A. Thread-0
EXPLANATION
When a new Thread is created without a name parameter, it gets a default name 'Thread-n' where n is a counter. The thread will print 'Thread-0' as it is the first thread created.
In a high-concurrency scenario using Java 21, which approach is recommended for I/O-bound operations?
AVirtual Threads with structured concurrency
BTraditional platform threads with thread pools
CCallbacks without threading
DBusy-waiting loops
Correct Answer:
A. Virtual Threads with structured concurrency
EXPLANATION
Virtual Threads (Project Loom) are ideal for I/O-bound operations as they have minimal overhead and can number in millions, improving scalability significantly.