Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Kotlin Exercises: Functions, Collections, and Classes

Exercise 1: Functions and Collections

Write a function `filterAndSum` that takes a list of integers and returns the sum of all even numbers in the list.

Example:

fun main() {

val numbers = listOf(1, 2, 3, 4, 5, 6)

println(filterAndSum(numbers)) // Output: 12

fun filterAndSum(numbers: List<Int>): Int {

// Your code here

Exercise 2: Using Collections

Given a list of strings, write a function `longestString` that returns the longest string. If there are multiple strings with the

same maximum length, return the first one.

Example:

fun main() {

val strings = listOf("apple", "banana", "cherry", "date")

println(longestString(strings)) // Output: "banana"

fun longestString(strings: List<String>): String {

// Your code here


}

Exercise 3: Classes and Methods

Create a class `Rectangle` with properties `width` and `height`. Add a method `area` that returns the area of the

rectangle and a method `perimeter` that returns the perimeter of the rectangle.

Example:

fun main() {

val rectangle = Rectangle(5.0, 10.0)

println(rectangle.area()) // Output: 50.0

println(rectangle.perimeter()) // Output: 30.0

class Rectangle(val width: Double, val height: Double) {

// Your code here

Exercise 4: Higher-Order Functions and Collections

Write a function `applyOperation` that takes a list of integers and a function that operates on an integer, and returns a

new list where the function has been applied to each element.

Example:

fun main() {

val numbers = listOf(1, 2, 3, 4)

val doubled = applyOperation(numbers) { it * 2 }

println(doubled) // Output: [2, 4, 6, 8]


}

fun applyOperation(numbers: List<Int>, operation: (Int) -> Int): List<Int> {

// Your code here

Exercise 5: Data Classes

Create a data class `Person` with properties `name` (String), `age` (Int), and `email` (String). Write a function

`createPeople` that returns a list of `Person` objects created from given input data.

Example:

fun main() {

val data = listOf(

Triple("Alice", 30, "alice@example.com"),

Triple("Bob", 25, "bob@example.com"),

Triple("Charlie", 35, "charlie@example.com")

val people = createPeople(data)

println(people)

data class Person(val name: String, val age: Int, val email: String)

fun createPeople(data: List<Triple<String, Int, String>>): List<Person> {

// Your code here

}
Exercise 6: Map and Filter

Write a function `findAdults` that takes a list of `Person` objects and returns a list of names of people who are 18 or

older.

Example:

fun main() {

val people = listOf(

Person("Alice", 17, "alice@example.com"),

Person("Bob", 25, "bob@example.com"),

Person("Charlie", 16, "charlie@example.com"),

Person("Dave", 20, "dave@example.com")

val adults = findAdults(people)

println(adults) // Output: ["Bob", "Dave"]

data class Person(val name: String, val age: Int, val email: String)

fun findAdults(people: List<Person>): List<String> {

// Your code here

You might also like