Practice 50 Kotlin Coroutines and Concurrency Coding Questions, TechnoVlogs

Practice 50 Kotlin Coroutines and Concurrency Coding Questions


Q1. Write a Kotlin program to launch a simple coroutine that prints "Hello, Kotlin!"
  Expected Output: "Hello, Kotlin!"

Q2. Write a Kotlin program to run two coroutines concurrently that print "Coroutine 1" and "Coroutine 2" respectively.
  Expected Output: "Coroutine 1"
  Expected Output: "Coroutine 2"

Q3. Write a Kotlin program to use delay inside a coroutine to print "Delayed Message" after a 1-second delay.
  Expected Output: "Delayed Message" (after 1 second)

Q4. Write a Kotlin program to launch a coroutine and use runBlocking to wait for its completion.
  Expected Output: "Coroutine Complete"

Q5. Write a Kotlin program to use a coroutine with async to compute the sum of two numbers and print the result.
  Input: a = 5, b = 10
  Expected Output: 15

Q6. Write a Kotlin program to run two coroutines concurrently using async and print the results of their operations.
  Input: a = 5, b = 10
  Expected Output: 5
  Expected Output: 10

Q7. Write a Kotlin program to demonstrate how to handle exceptions in a coroutine using try-catch.
  Input: throws Exception("Error")
  Expected Output: "Caught exception"

Q8. Write a Kotlin program to launch a coroutine that performs a simple task and returns a result using Deferred.
  Expected Output: "Task completed"

Q9. Write a Kotlin program to launch two coroutines that run in parallel and use await to get their results.
  Input: a = 5, b = 10
  Expected Output: 5
  Expected Output: 10

Q10. Write a Kotlin program to use withContext to change the dispatcher inside a coroutine and print a message.
   Expected Output: "Running on a different dispatcher"

Q11. Write a Kotlin program to use CoroutineScope to launch a coroutine and print a message.
   Expected Output: "Coroutine in scope"

Q12. Write a Kotlin program to create a coroutine that performs an asynchronous task and prints the result.
   Expected Output: "Async task result"

Q13. Write a Kotlin program to use launch inside runBlocking to execute a coroutine.
   Expected Output: "Task completed"

Q14. Write a Kotlin program that uses GlobalScope.launch to launch a coroutine that prints a message.
   Expected Output: "Global coroutine running"

Q15. Write a Kotlin program to use join to wait for a coroutine to complete before continuing the main thread.
   Expected Output: "Coroutine joined"

Q16. Write a Kotlin program to use delay inside a coroutine and wait for the task to finish.
   Expected Output: "Task done after delay"

Q17. Write a Kotlin program to demonstrate the use of async and await to calculate the sum of two numbers.
   Input: a = 10, b = 20
   Expected Output: 30

Q18. Write a Kotlin program to use withContext to switch between dispatchers within a coroutine.
   Expected Output: "Switching dispatcher"

Q19. Write a Kotlin program to handle multiple tasks concurrently using multiple coroutines with async.
   Input: tasks = [5, 10, 15]
   Expected Output: 5
   Expected Output: 10
   Expected Output: 15

Q20. Write a Kotlin program to demonstrate the difference between launch and async in coroutines.
   Expected Output: "Launch task"
   Expected Output: "Async task"

Q21. Write a Kotlin program to create a coroutine using async that computes and returns the square of a number.
   Input: n = 5
   Expected Output: 25

Q22. Write a Kotlin program to use runBlocking to block the main thread while waiting for the completion of a coroutine.
   Expected Output: "Main thread blocked"

Q23. Write a Kotlin program to use GlobalScope to launch a coroutine that runs independently.
   Expected Output: "GlobalScope task"

Q24. Write a Kotlin program to launch multiple coroutines concurrently and print their outputs using async.
   Expected Output: "Coroutine 1"
   Expected Output: "Coroutine 2"

Q25. Write a Kotlin program to use async to run multiple asynchronous operations and calculate the sum of their results.
   Input: task1 = 10, task2 = 20
   Expected Output: 30

Q26. Write a Kotlin program to demonstrate the use of yield inside a coroutine to pause execution.
   Expected Output: "Coroutine yielding"

Q27. Write a Kotlin program to use withContext to switch to a background thread and print a message.
   Expected Output: "Running in background"

Q28. Write a Kotlin program to create a coroutine that throws an exception and catches it using try-catch.
   Expected Output: "Exception caught"

Q29. Write a Kotlin program to execute a long-running task asynchronously using async and await.
   Expected Output: "Task result"

Q30. Write a Kotlin program to use CoroutineScope.launch to start a coroutine within a custom scope.
   Expected Output: "Coroutine in custom scope"

Q31. Write a Kotlin program to use launch and delay to simulate a delay within a coroutine.
   Expected Output: "Task delayed"

Q32. Write a Kotlin program to demonstrate a basic coroutine flow with runBlocking and launch.
   Expected Output: "Coroutine started"

Q33. Write a Kotlin program to use runBlocking and async to execute a coroutine and return a value.
   Expected Output: "Result from async"

Q34. Write a Kotlin program to demonstrate CoroutineScope.launch to run a coroutine in a new scope.
   Expected Output: "New coroutine scope"

Q35. Write a Kotlin program to create a coroutine using async that computes the factorial of a number.
   Input: n = 5
   Expected Output: 120

Q36. Write a Kotlin program to demonstrate coroutine cancellation with cancel in a long-running task.
   Expected Output: "Coroutine cancelled"

Q37. Write a Kotlin program to demonstrate how to use supervisorScope for handling child coroutine failures.
   Expected Output: "Parent task completed"

Q38. Write a Kotlin program to use CoroutineScope.async to run an asynchronous task and return a value.
   Expected Output: "Async task result"

Q39. Write a Kotlin program to use runBlocking to launch coroutines in a blocking manner for sequential execution.
   Expected Output: "Sequential task 1"
   Expected Output: "Sequential task 2"

Q40. Write a Kotlin program to use launch and async to compute two results and print them concurrently.
   Expected Output: Task 1 result
   Expected Output: Task 2 result

Q41. Write a Kotlin program to launch a coroutine inside runBlocking and handle an exception that occurs.
   Expected Output: "Exception caught in coroutine"

Q42. Write a Kotlin program to use withContext to change the execution context of a coroutine.
   Expected Output: "Changed context"

Q43. Write a Kotlin program to demonstrate how async can be used to return values concurrently and combine them.
   Input: a = 5, b = 10
   Expected Output: 15

Q44. Write a Kotlin program to launch multiple coroutines that each return a value and collect their results.
   Expected Output: Result 1
   Expected Output: Result 2

Q45. Write a Kotlin program to demonstrate the use of CoroutineScope and cancellation of coroutines.
   Expected Output: "Coroutine cancelled"

Q46. Write a Kotlin program to use async to fetch data from two different sources concurrently and combine the results.
   Expected Output: "Data from source 1"
   Expected Output: "Data from source 2"

Q47. Write a Kotlin program to use launch to start a coroutine and join to ensure its completion.
   Expected Output: "Coroutine joined"

Q48. Write a Kotlin program to run a coroutine that performs background work using withContext.
   Expected Output: "Background task completed"

Q49. Write a Kotlin program to simulate a task running in the background and then print a result using async.
   Expected Output: "Background task result"

Q50. Write a Kotlin program to use delay within runBlocking to simulate a non-blocking delay and return a value.
   Expected Output: "Value after delay"

Share on Social Media