
Practice 50 Kotlin Function Coding Questions
Q1. Write a Kotlin program to create a function that returns the sum of two numbers.
Input: a = 3, b = 4
Expected Output: 7
Q2. Write a Kotlin program to create a function that checks if a number is even or odd.
Input: num = 5
Expected Output: Odd
Q3. Write a Kotlin program to create a function that returns the factorial of a number.
Input: num = 4
Expected Output: 24
Q4. Write a Kotlin program to create a function that finds the largest of three numbers.
Input: a = 3, b = 5, c = 2
Expected Output: 5
Q5. Write a Kotlin program to create a function that calculates the area of a rectangle.
Input: length = 5, width = 3
Expected Output: 15
Q6. Write a Kotlin program to create a function that checks if a number is prime.
Input: num = 11
Expected Output: Prime
Q7. Write a Kotlin program to create a function that reverses a string.
Input: str = "Kotlin"
Expected Output: "niltoK"
Q8. Write a Kotlin program to create a function that checks if a number is a palindrome.
Input: num = 121
Expected Output: Palindrome
Q9. Write a Kotlin program to create a function that finds the Fibonacci sequence up to the given number.
Input: num = 5
Expected Output: 0 1 1 2 3
Q10. Write a Kotlin program to create a function that calculates the power of a number.
Input: base = 3, exponent = 2
Expected Output: 9
Q11. Write a Kotlin program to create a function that finds the greatest common divisor (GCD) of two numbers.
Input: a = 56, b = 98
Expected Output: 14
Q12. Write a Kotlin program to create a function that returns the length of a string.
Input: str = "Kotlin"
Expected Output: 6
Q13. Write a Kotlin program to create a function that calculates the sum of elements in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 10
Q14. Write a Kotlin program to create a function that checks if a string contains a substring.
Input: str = "Kotlin", sub = "tin"
Expected Output: True
Q15. Write a Kotlin program to create a function that returns the reverse of an array.
Input: arr = [1, 2, 3]
Expected Output: [3, 2, 1]
Q16. Write a Kotlin program to create a function that checks if a string is a palindrome.
Input: str = "madam"
Expected Output: Palindrome
Q17. Write a Kotlin program to create a function that returns the average of a list of numbers.
Input: nums = [1, 2, 3, 4]
Expected Output: 2.5
Q18. Write a Kotlin program to create a function that calculates the sum of digits of a number.
Input: num = 123
Expected Output: 6
Q19. Write a Kotlin program to create a function that checks if a number is a perfect number.
Input: num = 28
Expected Output: Perfect number
Q20. Write a Kotlin program to create a function that checks if a year is a leap year.
Input: year = 2020
Expected Output: Leap year
Q21. Write a Kotlin program to create a function that finds the minimum number in an array.
Input: arr = [3, 1, 4, 1]
Expected Output: 1
Q22. Write a Kotlin program to create a function that swaps two numbers.
Input: a = 5, b = 10
Expected Output: a = 10, b = 5
Q23. Write a Kotlin program to create a function that finds the square of a number.
Input: num = 4
Expected Output: 16
Q24. Write a Kotlin program to create a function that prints a multiplication table of a number.
Input: num = 5
Expected Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
Q25. Write a Kotlin program to create a function that checks if a string starts with a specific character.
Input: str = "Kotlin", char = 'K'
Expected Output: True
Q26. Write a Kotlin program to create a function that finds the square root of a number.
Input: num = 16
Expected Output: 4
Q27. Write a Kotlin program to create a function that calculates the area of a circle.
Input: radius = 5
Expected Output: 78.54
Q28. Write a Kotlin program to create a function that removes duplicates from a list.
Input: list = [1, 2, 2, 3]
Expected Output: [1, 2, 3]
Q29. Write a Kotlin program to create a function that concatenates two strings.
Input: str1 = "Hello", str2 = "World"
Expected Output: "HelloWorld"
Q30. Write a Kotlin program to create a function that returns the maximum number in an array.
Input: arr = [10, 20, 30]
Expected Output: 30
Q31. Write a Kotlin program to create a function that counts the number of vowels in a string.
Input: str = "Kotlin"
Expected Output: 2
Q32. Write a Kotlin program to create a function that multiplies two numbers.
Input: a = 5, b = 4
Expected Output: 20
Q33. Write a Kotlin program to create a function that checks if a number is divisible by 3 or 5.
Input: num = 15
Expected Output: Divisible by 3 or 5
Q34. Write a Kotlin program to create a function that finds the sum of the even numbers in an array.
Input: arr = [1, 2, 3, 4]
Expected Output: 6
Q35. Write a Kotlin program to create a function that checks if a number is Armstrong.
Input: num = 153
Expected Output: Armstrong number
Q36. Write a Kotlin program to create a function that prints a triangle pattern.
Input: n = 4
Expected Output:
*
**
***
****
Q37. Write a Kotlin program to create a function that returns a substring of a string.
Input: str = "Kotlin", start = 1, end = 4
Expected Output: "otl"
Q38. Write a Kotlin program to create a function that checks if a number is a perfect square.
Input: num = 16
Expected Output: Perfect square
Q39. Write a Kotlin program to create a function that checks if a string ends with a specific character.
Input: str = "Kotlin", char = 'n'
Expected Output: True
Q40. Write a Kotlin program to create a function that calculates the sum of two arrays.
Input: arr1 = [1, 2], arr2 = [3, 4]
Expected Output: [4, 6]
Q41. Write a Kotlin program to create a function that checks if a number is negative.
Input: num = -5
Expected Output: Negative
Q42. Write a Kotlin program to create a function that returns the remainder when dividing two numbers.
Input: a = 5, b = 2
Expected Output: 1
Q43. Write a Kotlin program to create a function that multiplies all elements in an array.
Input: arr = [1, 2, 3]
Expected Output: 6
Q44. Write a Kotlin program to create a function that finds the difference between the largest and smallest number in an array.
Input: arr = [10, 20, 30]
Expected Output: 20
Q45. Write a Kotlin program to create a function that converts a string to uppercase.
Input: str = "kotlin"
Expected Output: "KOTLIN"
Q46. Write a Kotlin program to create a function that prints a square pattern.
Input: n = 3
Expected Output:
***
***
***
Q47. Write a Kotlin program to create a function that calculates the sum of numbers in a range.
Input: start = 1, end = 5
Expected Output: 15
Q48. Write a Kotlin program to create a function that checks if a string contains a digit.
Input: str = "Kotlin3"
Expected Output: True
Q49. Write a Kotlin program to create a function that returns the square of a number.
Input: num = 4
Expected Output: 16
Q50. Write a Kotlin program to create a function that prints a diamond pattern.
Input: n = 3
Expected Output:
*
***
*****
***
*