Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Multithreading Practice

Threads via Thread class and Runnable, join, sleep and simple synchronization.

1
Extend Thread Class
Easy
Create a class MyThread extends Thread whose run() prints "Thread running". In main, create and start it. Print "Main running" after start().
No input.
Main running may appear before or after Thread running (order not fixed).
Sample Input 1

                      
Sample Output 1
Main running
Thread running
Call start(), not run().
2
Implement Runnable
Easy
Create a class MyRunnable implements Runnable whose run() prints "Runnable running". In main create a Thread with it and start it. Then print "Main done".
No input.
Main done and Runnable running in either order.
Sample Input 1

                      
Sample Output 1
Main done
Runnable running
Thread t = new Thread(new MyRunnable()).
1
Thread Name
Medium
In main, read a name for a thread. Create a Thread whose run() prints Thread.currentThread().getName(). Start it and print the name in main as "Main: ".
A single line: thread name.
Thread: (from the thread) and Main: .
Sample Input 1
Worker
Sample Output 1
Thread: Worker
Main: Worker
Name length
setName() or constructor super(name).
2
Sleep in Thread
Medium
Create a thread that prints "Start", sleeps 100 ms, then prints "End". In main start it, sleep 50 ms, and print "Main". Order: Start, Main, End.
No input.
Start then Main then End.
Sample Input 1

                      
Sample Output 1
Start
Main
End
Thread.sleep throws InterruptedException.
3
Join a Thread
Medium
Create a thread that prints numbers 1 to 3 (each on its own line). In main, start it and call join(). Then print "Finished". Because of join, numbers always come before Finished.
No input.
1,2,3 then Finished.
Sample Input 1

                      
Sample Output 1
1
2
3
Finished
join() waits for the thread to die.
4
Two Threads with Runnable
Medium
Create two threads (both Runnable) that each print their given message 3 times. Read two messages A and B. Start both. Print "Both started" first. Output lines from A and B may interleave.
Two lines: message A, message B.
Both started, then three A-lines and three B-lines in any order.
Sample Input 1
Hi
Bye
Sample Output 1
Both started
Hi
Bye
Hi
Bye
Hi
Bye
Message length
Pass the message into the Runnable.
1
Synchronized Counter
Hard
Create a Counter class with an int count and a synchronized method void increment(). Create two threads that each call increment() 1000 times. In main, join both and print the final count (must be 2000).
No input.
2000
Sample Input 1

                      
Sample Output 1
2000
synchronized prevents race conditions.
2
Producer-Consumer (Simple)
Hard
Create a shared Box class with a value and synchronized put(int)/get() methods using wait/notify. The producer thread puts 5 values (1..5); the consumer prints each. Read nothing; output is 1..5 each on a line.
No input.
1 to 5, each on its own line.
Sample Input 1

                      
Sample Output 1
1
2
3
4
5
wait() and notifyAll() coordinate threads.
3
Daemon Thread
Hard
Create a daemon thread that loops printing "Daemon tick" forever (use while(true) with sleep 50ms). In main, start it as daemon, sleep 150 ms, then print "Main ending" and exit.
No input.
A few "Daemon tick" lines then "Main ending". The daemon dies when main exits.
Sample Input 1

                      
Sample Output 1
Daemon tick
Daemon tick
Daemon tick
Main ending
setDaemon(true) before start().
4
Thread Priority
Hard
Create a thread, set its priority to MAX_PRIORITY, and print its priority inside run() as "Priority: 10". In main also print the main thread priority using Thread.currentThread().getPriority() as "Main: 5".
No input.
Priority: 10 (child) and Main: 5 (or whatever the main priority is).
Sample Input 1

                      
Sample Output 1
Priority: 10
Main: 5
setPriority(Thread.MAX_PRIORITY).
Competitive MCQs — Java Multithreading
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 29
Q1
Which method starts a thread?
Correct!
Wrong — correct answer is .
start() spawns the thread; run() holds the body.
Q2
Which interface represents a task that can run on a thread?
Correct!
Wrong — correct answer is .
Runnable has run() and can be given to a Thread.
Q3
Which class creates a thread by extending it?
Correct!
Wrong — correct answer is .
class MyThread extends Thread and overrides run().
Q4
What is the output of this code?
java
1
2
3
4
5
6
class T extends Thread {
    public void run() {
        System.out.println("Run");
    }
}
new T().start();
Correct!
Wrong — correct answer is .
start() eventually invokes run(), printing "Run".
Q5
What is the output of this code?
class T extends Thread {
public void run() {
System.out.println("Run");
}
}
new T().start();
Correct!
Wrong — correct answer is .
start() eventually invokes run(), printing "Run".
Q6
What does join() do?
Correct!
Wrong — correct answer is .
t.join() blocks the caller until thread t dies.
Q7
Which keyword makes a block/method safe from concurrent interference?
Correct!
Wrong — correct answer is .
synchronized serializes access to the critical section.
Q8
What is a daemon thread?
Correct!
Wrong — correct answer is .
Daemon threads run in the background and stop with the JVM.
Q9
Which method pauses a thread for milliseconds?
Correct!
Wrong — correct answer is .
Thread.sleep(ms) suspends the thread; it throws InterruptedException.
Q10
What is a race condition?
Correct!
Wrong — correct answer is .
Uncoordinated access to shared state causes races.
Q11
Which method causes the current thread to give up the CPU?
Correct!
Wrong — correct answer is .
yield(), sleep(), and wait() all pause the current thread.
Q12
What is the output of this code?
Thread t = new Thread(() -> System.out.println("run"));
t.start();
Correct!
Wrong — correct answer is .
start() launches the thread whose task prints "run".
Q13
Which method should contain the thread's work?
Correct!
Wrong — correct answer is .
run() holds the logic; start() schedules it.
Q14
What is the output of this code?
class T extends Thread {
public void run() {
System.out.print("T");
}
}
new T().run();
Correct!
Wrong — correct answer is .
Calling run() directly executes it in the current thread.
Q15
What does join() accomplish?
Correct!
Wrong — correct answer is .
t.join() blocks the caller until t terminates.
Q16
Which keyword synchronizes access to a block?
Correct!
Wrong — correct answer is .
synchronized serializes concurrent access to critical sections.
Q17
Which method pauses a thread for 100 ms?
Correct!
Wrong — correct answer is .
sleep(ms) suspends the thread; it throws InterruptedException.
Q18
Which exception must sleep() handle?
Correct!
Wrong — correct answer is .
sleep() declares the checked InterruptedException.
Q19
What is a thread pool?
Correct!
Wrong — correct answer is .
Executors provide pools that recycle threads for many tasks.
Q20
Which class submits tasks to a pool?
Correct!
Wrong — correct answer is .
ExecutorService manages a pool and accepts submitted tasks.
Q21
What is deadlock?
Correct!
Wrong — correct answer is .
Circular lock waits make threads block indefinitely.
Q22
What is the output of this code?
Thread t = new Thread(() -> System.out.println("A"));
t.start();
t.join();
System.out.println("B");
Correct!
Wrong — correct answer is .
join() makes main wait until the thread prints A.
Q23
Which keyword marks a variable visible across threads?
Correct!
Wrong — correct answer is .
volatile ensures visibility of writes across threads.
Q24
What does synchronized guarantee?
Correct!
Wrong — correct answer is .
Only one thread holds the lock at a time.
Q25
What is the output of this code?
class Counter {
int c = 0;
synchronized void inc() {
c++;
}
}
Counter c = new Counter();
c.inc();
System.out.println(c.c);
Correct!
Wrong — correct answer is .
One synchronized increment raises c to 1.
Q26
Which is a lambda-compatible thread task?
Correct!
Wrong — correct answer is .
Runnable is a functional interface, so a lambda works.
Q27
What does the Thread constructor with a Runnable do?
Correct!
Wrong — correct answer is .
The Runnable supplies the run() body for the new thread.
Q28
How many times can start() be called on one thread?
Correct!
Wrong — correct answer is .
A thread cannot be restarted after it has started.
Q29
Which is the main thread created by?
Correct!
Wrong — correct answer is .
The JVM launches the main thread to run main().