Top Kotlin Frequently Asked Interview Questions | JavaInUse



Kotlin Framework Interview Questions


In this post we will look at Kotlin Interview questions. Examples are provided with explanation.


Q: What is Kotlin?
A:
Kotlin is a programming language developed by Jetbrains. It runs primarily on the Java Virtual Machine (JVM) but also provides the ability to target JavaScript allowing it to be used for browsers and JavaScript server-side development.
A native implementation is being worked on to allow compilation for platforms where virtual machines are not desirable or possible.
Kotlin is now an official language on Android. It's expressive, concise, and powerful. .

Q: What are the advantages of using Kotlin?
A:
The Advantages of Kotlin are as follows -
  • Lambda expressions + Inline functions
  • Extension functions
  • Null-safety
  • Smart casts
  • Properties
  • Primary constructors
  • First-class delegation
  • Type inference for variable and property types
  • Singletons

Q: What type of programming styles does Kotlin support?
A:
Kotlin supports all three programming styles -
  • Procedural - Kotlin supports top-level functions outside of a class so you can write code that looks strictly procedural.
  • Functional - Kotlin has first-class functions, lambdas, and tail recursion, which are the building blocks of functional programming.
  • Object Oriented - Similar to Java OO features where it makes use of classes

Q: What is the difference between Java class and kotlin data class?
A:
The Kotlin language introduces the concept of Data Classes, which represent simple classes used as data containers. As the name indicates it is used to only store data and contains no other business logic .
The regular java class is as follows-

public class Employee {

	private String empName;
	private int empId;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + empId;
		result = prime * result + ((empName == null) ? 0 : empName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (empId != other.empId)
			return false;
		if (empName == null) {
			if (other.empName != null)
				return false;
		} else if (!empName.equals(other.empName))
			return false;
		return true;
	}
}
The same using Kotin is as follows -

data class Employee(var empName: String, var empId: Int)
The Constructor, toString(), equals(), hashCode(), copy() , componentN() functions are generated automatically. So there is less boiler plate code

Q: What is the difference between var and val in Kotin?
A:
  • variables defined with var are mutable(Read and Write)
    var a: Int=3
    a *= a
    	
  • variables defined with val are immutable(Read only)
    val b: Int = 6
    b*=b
    

See Also

Spring Batch Interview Questions Apache Camel Interview Questions JBoss Fuse Interview Questions Drools Interview Questions Java 8 Interview Questions