Kotlin

Kotlin is a modern, concise, and safe programming language that runs on the Java Virtual Machine and is fully interoperable with Java. It is widely used for Android development, desktop software, and server applications. Kotlin eliminates boilerplate code, supports type inference, and emphasizes null safety. Programs are saved as .kt files. In this chapter, we will explore Kotlin basics: printing, variables, conditions, loops, functions, and finally a subtraction calculator.

Hello world

The simplest Kotlin program uses the fun main() function as the entry point.

fun main() {
  println("Hello, world!")
}

Variables and types

Kotlin has val for read-only values and var for mutable variables. Types include Int, Double, Boolean, Char, and String.

val name: String = "Alice"
var age: Int = 20
val pi = 3.14 // type inferred

Conditions

Kotlin uses if as an expression, returning a value. It also has when for multiple choices.

val score = 72
val result = if (score >= 50) "Pass" else "Fail"
println(result)

Loops

Kotlin provides for, while, and do...while loops. Collections can be looped directly.

for (i in 1..3) {
  println("Step $i")
}

Functions

Functions in Kotlin are defined with fun. They can return values and support default parameters.

fun multiply(a: Int, b: Int): Int {
  return a * b
}

Final example — calculator for subtraction

This Kotlin program reads two numbers from input, calculates the difference, and prints it. Save as Main.kt and run with kotlinc Main.kt -include-runtime -d Main.jar then java -jar Main.jar.

Main.kt

import java.util.Scanner

fun main() {
  val sc = Scanner(System.`in`)
  print("Enter minuend: ")
  val minuend = sc.nextDouble()
  print("Enter subtrahend: ")
  val subtrahend = sc.nextDouble()
  val difference = minuend - subtrahend
  println("Difference: $difference")
}

MCQs

1. What is the correct file extension for Kotlin source files?

(a) .java

(b) .kt

(c) .kot

(d) .ktsrc

► (b) .kt

2. Which function is the entry point of a Kotlin program?

(a) start()

(b) run()

(c) main()

(d) init()

► (c) main()

3. Which keyword declares a read-only variable in Kotlin?

(a) let

(b) val

(c) var

(d) const

► (b) val

4. Which keyword declares a mutable variable in Kotlin?

(a) var

(b) let

(c) const

(d) value

► (a) var

5. Which symbol is used to start a single-line comment in Kotlin?

(a) #

(b) //

(c) --

(d) /* */

► (b) //

6. What will val result = if (3 > 2) "Yes" else "No" store in result?

(a) Yes

(b) No

(c) true

(d) false

► (a) Yes

7. Which control structure in Kotlin is similar to switch in Java?

(a) case

(b) choose

(c) when

(d) select

► (c) when

8. What is the output of for (i in 1..3) print(i)?

(a) 1 2 3

(b) 123

(c) 0 1 2

(d) 0 1 2 3

► (b) 123

9. How are functions declared in Kotlin?

(a) function name()

(b) def name()

(c) fun name()

(d) void name()

► (c) fun name()

10. Which type represents true/false values in Kotlin?

(a) Boolean

(b) Int

(c) Char

(d) String

► (a) Boolean

11. What does the expression val pi = 3.14 demonstrate?

(a) Explicit typing

(b) Type inference

(c) String assignment

(d) Boolean value

► (b) Type inference

12. Which statement is used to exit a loop immediately in Kotlin?

(a) stop

(b) quit

(c) break

(d) end

► (c) break

13. Which keyword skips the current loop iteration in Kotlin?

(a) skip

(b) continue

(c) next

(d) pass

► (b) continue

14. What is the default value of an uninitialized Boolean property in a Kotlin class?

(a) true

(b) false

(c) null

(d) 0

► (c) null

15. Which keyword is used to inherit a class in Kotlin?

(a) inherits

(b) extends

(c) :

(d) implements

► (c) :

16. What is the output of println("Kotlin".length)?

(a) 5

(b) 6

(c) Kotlin

(d) Error

► (b) 6

17. Which operator is used for string templates in Kotlin?

(a) $

(b) #

(c) %

(d) &

► (a) $

18. In the subtraction calculator, which variable stores the result?

(a) total

(b) output

(c) difference

(d) result

► (c) difference