• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - How can you ensure null safety in Kotlin?

Answer -

Null safety is a feature introduced in Kotlin. In Java, if you access some null variable, then you will get a NullPointerException. So, the following code in Kotlin will produce a compile-time error:

var name: String = "MindOrks"  
name = null //error  
So, to overcome this issue, you have to assign null values to a variable, and you need to declare the name variable as a nullable string, and then during the access of this variable, you need to use a safe call operator; i.e.?.

var name: String? = "MindOrks"  
print(name?.length) // ok  
name = null // ok   

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners