Kotlin Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Kotlin Introduction
Exercise
Print a line.
("Hi")Kotlin Get Started
Exercise
File extension.
hello.Kotlin Syntax
Exercise
Declare a function.
main() {}Kotlin Variables
Exercise
Make a changeable variable.
count = 0Kotlin Data Types
Exercise
Convert to Double.
n.()
Kotlin Strings
Exercise
Insert a variable.
"Hi name"
Kotlin Null Safety
Exercise
Default if null.
x "none"
Kotlin Input and Output
Exercise
Safe conversion.
"12".()
Kotlin Comments
Exercise
Start a line comment.
noteKotlin Booleans
Exercise
Negate a value.
adultKotlin Constants
Exercise
Declare a compile time constant.
val MAX = 1Type Checks and Smart Casts
Exercise
Test a type.
if (x String)
Kotlin Break and Continue
Exercise
Skip to the next round.
if (x) val and var
Exercise
Read only variable.
pi = 3.14Basic Types
Exercise
Long literal.
val big = 5Null Safety
Exercise
Elvis operator.
x 0
String Templates
Exercise
Insert an expression.
"${a 1}"
Type Conversion
Exercise
Safe parse.
"5".()
Operators
Exercise
Range check.
3 1..5
Equality: == and ===
Exercise
Reference equality.
a b
Reading Input
Exercise
Read a line.
()Math Functions
Exercise
Square root.
(16.0)Random Numbers
Exercise
Range of ints.
r.nextInt(1, )
The main Function
Exercise
Entry function.
fun () {}
Comments and KDoc
Exercise
Line comment.
noteconst and lateinit
Exercise
Set later.
var name: StringChar and Boolean
Exercise
Letter test.
c.()
Any, is and as
Exercise
Safe cast.
x ? Int
Kotlin Operators
Exercise
Remainder operator.
17 5
Kotlin Ranges
Exercise
Count down.
for (i in 3 1)
Kotlin If as an Expression
Exercise
Replace the ternary operator.
val x = (a > b) a else b
Kotlin When
Exercise
Fallback branch.
when (x) { -> 0 }
Kotlin Loops
Exercise
Loop over a range.
for (i 1..3)
if as an Expression
Exercise
Return from if.
val x = (c) 1 else 2
when
Exercise
Default branch.
-> "other"for Loops
Exercise
Exclude the end.
0 3
while and do while
Exercise
Run once at least.
{ } while (c)break, continue and Labels
Exercise
Label syntax.
outer for
Ranges and Progressions
Exercise
Make a list.
(1..3).()
Kotlin Arrays
Exercise
Create an array.
(1, 2, 3)Kotlin Lists
Exercise
Mutable list.
Of(1, 2)Kotlin Sets and Maps
Exercise
Build a pair.
"a" 1
Kotlin Collection Functions
Exercise
Keep matching items.
n. { it > 2 }
Lists
Exercise
Changeable list.
Of(1)Collection Operations
Exercise
Keep matches.
n. { it > 2 }
Sets
Exercise
Unique values.
Of(1, 1)Maps
Exercise
Pair a key.
"a" 1
Sequences
Exercise
Lazy collection.
list.as()
Arrays
Exercise
Sort in place.
n.()
Destructuring
Exercise
Unpack a pair.
val (x, ) = pair
String Operations
Exercise
Split text.
s.(",")
groupBy, associate and partition
Exercise
Split in two.
list. { it > 1 }
fold and reduce
Exercise
Starting value.
list.(0) { a, b -> a + b }
zip, flatten and windowed
Exercise
Pair lists.
a.(b)
distinct, count and minOf
Exercise
Remove duplicates.
list.()
StringBuilder and buildString
Exercise
Build text.
String { append("x") }Pairs and Triples
Exercise
Third item.
t.Kotlin Functions
Exercise
Return type position.
fun f(): = 5
Default and Named Arguments
Exercise
Accept many values.
fun f( n: Int)
Lambdas and Higher Order Functions
Exercise
Lambda parameter arrow.
{ x: Int x * 2 }
Functions
Exercise
Short body.
fun f(x: Int) x * 2
Default and Named Arguments
Exercise
Name an argument.
box(w = 1, = 2)
vararg
Exercise
Spread an array.
sum(a)
Lambdas and Higher Order Functions
Exercise
Single parameter name.
list.map { * 2 }
Extension Functions
Exercise
Receiver type.
fun .shout() = this
Scope Functions
Exercise
Configure an object.
StringBuilder(). { append("x") }
Recursion and tailrec
Exercise
Optimise tail calls.
fun f()Kotlin Classes and Objects
Exercise
Declare a class.
Dog(val name: String)Kotlin Inheritance
Exercise
Allow inheritance.
class AnimalKotlin Data Classes
Exercise
Declare a data class.
class P(val x: Int)Kotlin Interfaces and Abstract Classes
Exercise
Declare an interface.
Shape { }Object and Companion Object
Exercise
Singleton keyword.
Logger { }Enums and Sealed Classes
Exercise
Restrict subclasses.
class ResultProperties, Getters and Setters
Exercise
Backing value keyword.
set(v) { = v }
Visibility Modifiers
Exercise
Hide from outside.
var secret = 1Operator Overloading
Exercise
Overload plus.
operator fun (o: V)
Destructuring and Typealias
Exercise
Short type name.
Ids = List<Int>Classes
Exercise
Create an object.
val d = ("Rex")
Data Classes
Exercise
Declare a data class.
class P(val x: Int)Properties, Getters and Setters
Exercise
Backing field.
set(v) { = v }
Inheritance
Exercise
Allow extending.
class AnimalInterfaces
Exercise
Implement.
class A : abstract and sealed Classes
Exercise
Closed hierarchy.
class Resultobject and companion object
Exercise
Singleton.
ConfigEnum Classes
Exercise
Enum keyword.
enum Level
Generics
Exercise
Type parameter.
class Box<>
Delegation and lazy
Exercise
Compute on first use.
val x by { 1 }
Operator Overloading
Exercise
Overload plus.
operator fun (o: V)
Kotlin Exceptions
Exercise
Always runs.
try {} catch (e: Exception) {} {}
Kotlin Files
Exercise
Read all text.
file.()
try, catch and finally
Exercise
Always runs.
try {} {}
throw and Custom Exceptions
Exercise
Validate an argument.
(x > 0)Files
Exercise
Read lines.
f.()
Coroutines Introduction
Exercise
Wait for a result.
a.()
Safe Calls and let
Exercise
Run if not null.
name?. { }
Smart Casts
Exercise
Type check.
x String
Practice: Word Frequency
Exercise
Count groups.
groupingBy { it }.()
Practice: FizzBuzz
Exercise
Branch without a subject.
when { }
Practice: Bank Account
Exercise
Private setter.
setExtension Functions
Premium exercise. See plans
Scope Functions
Premium exercise. See plans
Kotlin Generics
Premium exercise. See plans
Coroutines
Premium exercise. See plans
Kotlin Flow
Premium exercise. See plans
Delegation and Lazy
Premium exercise. See plans
Inline Functions and DSLs
Premium exercise. See plans
Kotlin for Android
Premium exercise. See plans
Ktor Web Server
Premium exercise. See plans
Testing Kotlin
Premium exercise. See plans
Gradle and Dependencies
Premium exercise. See plans
Java Interoperability
Premium exercise. See plans
Kotlin Best Practices
Premium exercise. See plans