0% found this document useful (0 votes)
11 views

Kotlin Exercises

Uploaded by

guyjoseph4902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Kotlin Exercises

Uploaded by

guyjoseph4902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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, "[email protected]"),

Triple("Bob", 25, "[email protected]"),

Triple("Charlie", 35, "[email protected]")

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, "[email protected]"),

Person("Bob", 25, "[email protected]"),

Person("Charlie", 16, "[email protected]"),

Person("Dave", 20, "[email protected]")

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