Examples and Summary

Practice Example 1: Variables and Constants

const val APP_NAME = "Student App"

fun main() {
    val studentName = "Mahesh"
    var studentAge = 30

    studentAge = 31

    println("App Name: $APP_NAME")
    println("Student Name: $studentName")
    println("Student Age: $studentAge")
}

Output

App Name: Student App
Student Name: Mahesh
Student Age: 31


Practice Example 2: String Templates

fun main() {
    val name = "Mahesh"
    val course = "Kotlin"
    val duration = 14

    println("Hello $name")
    println("You are learning $course")
    println("Course duration is $duration days")
}

Output

Hello Mahesh
You are learning Kotlin
Course duration is 14 days


Practice Example 3: Basic Calculation

fun main() {
    val num1 = 20
    val num2 = 10

    val addition = num1 + num2
    val subtraction = num1 - num2
    val multiplication = num1 * num2
    val division = num1 / num2

    println("Addition: $addition")
    println("Subtraction: $subtraction")
    println("Multiplication: $multiplication")
    println("Division: $division")
}

Output

Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2


Practice Example 4: Student Result

const val SCHOOL_NAME = "ABC Public School"

fun main() {
    val studentName = "Mahesh"
    val subject = "Math"
    var marks = 75

    marks += 10

    val isPassed = marks >= 40

    println("School: $SCHOOL_NAME")
    println("Student: $studentName")
    println("Subject: $subject")
    println("Final Marks: $marks")
    println("Passed: $isPassed")
}

Output

School: ABC Public School
Student: Mahesh
Subject: Math
Final Marks: 85
Passed: true


Practice Example 5: Shopping Bill

fun main() {
    val itemName = "Notebook"
    val price = 50
    val quantity = 4

    val totalAmount = price * quantity

    println("Item: $itemName")
    println("Price: ₹$price")
    println("Quantity: $quantity")
    println("Total Amount: ₹$totalAmount")
}

Output

Item: Notebook
Price: ₹50
Quantity: 4
Total Amount: ₹200


Full Combined Example

const val APP_NAME = "Kotlin Practice App"
const val VERSION = "1.0"

fun main() {
    val userName = "Mahesh"
    var score = 80

    score += 10

    val totalQuestions = 100
    val correctAnswers = score
    val isPassed = score >= 40
    val percentage = score.toDouble() / totalQuestions * 100

    println("App Name: $APP_NAME")
    println("Version: $VERSION")
    println("User Name: $userName")
    println("Correct Answers: $correctAnswers")
    println("Total Questions: $totalQuestions")
    println("Percentage: $percentage%")
    println("Passed: $isPassed")
}

Output

App Name: Kotlin Practice App
Version: 1.0
User Name: Mahesh
Correct Answers: 90
Total Questions: 100
Percentage: 90.0%
Passed: true


Quick Revision Table

TopicRemember Like This
KotlinModern language for Android
Kotlin featureLess code, safe code, Java support
Java vs KotlinKotlin is shorter and safer
valValue cannot be reassigned
varValue can be reassigned
const valFixed compile-time constant
Type inferenceKotlin detects type automatically
Data typesType of value stored
String templateUse $ to print variable
Arithmetic operatorUsed for calculations
Comparison operatorGives true or false
Logical operatorCombines conditions


Mini Diagram


Student Practice Tasks

Try these by yourself:

TaskExpected Practice
Create your name variable using valPrint your name
Create age using varUpdate age and print
Create const val APP_NAMEPrint app name
Create two numbersPrint addition, subtraction, multiplication
Create name and cityPrint using string template
Create marksCheck pass/fail using comparison operator


Interview-Friendly Summary

Kotlin is a modern, concise, and safe programming language widely used for Android development. It reduces boilerplate code compared to Java and provides features like null safety, type inference, Java interoperability, data classes, extension functions, and coroutines.

In Kotlin, val is used for read-only values, var is used for changeable values, and const val is used for compile-time constants. Kotlin supports basic data types such as Int, Long, Float, Double, Boolean, Char, and String. String templates allow easy printing of variables using $, and operators are used for calculations, comparisons, logical checks, and assignments.