Kotlin

Kotlin is a modern, statically-typed programming language developed by JetBrains. It is fully interoperable with Java and widely used for Android app development, server-side applications, and scripting. In this chapter, we will explore Kotlin syntax, variables, data types, functions, control flow, classes, objects, and a final example.

Introduction to Kotlin

Kotlin files use the .kt extension. Programs start with a main function. Kotlin is concise, expressive, and supports functional programming alongside object-oriented programming.

Variables and Data Types

Kotlin supports immutable variables (val) and mutable variables (var). Common data types include Int, Double, Float, Boolean, Char, String.

val name: String = "Riya"<br>
var age: Int = 14<br>
val height: Double = 5.6<br>
var isStudent: Boolean = true<br>
val grade: Char = 'A'<br>
>

Functions

Functions are defined using fun. Kotlin supports default parameters and expression body functions.

fun greet(name: String) {<br>
  println("Hello, $name!")<br>
}<br>
fun sum(a: Int, b: Int): Int = a + b<br>
>

Control Flow

Kotlin supports if, else, when statements for conditional execution.

Loops

Repetition is supported with for, while, and do-while loops.

Classes and Objects

Classes are defined using class. Objects are instances created using the constructor or init block.

Final Example — Collatz Sequence

This example demonstrates the Collatz sequence using Kotlin. Save as Collatz.kt.

fun main() {<br>
  print("Enter a number: ")<br>
  var n = readLine()!!.toInt()<br>
  while (n != 1) {<br>
    print("$n ")<br>
    if (n % 2 == 0) n /= 2<br>
    else n = 3*n + 1<br>
  }<br>
  println("1")<br>
}<br>
>

MCQs

1. Which keyword is used for defining a variable that cannot be reassigned?

(a) var

(b) val

(c) const

(d) let

► (b) val

2. Which keyword defines a function in Kotlin?

(a) function

(b) def

(c) fun

(d) lambda

► (c) fun

3. What is the correct way to declare a string in Kotlin?

(a) var name = 'Riya'

(b) var name = "Riya"

(c) var name = Riya

(d) var name = `Riya`

► (b) var name = "Riya"

4. How do you read input from the user?

(a) input()

(b) read()

(c) readLine()

(d) scanner()

► (c) readLine()

5. Which data type represents decimal numbers?

(a) Int

(b) Double

(c) Boolean

(d) Char

► (b) Double

6. Which operator is used for equality check?

(a) =

(b) ==

(c) ===

(d) !=

► (b) ==

7. Which statement is used for multiple conditional checks in Kotlin?

(a) if

(b) when

(c) switch

(d) select

► (b) when

8. How do you define a class in Kotlin?

(a) object

(b) struct

(c) class

(d) type

► (c) class

9. Which symbol is used for string templates?

(a) $

(b) @

(c) #

(d) &

► (a) $

10. Which type is used for true/false values?

(a) Boolean

(b) Int

(c) String

(d) Char

► (a) Boolean

11. How do you create a loop that executes at least once?

(a) for

(b) while

(c) do-while

(d) loop

► (c) do-while

12. Which of the following is immutable?

(a) var

(b) val

(c) varlate

(d) lateinit

► (b) val

13. What is the default return type of a function that does not return anything?

(a) Int

(b) Unit

(c) Void

(d) None

► (b) Unit

14. How do you convert a string to an integer?

(a) str.toInt()

(b) str.parseInt()

(c) int(str)

(d) str.int()

► (a) str.toInt()

15. Which keyword allows creating a singleton object?

(a) class

(b) object

(c) singleton

(d) instance

► (b) object

16. Which loop is best for iterating over a range?

(a) while

(b) do-while

(c) for

(d) loop

► (c) for

17. What does `!!` do in Kotlin?

(a) Converts to nullable

(b) Throws an exception if null

(c) Declares a variable

(d) Compares equality

► (b) Throws an exception if null

18. Which file extension is used for Kotlin files?

(a) .java

(b) .kt

(c) .kotlin

(d) .k

► (b) .kt