
Practice 50 Kotlin Extensions Coding Questions
Q1. Write a Kotlin program to create an extension function for String that checks if the string is a palindrome.
Input: "racecar"
Expected Output: true
Q2. Write a Kotlin program to create an extension function for Int that returns the factorial of the number.
Input: 5
Expected Output: 120
Q3. Write a Kotlin program to create an extension function for String that counts the number of vowels in the string.
Input: "hello"
Expected Output: 2
Q4. Write a Kotlin program to create an extension function for List<Int> that returns the sum of all elements.
Input: [1, 2, 3, 4]
Expected Output: 10
Q5. Write a Kotlin program to create an extension function for String that returns the string reversed.
Input: "Kotlin"
Expected Output: "niltok"
Q6. Write a Kotlin program to create an extension function for String that converts the string to title case.
Input: "hello world"
Expected Output: "Hello World"
Q7. Write a Kotlin program to create an extension function for Int that checks if the number is prime.
Input: 7
Expected Output: true
Q8. Write a Kotlin program to create an extension function for List<String> that finds the longest string in the list.
Input: ["apple", "banana", "cherry"]
Expected Output: "banana"
Q9. Write a Kotlin program to create an extension function for String that returns the number of words in the string.
Input: "Kotlin is awesome"
Expected Output: 3
Q10. Write a Kotlin program to create an extension function for Int that returns the square of the number.
Input: 4
Expected Output: 16
Q11. Write a Kotlin program to create an extension function for String that checks if the string contains only digits.
Input: "1234"
Expected Output: true
Q12. Write a Kotlin program to create an extension function for List<Int> that finds the average of the list.
Input: [1, 2, 3, 4, 5]
Expected Output: 3.0
Q13. Write a Kotlin program to create an extension function for String that returns a substring from a given index.
Input: "KotlinProgramming", index: 6
Expected Output: "Programming"
Q14. Write a Kotlin program to create an extension function for Int that returns the sum of its digits.
Input: 123
Expected Output: 6
Q15. Write a Kotlin program to create an extension function for List<String> that sorts the list in reverse alphabetical order.
Input: ["apple", "banana", "cherry"]
Expected Output: ["cherry", "banana", "apple"]
Q16. Write a Kotlin program to create an extension function for String that checks if the string is an anagram of another string.
Input: "listen", "silent"
Expected Output: true
Q17. Write a Kotlin program to create an extension function for Int that checks if the number is even.
Input: 4
Expected Output: true
Q18. Write a Kotlin program to create an extension function for Double that rounds the number to two decimal places.
Input: 3.14159
Expected Output: 3.14
Q19. Write a Kotlin program to create an extension function for String that removes spaces from the string.
Input: "Kotlin Programming"
Expected Output: "KotlinProgramming"
Q20. Write a Kotlin program to create an extension function for List<Int> that returns the minimum value in the list.
Input: [3, 1, 4, 1, 5]
Expected Output: 1
Q21. Write a Kotlin program to create an extension function for String that returns the character at a specific index.
Input: "Kotlin", index: 2
Expected Output: "t"
Q22. Write a Kotlin program to create an extension function for List<Int> that returns the largest number in the list.
Input: [2, 5, 1, 8, 3]
Expected Output: 8
Q23. Write a Kotlin program to create an extension function for String that checks if the string contains vowels.
Input: "kotlin"
Expected Output: true
Q24. Write a Kotlin program to create an extension function for Int that converts the number to binary.
Input: 5
Expected Output: "101"
Q25. Write a Kotlin program to create an extension function for String that replaces all vowels with a given character.
Input: "hello", character: 'x'
Expected Output: "hxllx"
Q26. Write a Kotlin program to create an extension function for List<Int> that removes all even numbers from the list.
Input: [1, 2, 3, 4, 5]
Expected Output: [1, 3, 5]
Q27. Write a Kotlin program to create an extension function for Int that returns the cube of the number.
Input: 3
Expected Output: 27
Q28. Write a Kotlin program to create an extension function for String that counts the number of occurrences of a character.
Input: "kotlin", character: 't'
Expected Output: 1
Q29. Write a Kotlin program to create an extension function for List<String> that concatenates all strings into a single string.
Input: ["apple", "banana", "cherry"]
Expected Output: "applebananacherry"
Q30. Write a Kotlin program to create an extension function for Double that checks if the number is positive.
Input: -4.5
Expected Output: false
Q31. Write a Kotlin program to create an extension function for Int that checks if the number is a perfect square.
Input: 16
Expected Output: true
Q32. Write a Kotlin program to create an extension function for List<Int> that returns a list of squared elements.
Input: [1, 2, 3]
Expected Output: [1, 4, 9]
Q33. Write a Kotlin program to create an extension function for String that converts the string to uppercase without using the built-in toUpperCase() function.
Input: "kotlin"
Expected Output: "KOTLIN"
Q34. Write a Kotlin program to create an extension function for List<Int> that checks if the list contains only even numbers.
Input: [2, 4, 6, 8]
Expected Output: true
Q35. Write a Kotlin program to create an extension function for String that returns the string with no duplicates.
Input: "aabbcc"
Expected Output: "abc"
Q36. Write a Kotlin program to create an extension function for List<Int> that returns the product of all elements.
Input: [1, 2, 3, 4]
Expected Output: 24
Q37. Write a Kotlin program to create an extension function for String that checks if the string is a valid email address.
Input: "user@example.com"
Expected Output: true
Q38. Write a Kotlin program to create an extension function for List<Int> that checks if the list is sorted.
Input: [1, 2, 3, 4]
Expected Output: true
Q39. Write a Kotlin program to create an extension function for String that repeats the string a given number of times.
Input: "hello", times: 3
Expected Output: "hellohellohello"
Q40. Write a Kotlin program to create an extension function for String that checks if the string is a valid phone number (only digits).
Input: "1234567890"
Expected Output: true
Q41. Write a Kotlin program to create an extension function for Int that checks if the number is a multiple of another number.
Input: 10, multiple: 5
Expected Output: true
Q42. Write a Kotlin program to create an extension function for List<Int> that filters out values less than a given threshold.
Input: [1, 2, 3, 4, 5], threshold: 3
Expected Output: [3, 4, 5]
Q43. Write a Kotlin program to create an extension function for String that checks if the string starts with a given prefix.
Input: "kotlin", prefix: "kot"
Expected Output: true
Q44. Write a Kotlin program to create an extension function for List<String> that returns the first and last element as a pair.
Input: ["apple", "banana", "cherry"]
Expected Output: ("apple", "cherry")
Q45. Write a Kotlin program to create an extension function for List<Int> that returns the sum of all even numbers in the list.
Input: [1, 2, 3, 4, 5]
Expected Output: 6
Q46. Write a Kotlin program to create an extension function for String that checks if the string contains any digits.
Input: "abc123"
Expected Output: true
Q47. Write a Kotlin program to create an extension function for Double that converts it to a percentage with two decimal places.
Input: 0.123
Expected Output: "12.30%"
Q48. Write a Kotlin program to create an extension function for List<String> that converts the list into a set of unique values.
Input: ["apple", "banana", "apple", "cherry"]
Expected Output: {"apple", "banana", "cherry"}
Q49. Write a Kotlin program to create an extension function for String that checks if the string contains only lowercase letters.
Input: "kotlin"
Expected Output: true
Q50. Write a Kotlin program to create an extension function for List<Int> that returns the difference between the largest and smallest numbers in the list.
Input: [1, 2, 3, 4, 5]
Expected Output: 4