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: 31Practice 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 daysPractice 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: 2Practice 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: truePractice 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: ₹200Full 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: trueQuick Revision Table
| Topic | Remember Like This |
|---|---|
| Kotlin | Modern language for Android |
| Kotlin feature | Less code, safe code, Java support |
| Java vs Kotlin | Kotlin is shorter and safer |
val | Value cannot be reassigned |
var | Value can be reassigned |
const val | Fixed compile-time constant |
| Type inference | Kotlin detects type automatically |
| Data types | Type of value stored |
| String template | Use $ to print variable |
| Arithmetic operator | Used for calculations |
| Comparison operator | Gives true or false |
| Logical operator | Combines conditions |
Mini Diagram
Student Practice Tasks
Try these by yourself:
| Task | Expected Practice |
|---|---|
Create your name variable using val | Print your name |
Create age using var | Update age and print |
Create const val APP_NAME | Print app name |
| Create two numbers | Print addition, subtraction, multiplication |
| Create name and city | Print using string template |
| Create marks | Check 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.