Practice 70 Java Thread Programming Questions
Q1. Write a Java program to create a thread that prints "Hello, World!" five times.
Expected Output:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Q2. Write a Java program to create a thread that prints numbers from 1 to 5.
Expected Output:
1
2
3
4
5
Q3. Write a Java program to create two threads where one thread prints numbers from 1 to 5, and the other thread prints "Hello" five times.
Expected Output:
1
2
3
4
5
Hello
Hello
Hello
Hello
Hello
Q4. Write a Java program to create a thread that prints a string in reverse order.
Input:
String = "Java"
Expected Output:
avaJ
Q5. Write a Java program to demonstrate thread priority by creating two threads. The higher priority thread should print numbers from 1 to 5, and the lower priority thread should print "Java".
Expected Output:
1
2
3
4
5
Java
Q6. Write a Java program to implement a thread that counts down from 10 to 1.
Expected Output:
10
9
8
7
6
5
4
3
2
1
Q7. Write a Java program to implement a thread that prints the first 5 even numbers.
Expected Output:
2
4
6
8
10
Q8. Write a Java program that demonstrates the use of the `join()` method with two threads.
Expected Output:
First thread completed
Second thread completed
Q9. Write a Java program to create a thread that prints "Java Thread" in an infinite loop.
Expected Output:
Java Thread (prints continuously until the program is terminated)
Q10. Write a Java program to create a thread that prints numbers from 1 to 10 with a 1-second delay between each print.
Expected Output:
1 (after 1 second)
2 (after 1 second)
3 (after 1 second)
...
10
Q11. Write a Java program to demonstrate the use of the `sleep()` method with two threads.
Expected Output:
Thread 1
Thread 2 (after 1-second delay)
Q12. Write a Java program to implement a thread that prints the multiplication table of 2.
Expected Output:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
...
Q13. Write a Java program to create a thread that prints numbers from 1 to 100 in intervals of 10.
Expected Output:
1
11
21
...
91
Q14. Write a Java program to implement two threads where one prints "A" and the other prints "B" alternately.
Expected Output:
A
B
A
B
...
Q15. Write a Java program to create a thread that prints "Thread Running" every 2 seconds.
Expected Output:
Thread Running (prints every 2 seconds)
Q16. Write a Java program to create a thread that prints a countdown from 5 to 1 with a 2-second delay.
Expected Output:
5
4
3
2
1
Q17. Write a Java program to demonstrate thread synchronization by using two threads that access the same resource.
Expected Output:
Thread 1 accessing the resource
Thread 2 accessing the resource
Q18. Write a Java program to implement a thread that prints numbers from 1 to 5 and then stops.
Expected Output:
1
2
3
4
5
Q19. Write a Java program to create a thread that prints "Thread Completed" after a 3-second delay.
Expected Output:
Thread Completed (after 3 seconds)
Q20. Write a Java program to create a thread that simulates a task running for 5 seconds, then prints "Task Completed".
Expected Output:
Task Completed (after 5 seconds)
Q21. Write a Java program to implement two threads that print numbers from 1 to 10, and the other prints alphabets from A to J.
Expected Output:
1
A
2
B
3
C
...
Q22. Write a Java program to create a thread that prints "Task Started", waits for 5 seconds, and then prints "Task Ended".
Expected Output:
Task Started
(Task waits for 5 seconds)
Task Ended
Q23. Write a Java program to demonstrate thread creation using the `Runnable` interface.
Expected Output:
Thread is running
Q24. Write a Java program to create a thread that prints the Fibonacci series up to the 10th term.
Expected Output:
0
1
1
2
3
5
8
13
21
34
Q25. Write a Java program to create a thread that checks if a number is prime.
Input:
Number = 7
Expected Output:
7 is prime
Q26. Write a Java program to implement a thread that prints the square of numbers from 1 to 5.
Expected Output:
1
4
9
16
25
Q27. Write a Java program to create a thread that prints numbers from 10 to 1 in reverse order.
Expected Output:
10
9
8
...
1
Q28. Write a Java program to create a thread that simulates a delay of 3 seconds and then prints "Operation Complete".
Expected Output:
Operation Complete (after 3 seconds)
Q29. Write a Java program to create a thread that counts from 1 to 5 with a delay of 500 milliseconds between each print.
Expected Output:
1 (after 500 ms)
2 (after 500 ms)
...
Q30. Write a Java program to implement a thread that prints the prime numbers between 1 and 50.
Expected Output:
2
3
5
7
...
Q31. Write a Java program to create a thread that prints the factorial of a number.
Input:
Number = 5
Expected Output:
Factorial of 5 = 120
Q32. Write a Java program to create a thread that prints the sum of numbers from 1 to 5.
Expected Output:
Sum = 15
Q33. Write a Java program to create a thread that prints the product of numbers from 1 to 5.
Expected Output:
Product = 120
Q34. Write a Java program to demonstrate thread communication using `wait()` and `notify()`.
Expected Output:
Thread 1 waiting
Thread 2 notified
Q35. Write a Java program to create a thread that prints the current system time every second.
Expected Output:
System Time: [current time] (prints every second)
Q36. Write a Java program to create a thread that prints "Good Morning" 5 times with a 1-second delay.
Expected Output:
Good Morning
Good Morning
...
Q37. Write a Java program to implement a thread that counts the number of occurrences of a character in a string.
Input:
String = "Java programming", Character = 'a'
Expected Output:
Occurrences of 'a' = 2
Q38. Write a Java program to create a thread that calculates the square root of a number.
Input:
Number = 16
Expected Output:
Square root of 16 = 4.0
Q39. Write a Java program to demonstrate thread synchronization using `synchronized` block.
Expected Output:
Thread 1 accessing synchronized block
Thread 2 accessing synchronized block
Q40. Write a Java program to create a thread that prints "Thread Active" five times with a 2-second delay.
Expected Output:
Thread Active
Thread Active
...
Q41. Write a Java program to implement a thread that prints the first 5 multiples of 5.
Expected Output:
5
10
15
20
25
Q42. Write a Java program to create a thread that reverses an array of integers.
Input:
Array = [1, 2, 3, 4, 5]
Expected Output:
[5, 4, 3, 2, 1]
Q43. Write a Java program to create a thread that finds the largest number in an array.
Input:
Array = [1, 5, 3, 9, 2]
Expected Output:
Largest number = 9
Q44. Write a Java program to create a thread that calculates the sum of digits of a number.
Input:
Number = 123
Expected Output:
Sum of digits = 6
Q45. Write a Java program to create a thread that calculates the factorial of a number recursively.
Input:
Number = 4
Expected Output:
Factorial of 4 = 24
Q46. Write a Java program to create a thread that simulates a task taking 2 seconds to complete.
Expected Output:
Task Completed (after 2 seconds)
Q47. Write a Java program to create a thread that calculates the sum of all even numbers from 1 to 10.
Expected Output:
Sum of even numbers = 30
Q48. Write a Java program to create a thread that counts down from a given number to 1.
Input:
Number = 6
Expected Output:
6
5
4
3
2
1
Q49. Write a Java program to implement a thread that finds the square of a number.
Input:
Number = 5
Expected Output:
Square of 5 = 25
Q50. Write a Java program to create a thread that prints "Thread Running" 5 times.
Expected Output:
Thread Running
Thread Running
...
Q51. Write a Java program to create a thread that prints numbers from 1 to 10 with a delay of 1 second.
Expected Output:
1
2
...
Q52. Write a Java program to implement a thread that checks whether a given number is even or odd.
Input:
Number = 5
Expected Output:
5 is odd
Q53. Write a Java program to create a thread that finds the GCD of two numbers.
Input:
Numbers = 24, 36
Expected Output:
GCD = 12
Q54. Write a Java program to implement a thread that checks if a given string is a palindrome.
Input:
String = "madam"
Expected Output:
madam is a palindrome
Q55. Write a Java program to create a thread that prints a multiplication table for any number.
Input:
Number = 7
Expected Output:
7 x 1 = 7
7 x 2 = 14
...
Q56. Write a Java program to create a thread that finds the sum of the first 100 natural numbers.
Expected Output:
Sum = 5050
Q57. Write a Java program to create a thread that prints the Fibonacci sequence up to a given number.
Input:
Number = 10
Expected Output:
0 1 1 2 3 5 8
Q58. Write a Java program to create a thread that checks if a number is a perfect number.
Input:
Number = 6
Expected Output:
6 is a perfect number
Q59. Write a Java program to create a thread that prints numbers in an arithmetic progression.
Input:
Start = 2, Difference = 3, Count = 5
Expected Output:
2 5 8 11 14
Q60. Write a Java program to create a thread that prints the sum of squares of numbers from 1 to 5.
Expected Output:
55
Q61. Write a Java program to create a thread that prints numbers from 1 to 10 in increasing order.
Expected Output:
1
2
...
Q62. Write a Java program to create a thread that calculates the power of a number.
Input:
Base = 2, Exponent = 3
Expected Output:
2^3 = 8
Q63. Write a Java program to create a thread that prints the prime numbers up to 100.
Expected Output:
2 3 5 7 11 13 ...
Q64. Write a Java program to create a thread that computes the sum of an array of integers.
Input:
Array = [1, 2, 3, 4, 5]
Expected Output:
Sum = 15
Q65. Write a Java program to create a thread that calculates the product of all numbers in an array.
Input:
Array = [1, 2, 3, 4]
Expected Output:
Product = 24
Q66. Write a Java program to create a thread that simulates a clock (printing time every second).
Expected Output:
Time: [current time] (prints every second)
Q67. Write a Java program to create a thread that prints the first N prime numbers.
Input:
N = 5
Expected Output:
2 3 5 7 11
Q68. Write a Java program to create a thread that calculates the sum of the digits of a given number.
Input:
Number = 123
Expected Output:
Sum of digits = 6
Q69. Write a Java program to create a thread that simulates a countdown timer from a given number.
Input:
Number = 10
Expected Output:
10
9
...
Q70. Write a Java program to create a thread that prints numbers divisible by both 3 and 5 in a given range.
Input:
Range = 1 to 30
Expected Output:
15
30
---
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!