Data types tell the app what kind of information it is working with. Type inference helps the app guess the data type automatically, so you write less code.
When you want to store a number like age or price.
When you need to keep text like a user's name or message.
When you want the app to figure out the type for you to save time.
When you want to avoid mistakes by clearly defining what kind of data you use.
When you want to work with true/false values to control app behavior.
Syntax
Android Kotlin
val variableName: DataType = value
val variableName = value // type inferred
val means the value cannot change (like a constant).
If you don't write the type, Kotlin guesses it from the value (type inference).
Examples
This creates a number variable called age with the value 25.
Android Kotlin
val age: Int = 25
Kotlin guesses name is a String because of the quotes.
Android Kotlin
val name = "Alice"
This stores a true/false value to check if something is active.
Android Kotlin
val isActive: Boolean = true
Kotlin infers price as a Double because of the decimal number.
Android Kotlin
val price = 19.99
Sample App
This program creates variables with different data types using both explicit types and type inference. It then prints each value.
Android Kotlin
fun main() {
val userName = "Bob"
val userAge: Int = 30
val isMember = true
val balance = 150.75
println("Name: $userName")
println("Age: $userAge")
println("Member: $isMember")
println("Balance: $balance")
}
OutputSuccess
Important Notes
Use val for values that don't change and var for values that can change.
Kotlin's type inference helps keep your code clean and easy to read.
Always choose the right data type to avoid errors and make your app faster.
Summary
Data types define what kind of data a variable holds.
Type inference lets Kotlin guess the data type from the value.
Use val for fixed values and var for variables that change.
Practice
(1/5)
1. Which keyword in Kotlin declares a variable whose value cannot be changed after assignment?
easy
A. val
B. var
C. let
D. const
Solution
Step 1: Understand Kotlin variable keywords
Kotlin uses val for immutable variables and var for mutable ones.
Step 2: Identify the immutable keyword
val means the value cannot be changed after assignment.
Final Answer:
val -> Option A
Quick Check:
Immutable variable keyword = val [OK]
Hint: Remember: val = fixed, var = changeable [OK]
Common Mistakes:
Confusing val and var
Using let which is not a Kotlin keyword
Thinking const is used like val
2. Which of the following is the correct way to declare an integer variable with value 10 using type inference in Kotlin?
easy
A. val number: Int = 10
B. val number = 10.0
C. var number = "10"
D. val number = 10
Solution
Step 1: Understand type inference in Kotlin
Kotlin can guess the type from the assigned value, so val number = 10 infers Int.
Step 2: Check each option
val number: Int = 10 declares type explicitly, which is correct but not type inference. val number = 10 uses type inference correctly. var number = "10" assigns a String, not Int. val number = 10.0 assigns a Double, not Int.
Final Answer:
val number = 10 -> Option D
Quick Check:
Type inference assigns Int from 10 [OK]
Hint: Omit type to let Kotlin infer it [OK]
Common Mistakes:
Adding explicit type when not needed
Assigning wrong data type
Confusing Int and Double
3. What will be the output of this Kotlin code?
val x = 5
val y = 2
val result = x / y
println(result)
medium
A. 2.0
B. 2.5
C. 2
D. Compilation error
Solution
Step 1: Identify variable types and operation
Both x and y are Int by type inference. Integer division truncates decimals.
Step 2: Calculate integer division
5 / 2 = 2 (decimal part discarded).
Final Answer:
2 -> Option C
Quick Check:
Integer division truncates decimals = 2 [OK]
Hint: Integer division drops decimals in Kotlin [OK]
Common Mistakes:
Expecting floating point result
Using variables as Double without declaration
Thinking println shows decimal automatically
4. Identify the error in this Kotlin code snippet:
val name = "Alice"
name = "Bob"
medium
A. Missing semicolon after first line
B. Cannot reassign a val variable
C. String values must use single quotes
D. Variable name is a reserved keyword
Solution
Step 1: Check variable declaration
val name = "Alice" declares an immutable variable.
Step 2: Check reassignment
Trying to assign "Bob" to name causes error because val cannot be reassigned.
Final Answer:
Cannot reassign a val variable -> Option B
Quick Check:
val variables are immutable [OK]
Hint: val means no reassignment allowed [OK]
Common Mistakes:
Thinking val allows reassignment
Confusing quotes for strings
Assuming semicolons are mandatory
5. Given this Kotlin code, what is the inferred type of result?
val a = 3
val b = 4.5
val result = a + b
hard
A. Double
B. Int
C. Float
D. String
Solution
Step 1: Identify types of operands
a is Int, b is Double (4.5 is a Double literal).
Step 2: Understand type promotion in arithmetic
When adding Int and Double, Int is promoted to Double, so result is Double.
Final Answer:
Double -> Option A
Quick Check:
Int + Double results in Double [OK]
Hint: Mixed numeric types promote to the wider type [OK]
Common Mistakes:
Assuming result is Int
Confusing Float and Double
Thinking result is String
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.