
Practice 50 Kotlin Null Safety and Smart Casts Coding Questions
Q1. Write a Kotlin program to check if a nullable string is null using the safe call operator (?.).
Input: val str: String? = "Hello"
Expected Output: "Hello"
Q2. Write a Kotlin program to print "Not null" if a nullable string is not null using the safe call operator.
Input: val str: String? = null
Expected Output: "null"
Q3. Write a Kotlin program to safely access the length of a nullable string using the safe call operator.
Input: val str: String? = "Kotlin"
Expected Output: 6
Q4. Write a Kotlin program to use the elvis operator (?:) to provide a default value if a nullable string is null.
Input: val str: String? = null
Expected Output: "Default"
Q5. Write a Kotlin program to use the elvis operator (?:) to return a default value for a nullable integer.
Input: val num: Int? = null
Expected Output: 0
Q6. Write a Kotlin program to safely access the length of a nullable string and return "null" if it is null.
Input: val str: String? = null
Expected Output: "null"
Q7. Write a Kotlin program to check if a nullable string is null or empty.
Input: val str: String? = "Kotlin"
Expected Output: false
Q8. Write a Kotlin program to use the !! operator to assert that a nullable value is not null.
Input: val str: String? = "Hello"
Expected Output: "Hello"
Q9. Write a Kotlin program to demonstrate a NullPointerException when using the !! operator with a null value.
Input: val str: String? = null
Expected Output: NullPointerException
Q10. Write a Kotlin program to use the let function with a nullable value to execute code only if the value is not null.
Input: val str: String? = "Kotlin"
Expected Output: "Not null"
Q11. Write a Kotlin program to safely access the value of a nullable integer inside a let block.
Input: val num: Int? = 5
Expected Output: 5
Q12. Write a Kotlin program to use the safe call operator with a method on a nullable string.
Input: val str: String? = "Hello"
Expected Output: "Hello".toUpperCase()
Q13. Write a Kotlin program to check if a nullable integer is null and return a default value using let.
Input: val num: Int? = null
Expected Output: 0
Q14. Write a Kotlin program to perform null safety check and print the length of a nullable string if it's not null.
Input: val str: String? = "Kotlin"
Expected Output: 6
Q15. Write a Kotlin program to demonstrate null safety when accessing an element of a nullable list.
Input: val list: List<String>? = listOf("Kotlin", "Java")
Expected Output: Kotlin
Q16. Write a Kotlin program to use ? let's print the length of a nullable string if it's not null.
Input: val str: String? = "Programming"
Expected Output: 11
Q17. Write a Kotlin program to use the !! operator on a nullable integer and print its value.
Input: val num: Int? = 5
Expected Output: 5
Q18. Write a Kotlin program to demonstrate how to use smart casting with a nullable variable.
Input: val obj: Any? = "Kotlin"
Expected Output: Kotlin (after smart casting)
Q19. Write a Kotlin program to safely cast an object to a specific type using as?.
Input: val obj: Any? = 10
Expected Output: 10
Q20. Write a Kotlin program to use safe casting to Int? and print the result.
Input: val obj: Any? = "Kotlin"
Expected Output: null
Q21. Write a Kotlin program to use the smart cast feature to check the type of an object.
Input: val obj: Any = "Hello"
Expected Output: "String"
Q22. Write a Kotlin program to check the type of a nullable object using the is operator.
Input: val obj: Any? = 10
Expected Output: "Integer"
Q23. Write a Kotlin program to smart cast a nullable type after checking it with the is operator.
Input: val obj: Any? = "Hello"
Expected Output: "Hello" (smart cast to String)
Q24. Write a Kotlin program to use smart cast with an else condition in an if statement.
Input: val obj: Any? = 5
Expected Output: "Integer"
Q25. Write a Kotlin program to handle a nullable variable using the when expression and smart cast.
Input: val obj: Any? = 5
Expected Output: "Integer"
Q26. Write a Kotlin program to safely perform a cast and handle the case where casting is not possible using as?.
Input: val obj: Any? = "Kotlin"
Expected Output: null
Q27. Write a Kotlin program to handle nullable variables in a when expression without explicit null checks.
Input: val obj: Any? = null
Expected Output: "Null value"
Q28. Write a Kotlin program to use smart casting within a function that checks if an object is of type String.
Input: val obj: Any? = "Kotlin"
Expected Output: "Kotlin"
Q29. Write a Kotlin program to safely access a nullable integer and perform arithmetic operations.
Input: val num: Int? = 10
Expected Output: 20 (adding 10)
Q30. Write a Kotlin program to handle a nullable object of type String and safely convert it to uppercase.
Input: val str: String? = "kotlin"
Expected Output: "KOTLIN"
Q31. Write a Kotlin program to safely access the first element of a nullable list.
Input: val list: List<String>? = listOf("A", "B")
Expected Output: "A"
Q32. Write a Kotlin program to use safe calls to perform operations on nullable objects.
Input: val str: String? = "Kotlin"
Expected Output: "Kotlin".toLowerCase()
Q33. Write a Kotlin program to use the !! operator on a nullable string and handle potential exceptions.
Input: val str: String? = null
Expected Output: NullPointerException
Q34. Write a Kotlin program to check if a nullable string contains a substring using safe calls.
Input: val str: String? = "Kotlin"
Expected Output: true
Q35. Write a Kotlin program to use as? to cast an object safely to String and print it.
Input: val obj: Any? = "Kotlin"
Expected Output: "Kotlin"
Q36. Write a Kotlin program to use the smart cast feature inside a function to safely cast and access properties.
Input: val obj: Any? = "Hello"
Expected Output: "Hello"
Q37. Write a Kotlin program to use the smart cast feature after checking type using is.
Input: val obj: Any? = 10
Expected Output: "Integer"
Q38. Write a Kotlin program to handle null safety in a list of nullable integers and print their sum.
Input: val numbers: List<Int?> = listOf(1, 2, null, 4)
Expected Output: 7
Q39. Write a Kotlin program to check and cast a nullable object to String using as?.
Input: val obj: Any? = "Kotlin"
Expected Output: "Kotlin"
Q40. Write a Kotlin program to use smart casting in a when expression to check the type and print the type name.
Input: val obj: Any? = 10
Expected Output: "Integer"
Q41. Write a Kotlin program to check if a nullable integer is positive or negative using null safety.
Input: val num: Int? = -5
Expected Output: "Negative"
Q42. Write a Kotlin program to use smart casting inside a when expression to check the type of a variable.
Input: val obj: Any? = "Kotlin"
Expected Output: "String"
Q43. Write a Kotlin program to use !! operator to assert that a nullable value is not null and perform an operation.
Input: val num: Int? = 5
Expected Output: 10
Q44. Write a Kotlin program to handle nullable values and perform arithmetic operations if the value is not null.
Input: val num: Int? = 10
Expected Output: 20
Q45. Write a Kotlin program to safely access the property of a nullable object using the safe call operator.
Input: val person: Person? = Person("John", 25)
Expected Output: "John"
Q46. Write a Kotlin program to use the let function with nullable types for null safety.
Input: val str: String? = "Hello"
Expected Output: "Hello"
Q47. Write a Kotlin program to safely cast a nullable string to uppercase using safe call operators.
Input: val str: String? = "kotlin"
Expected Output: "KOTLIN"
Q48. Write a Kotlin program to use the smart cast feature after checking the type in a conditional statement.
Input: val obj: Any? = "Hello"
Expected Output: "Hello"
Q49. Write a Kotlin program to perform a null safety check and print the value if it is not null.
Input: val str: String? = "Kotlin"
Expected Output: "Kotlin"
Q50. Write a Kotlin program to demonstrate smart casting in a when statement with multiple branches.
Input: val obj: Any? = 100
Expected Output: "Integer"