Practice 50 Kotlin Collections and Generics Coding Questions, TechnoVlogs

Practice 50 Kotlin Collections and Generics Coding Questions


Q1. Write a Kotlin program to create a list of integers and print all the elements.
  Input: [1, 2, 3, 4, 5]
  Expected Output: 1 2 3 4 5

Q2. Write a Kotlin program to create a mutable list of strings and add an element to it.
  Input: ["apple", "banana", "cherry"] (add "grape")
  Expected Output: apple banana cherry grape

Q3. Write a Kotlin program to find the maximum number in a list of integers.
  Input: [5, 12, 8, 1, 20]
  Expected Output: 20

Q4. Write a Kotlin program to create a set of integers and print its size.
  Input: {1, 2, 3, 4, 5}
  Expected Output: 5

Q5. Write a Kotlin program to create a map of key-value pairs and print all keys.
  Input: {1="one", 2="two", 3="three"}
  Expected Output: 1 2 3

Q6. Write a Kotlin program to create a list of numbers and filter out even numbers.
  Input: [1, 2, 3, 4, 5, 6]
  Expected Output: [1, 3, 5]

Q7. Write a Kotlin program to check if a list of integers contains a specific number.
  Input: [1, 2, 3, 4, 5] (check for 3)
  Expected Output: true

Q8. Write a Kotlin program to create a map of strings and their lengths, and print the length of each string.
  Input: {"apple"=5, "banana"=6, "cherry"=6}
  Expected Output: apple: 5 banana: 6 cherry: 6

Q9. Write a Kotlin program to add a generic function that finds the maximum element in a list.
  Input: [10, 20, 30, 40, 50]
  Expected Output: 50

Q10. Write a Kotlin program to create a mutable list of strings and remove a specific element from it.
   Input: ["apple", "banana", "cherry"] (remove "banana")
   Expected Output: apple cherry

Q11. Write a Kotlin program to create a list of integers and calculate their sum.
   Input: [1, 2, 3, 4]
   Expected Output: 10

Q12. Write a Kotlin program to create a set and check if it contains a specific element.
   Input: {1, 2, 3, 4} (check for 3)
   Expected Output: true

Q13. Write a Kotlin program to convert a list of integers into a set to remove duplicates.
   Input: [1, 2, 2, 3, 3, 4]
   Expected Output: {1, 2, 3, 4}

Q14. Write a Kotlin program to create a generic list and print its elements.
   Input: listOf("apple", "banana", "cherry")
   Expected Output: apple banana cherry

Q15. Write a Kotlin program to create a list of integers and find the average.
   Input: [10, 20, 30, 40]
   Expected Output: 25.0

Q16. Write a Kotlin program to find the index of a specific element in a list.
   Input: ["apple", "banana", "cherry"] (find index of "banana")
   Expected Output: 1

Q17. Write a Kotlin program to create a map of student names and their scores, then print all scores.
   Input: {"John"=85, "Alice"=92, "Bob"=78}
   Expected Output: 85 92 78

Q18. Write a Kotlin program to filter a map of student names and scores to only include students with scores above 80.
   Input: {"John"=85, "Alice"=92, "Bob"=78}
   Expected Output: John: 85 Alice: 92

Q19. Write a Kotlin program to create a set of strings and check if it is empty.
   Input: {"apple", "banana", "cherry"}
   Expected Output: false

Q20. Write a Kotlin program to create a list of strings and convert it into a single string separated by commas.
   Input: ["apple", "banana", "cherry"]
   Expected Output: "apple, banana, cherry"

Q21. Write a Kotlin program to create a generic function to find the minimum value in a list of numbers.
   Input: [10, 20, 5, 30]
   Expected Output: 5

Q22. Write a Kotlin program to create a list of integers and remove all elements that are greater than 5.
   Input: [1, 2, 6, 4, 7, 3]
   Expected Output: [1, 2, 4, 3]

Q23. Write a Kotlin program to create a mutable list of integers and sort them in descending order.
   Input: [1, 5, 3, 9, 2]
   Expected Output: [9, 5, 3, 2, 1]

Q24. Write a Kotlin program to create a map with integer keys and string values, and print the value for a specific key.
   Input: {1="one", 2="two", 3="three"} (print value for key 2)
   Expected Output: "two"

Q25. Write a Kotlin program to merge two lists of integers and print the combined list.
   Input: [1, 2, 3] and [4, 5, 6]
   Expected Output: [1, 2, 3, 4, 5, 6]

