
Practice 50 Kotlin Testing in Kotlin Coding Questions
Q1. Write a Kotlin program to create a unit test that checks if a given string is empty.
Input: "hello"
Expected Output: false
Q2. Write a Kotlin program to test if a number is even or odd.
Input: 4
Expected Output: even
Q3. Write a Kotlin program to create a test for checking if a number is prime.
Input: 7
Expected Output: true
Q4. Write a Kotlin program to test if a number is a palindrome.
Input: 121
Expected Output: true
Q5. Write a Kotlin program to test if a string is a valid email.
Input: "test@example.com"
Expected Output: true
Q6. Write a Kotlin program to test if a given list contains a specific element.
Input: [1, 2, 3, 4], element: 3
Expected Output: true
Q7. Write a Kotlin program to test if a given number is divisible by 5.
Input: 10
Expected Output: true
Q8. Write a Kotlin program to test the sum of two integers.
Input: 3, 5
Expected Output: 8
Q9. Write a Kotlin program to test if the length of a string is greater than 5.
Input: "Hello"
Expected Output: false
Q10. Write a Kotlin program to test if a number is within a given range.
Input: 15, range: 10..20
Expected Output: true
Q11. Write a Kotlin program to test if a string contains a specific substring.
Input: "Kotlin Programming", substring: "Kotlin"
Expected Output: true
Q12. Write a Kotlin program to test the equality of two objects.
Input: val a = "Hello", val b = "Hello"
Expected Output: true
Q13. Write a Kotlin program to test if an integer is a multiple of 3.
Input: 9
Expected Output: true
Q14. Write a Kotlin program to test if a number is greater than 0.
Input: -5
Expected Output: false
Q15. Write a Kotlin program to test if a list is sorted in ascending order.
Input: [1, 2, 3]
Expected Output: true
Q16. Write a Kotlin program to test if a string starts with a specific character.
Input: "Kotlin", character: "K"
Expected Output: true
Q17. Write a Kotlin program to test if a string ends with a specific character.
Input: "Kotlin", character: "n"
Expected Output: true
Q18. Write a Kotlin program to test if a given map contains a specific key.
Input: {1: "One", 2: "Two"}, key: 2
Expected Output: true
Q19. Write a Kotlin program to test if a string contains only digits.
Input: "12345"
Expected Output: true
Q20. Write a Kotlin program to test if a list contains only unique elements.
Input: [1, 2, 3, 4]
Expected Output: true
Q21. Write a Kotlin program to test if a given date is in the future.
Input: "2025-01-01"
Expected Output: true
Q22. Write a Kotlin program to test if a given number is negative.
Input: -10
Expected Output: true
Q23. Write a Kotlin program to test the maximum of two numbers.
Input: 5, 10
Expected Output: 10
Q24. Write a Kotlin program to test if an element is removed from a list.
Input: [1, 2, 3], remove element: 2
Expected Output: [1, 3]
Q25. Write a Kotlin program to test if a given number is a power of 2.
Input: 8
Expected Output: true
Q26. Write a Kotlin program to test if an array is empty.
Input: []
Expected Output: true
Q27. Write a Kotlin program to test if a list is null or empty.
Input: []
Expected Output: true
Q28. Write a Kotlin program to test the factorial of a number.
Input: 5
Expected Output: 120
Q29. Write a Kotlin program to test the first character of a string.
Input: "Kotlin"
Expected Output: K
Q30. Write a Kotlin program to test the last character of a string.
Input: "Kotlin"
Expected Output: n
Q31. Write a Kotlin program to test if a string contains whitespace characters.
Input: "Hello World"
Expected Output: true
Q32. Write a Kotlin program to test if a given number is even using assertEquals.
Input: 4
Expected Output: true
Q33. Write a Kotlin program to test a function that checks if a number is greater than or equal to 10.
Input: 15
Expected Output: true
Q34. Write a Kotlin program to test a function that adds two numbers together.
Input: 2, 3
Expected Output: 5
Q35. Write a Kotlin program to test if a string is a valid palindrome using assertTrue.
Input: "madam"
Expected Output: true
Q36. Write a Kotlin program to test if a map contains a specific value.
Input: {1: "One", 2: "Two"}, value: "One"
Expected Output: true
Q37. Write a Kotlin program to test if a list contains only integers.
Input: [1, 2, 3]
Expected Output: true
Q38. Write a Kotlin program to test if a function throws an exception.
Input: val func = { throw IllegalArgumentException() }
Expected Output: IllegalArgumentException
Q39. Write a Kotlin program to test if a number is divisible by both 2 and 3.
Input: 6
Expected Output: true
Q40. Write a Kotlin program to test the first occurrence of an element in a list.
Input: [1, 2, 3, 4], element: 3
Expected Output: 2
Q41. Write a Kotlin program to test if a string has any digits.
Input: "Kotlin123"
Expected Output: true
Q42. Write a Kotlin program to test if a string is not null or empty.
Input: "Kotlin"
Expected Output: true
Q43. Write a Kotlin program to test the reverse of a string.
Input: "Kotlin"
Expected Output: "niltok"
Q44. Write a Kotlin program to test the sorting of a list of strings.
Input: ["Banana", "Apple", "Orange"]
Expected Output: ["Apple", "Banana", "Orange"]
Q45. Write a Kotlin program to test a function that checks if a number is negative or positive.
Input: -5
Expected Output: "negative"
Q46. Write a Kotlin program to test the average of an array of integers.
Input: [1, 2, 3, 4]
Expected Output: 2.5
Q47. Write a Kotlin program to test the length of a string after modification.
Input: "Hello Kotlin"
Expected Output: 12
Q48. Write a Kotlin program to test if an item exists in a set.
Input: {1, 2, 3}, element: 2
Expected Output: true
Q49. Write a Kotlin program to test if a list of numbers is in descending order.
Input: [9, 7, 5, 3]
Expected Output: true
Q50. Write a Kotlin program to test if a number is within a specified range using assertTrue.
Input: 15, range: 10..20
Expected Output: true