
Practice 50 Threads and Synchronization Coding Questions
Q1. Write a Rust program to create a simple thread that prints "Hello, World!"
Expected Output:
Hello, World!
Q2. Write a Rust program to create multiple threads and make them print their thread ID.
Expected Output:
Thread ID: 1
Thread ID: 2
Thread ID: 3
Q3. Write a Rust program to spawn a thread and pass data to it via closures.
Expected Output:
Data from thread: 42
Q4. Write a Rust program to create two threads that print numbers 1 to 5 in order.
Expected Output:
1
2
3
4
5
1
2
3
4
5
Q5. Write a Rust program to use join to wait for a thread to complete.
Expected Output:
Thread completed
Q6. Write a Rust program to use a thread pool to run multiple threads concurrently.
Expected Output:
Task 1 completed
Task 2 completed
Task 3 completed
Q7. Write a Rust program to create a thread and pass mutable data to it using Arc and Mutex.
Expected Output:
Data inside Mutex: 1
Data inside Mutex: 2
Q8. Write a Rust program to create a thread that calculates the sum of an array of numbers.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Sum of the array: 10
Q9. Write a Rust program to create two threads that increment a shared counter using Mutex.
Expected Output:
Counter value: 2
Q10. Write a Rust program to create a thread that squares a number passed from the main thread.
Input:
Number: 4
Expected Output:
Squared value: 16
Q11. Write a Rust program to use thread::spawn to perform parallel computations and return the results.
Input:
Computations: x = 2, y = 3
Expected Output:
Results: 4, 9
Q12. Write a Rust program to demonstrate the deadlock scenario with Mutex.
Expected Output:
Deadlock detected!
Q13. Write a Rust program to spawn a thread that computes Fibonacci numbers.
Input:
Fibonacci index: 5
Expected Output:
Fibonacci number: 5
Q14. Write a Rust program to use Arc and Mutex to share a string between threads.
Expected Output:
Shared string: "Hello, Rust!"
Q15. Write a Rust program to create two threads that perform different tasks concurrently (e.g., printing numbers and letters).
Expected Output:
1
A
2
B
3
C
Q16. Write a Rust program to use thread::sleep to delay a thread execution.
Expected Output:
Thread delayed by 2 seconds
Q17. Write a Rust program to create a thread that calculates the factorial of a number.
Input:
Number: 5
Expected Output:
Factorial: 120
Q18. Write a Rust program to spawn threads that each calculate the product of elements in an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Product: 24
Q19. Write a Rust program to demonstrate how to use mpsc channels for communication between threads.
Expected Output:
Message from thread: "Hello from the thread!"
Q20. Write a Rust program to spawn two threads that increment a shared counter using Arc and Mutex.
Expected Output:
Counter value: 100
Q21. Write a Rust program that spawns multiple threads to calculate the sum of different segments of an array.
Input:
Array: [1, 2, 3, 4, 5, 6, 7, 8]
Expected Output:
Sum of segments: 36
Q22. Write a Rust program to spawn a thread and capture its output using thread::spawn and join.
Expected Output:
Thread output: "Task completed"
Q23. Write a Rust program to use Condvar for synchronization between threads.
Expected Output:
Thread 1 waiting, Thread 2 signaling
Q24. Write a Rust program to use Mutex to synchronize access to a vector between threads.
Expected Output:
Thread 1 added: 1
Thread 2 added: 2
Vector: [1, 2]
Q25. Write a Rust program to spawn multiple threads that all access and update a shared value.
Expected Output:
Final value: 10
Q26. Write a Rust program to demonstrate a race condition with shared data.
Expected Output:
Race condition occurred!
Q27. Write a Rust program to use thread::spawn and join to calculate the average of numbers in a list concurrently.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Average: 3
Q28. Write a Rust program to spawn a thread that performs a task and prints the execution time.
Expected Output:
Thread execution time: 1.2 seconds
Q29. Write a Rust program that uses thread::spawn to parallelize matrix multiplication.
Input:
Matrix A: [[1, 2], [3, 4]], Matrix B: [[5, 6], [7, 8]]
Expected Output:
Result Matrix: [[19, 22], [43, 50]]
Q30. Write a Rust program to use Arc and Mutex to safely share data between threads.
Expected Output:
Shared value: 100
Q31. Write a Rust program to spawn a thread and use thread::sleep to pause it for a few seconds.
Expected Output:
Thread sleeping for 3 seconds
Q32. Write a Rust program to spawn multiple threads that calculate prime numbers up to a certain limit.
Input:
Limit: 10
Expected Output:
Prime numbers: 2, 3, 5, 7
Q33. Write a Rust program to use thread::spawn and join to sum the elements of a matrix.
Input:
Matrix: [[1, 2], [3, 4]]
Expected Output:
Sum of matrix: 10
Q34. Write a Rust program to use thread::spawn to download multiple files concurrently.
Expected Output:
File 1 downloaded
File 2 downloaded
Q35. Write a Rust program to implement parallel sorting using threads.
Input:
Array: [3, 1, 4, 5, 2]
Expected Output:
Sorted array: [1, 2, 3, 4, 5]
Q36. Write a Rust program to create a thread that checks whether a number is prime.
Input:
Number: 7
Expected Output:
7 is a prime number.
Q37. Write a Rust program to use thread::spawn to run multiple tasks concurrently and return the results.
Input:
Task: Task1, Task2
Expected Output:
Task1 completed
Task2 completed
Q38. Write a Rust program to spawn a thread that performs file I/O operations.
Expected Output:
File written successfully
Q39. Write a Rust program to use thread::spawn and join to calculate the factorial of a number in a thread.
Input:
Number: 6
Expected Output:
Factorial: 720
Q40. Write a Rust program to use mpsc for thread communication where one thread sends data to another.
Expected Output:
Data received: "Hello"
Q41. Write a Rust program to demonstrate thread synchronization using a Mutex.
Expected Output:
Mutex locked by thread 1
Mutex locked by thread 2
Q42. Write a Rust program to spawn a thread to calculate the average of numbers using a shared mutable vector.
Input:
Numbers: [1, 2, 3, 4, 5]
Expected Output:
Average: 3
Q43. Write a Rust program to spawn multiple threads and collect their results using mpsc channels.
Expected Output:
Results: 10, 20, 30
Q44. Write a Rust program to spawn threads that download multiple files concurrently and print their sizes.
Expected Output:
File 1 size: 1024 bytes
File 2 size: 2048 bytes
Q45. Write a Rust program to spawn multiple threads and calculate the sum of elements in parts of an array.
Input:
Array: [1, 2, 3, 4, 5, 6]
Expected Output:
Sum: 21
Q46. Write a Rust program to spawn threads that implement a parallel search for an element in an array.
Input:
Array: [1, 2, 3, 4], Search element: 3
Expected Output:
Element found: 3
Q47. Write a Rust program to create threads and wait for them to finish using join.
Expected Output:
All threads completed
Q48. Write a Rust program to spawn threads that calculate the product of elements in different parts of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Product: 24
Q49. Write a Rust program to use Arc and Mutex for safely sharing data between threads.
Expected Output:
Final value: 50
Q50. Write a Rust program to implement parallel execution of multiple tasks using threads.
Expected Output:
Task 1 completed
Task 2 completed