notes on Android Interview Questions Scaler

How are variables declared in Kotlin? What are the different types
val marks = 95 // immutable
var totalMarks = 565 // mutable
  • there are two types Val and var
  • val is immutable meaning the value cannot be changed later
  • var is mutable meaning the value can be changed later on.

Difference between variable declaration with val and const
  • both variables declared with val and const are immutable
  • value declared with const must be known at the compile time

example

val myVal = System.currentTimeMillis() // Allowed (runtime evaluated)
println(myVal) // Prints different values each time the program runs
 
const val PI = 3.14159 // Allowed (compile-time constant)
  • You can assign any type to val, but const can hold only primitive types.
  • const must be top-level or inside companion object

Why should we use Kotlin?
  • Concise
  • Null Safe
  • Interoperable

How to ensure null safety feature in Kotlin

By using null check syntax

var name: String? = "Vignesh"  
name = null  
println(name?.length ?: "Name is null")
 
// ? --> Nullability Operator
// ?. --> Safety call operator
// ?: --> Elvis Operator
// !! --> Non-null assertion Operator (name!!.length)

What is data class in Kotlin
  • Used for holding data with built-in toString(), equals(), hashCode() and copy()
  • Requires atleast one property in the primary constructor
data class User(val name: String, val age: Int)

Example usage:

val user1 = User("Alice", 25)
val user2 = user1.copy(age = 26) // Copy with modification
println(user1) // User(name=Alice, age=25)

What are the types of classes in Kotlin

  1. Regular class
  2. Data Class
  3. Sealed Class
  4. Abstract Class
  5. Interface Class
  6. Singleton (Object Declaration)
  7. Enum Class
  8. Inline Class
  9. Companion Object
Class TypePurpose
Regular ClassGeneral object-oriented programming
Data ClassStores data with built-in utility methods
Sealed ClassRestricts subclass hierarchy (useful for when)
Abstract ClassBase class with abstract methods
Interface ClassDefines behavior for multiple classes
Singleton (Object Declaration)Singleton instance
Companion ObjectStatic-like members in a class
Enum ClassRepresents a fixed set of constants
Inline ClassWraps a single value with low memory overhead

What is lateinit in Kotlin

lateinit is a modifier used for declaring non-nullable properties that are initialized later, rather than at the time of declaration

When to Use lateinit
  • When you cannot initialize a property at the time of declaration.
  • When you want to avoid using nullable types (String?, Int?).

How to check if lateinit variable has been initialized or not
  • use isInitialised() method

Explain various methods to iterate over any data structure in Kotlin

for loop

// 1. Iterating over elements
val names = listOf("Alice", "Bob", "Charlie")
for (name in names) {
    println(name)
}
// 2. Iterating over indices
val nums = listOf(10, 20, 30)
for (index in nums.indices) {
    println("Element at $index is ${nums[index]}")
}

for each

val fruits = listOf("Apple", "Banana", "Cherry")
fruits.forEach { println(it) }
// with index
fruits.forEachIndexed { index, fruit ->
    println("Fruit at index $index is $fruit")
}
 

while loop

val numbers = listOf(1, 2, 3, 4)
var i = 0
while (i < numbers.size) {
    println(numbers[i])
    i++
}

Iterating Over a Range

for (i in 1..5) {
    println(i) // Prints 1 to 5
}

(a) Using downTo

for (i in 5 downTo 1) {
    println(i) // Prints 5 to 1
}

(b) Using step

for (i in 1..10 step 2) {
    println(i) // Prints 1, 3, 5, 7, 9
}

Iterating over a map

val scores = mapOf("Alice" to 90, "Bob" to 85)
 
for ((name, score) in scores) {
    println("$name scored $score")
}

What are the different types of sensors in android device
  • Position - Magnetometer, GPS
  • Motion - Accelerometer, Gyroscope,
  • Environmental - Proximity Sensor, Light Sensor
  • Others - Fingerprint Sensor, IR Blaster, Ultrasonic Sensor

How to Access Sensors in Android?
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
 

How do you find memory leaks in an android application

Memory leak occur when objects are not properly released, leading to high memory consumption and performance issues

  1. Using Android Profiler
  2. Leak canary - 3rd party library

Common Causes of Memory Leaks & Fixes
CauseExampleFix
Static References to Activitiesstatic Context context = this;Use WeakReference<Context> instead.
Anonymous Inner ClassesHandler handler = new Handler() { ... }Use static inner classes + WeakReference.
Registered but Unreleased ListenerssensorManager.registerListener(...)Call unregisterListener() in onDestroy().
View Binding Leaksbinding = ActivityMainBinding.inflate(layoutInflater)Set binding = null in onDestroy().
Background Threads Holding ContextThread { while(true) { /* do something */ } }.start()Use ThreadPoolExecutor or CoroutineScope.
Large Bitmaps Not RecycledimageView.setImageBitmap(largeBitmap)Use bitmap.recycle() or Glide/Picasso.