Q26. Write a Kotlin program to create a set of integers and remove all elements greater than 5.
   Input: {1, 2, 6, 4, 7, 3}
   Expected Output: {1, 2, 4, 3}

Q27. Write a Kotlin program to find the frequency of elements in a list of integers.
   Input: [1, 2, 2, 3, 3, 3, 4]
   Expected Output: 1: 1 2: 2 3: 3 4: 1

Q28. Write a Kotlin program to create a list of strings and find the longest string.
   Input: ["apple", "banana", "cherry"]
   Expected Output: banana

Q29. Write a Kotlin program to create a map of strings and their lengths, and find the string with the longest length.
   Input: {"apple"=5, "banana"=6, "cherry"=6}
   Expected Output: banana

Q30. Write a Kotlin program to create a generic function to find the sum of all elements in a list of numbers.
   Input: [10, 20, 30, 40]
   Expected Output: 100

Q31. Write a Kotlin program to create a list of numbers and find the even numbers.
   Input: [1, 2, 3, 4, 5, 6]
   Expected Output: [2, 4, 6]

Q32. Write a Kotlin program to create a set of integers and check if a number exists in the set.
   Input: {1, 2, 3, 4, 5} (check for 3)
   Expected Output: true

Q33. Write a Kotlin program to reverse a list of integers.
   Input: [1, 2, 3, 4, 5]
   Expected Output: [5, 4, 3, 2, 1]

Q34. Write a Kotlin program to create a list of numbers and filter out all numbers less than 10.
   Input: [5, 15, 7, 12, 3]
   Expected Output: [15, 12]

Q35. Write a Kotlin program to create a generic function that returns a set of unique elements from a list.
   Input: [1, 2, 2, 3, 3, 4]
   Expected Output: {1, 2, 3, 4}

Q36. Write a Kotlin program to create a list of integers and sort it in ascending order.
   Input: [3, 5, 1, 4, 2]
   Expected Output: [1, 2, 3, 4, 5]

Q37. Write a Kotlin program to create a mutable map and update a specific value.
   Input: {"John"=85, "Alice"=92} (update "John" to 90)
   Expected Output: John: 90 Alice: 92

Q38. Write a Kotlin program to create a list of strings and count the number of occurrences of a specific string.
   Input: ["apple", "banana", "apple", "cherry"] (count "apple")
   Expected Output: 2

Q39. Write a Kotlin program to create a list and get the first and last elements.
   Input: [1, 2, 3, 4, 5]
   Expected Output: First: 1, Last: 5

Q40. Write a Kotlin program to create a list of numbers and remove all occurrences of a specific element.
   Input: [1, 2, 3, 4, 3, 5] (remove 3)
   Expected Output: [1, 2, 4, 5]

Q41. Write a Kotlin program to create a list of strings and check if it contains a specific string.
   Input: ["apple", "banana", "cherry"] (check for "banana")
   Expected Output: true

Q42. Write a Kotlin program to merge two sets and print the combined set.
   Input: {1, 2, 3} and {3, 4, 5}
   Expected Output: {1, 2, 3, 4, 5}

Q43. Write a Kotlin program to create a map of strings and their lengths, and filter out strings with length less than 5.
   Input: {"apple"=5, "banana"=6, "cherry"=6}
   Expected Output: banana: 6 cherry: 6

Q44. Write a Kotlin program to create a set of integers and find the sum of all elements.
   Input: {1, 2, 3, 4, 5}
   Expected Output: 15

Q45. Write a Kotlin program to convert a map into a list of key-value pairs.
   Input: {1="one", 2="two", 3="three"}
   Expected Output: [1=one, 2=two, 3=three]

Q46. Write a Kotlin program to create a list of integers and find the index of the maximum value.
   Input: [3, 1, 4, 1, 5, 9, 2, 6]
   Expected Output: 5

Q47. Write a Kotlin program to create a list of integers and find the even numbers using a generic function.
   Input: [10, 15, 8, 20, 7]
   Expected Output: [10, 8, 20]

Q48. Write a Kotlin program to remove duplicates from a list using a set.
   Input: [1, 2, 2, 3, 3, 4]
   Expected Output: [1, 2, 3, 4]

Q49. Write a Kotlin program to create a list of strings and sort them alphabetically.
   Input: ["orange", "banana", "apple"]
   Expected Output: [apple, banana, orange]

Q50. Write a Kotlin program to create a set of strings and remove a specific string.
   Input: {"apple", "banana", "cherry"} (remove "banana")
   Expected Output: {"apple", "cherry"}

Share on Social Media