Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions
By Swati Saxena
()
About this ebook
With a complete overview of OOPs, null safety, generics, and many other exciting features, this book is a perfect choice for fresher and experienced Java developers who want to learn more about this alternative JVM language.
Read more from Swati Saxena
Python Interview Questions: Brush up for your next Python interview with 240+ solutions on most common challenging interview questions (English Edition) Rating: 0 out of 5 stars0 ratingsKotlin at a Glance: Use of Lambdas and higher-order functions to write more concise, clean, reusable, and simple code Rating: 0 out of 5 stars0 ratingsJava: A complete practical solution Rating: 0 out of 5 stars0 ratings
Related to Cracking Kotlin Interview
Related ebooks
Code with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsKotlin In-Depth [Vol-I]: A Comprehensive Guide to Modern Multi-Paradigm Language Rating: 0 out of 5 stars0 ratingsThe Java Workshop: Learn object-oriented programming and kickstart your career in software development Rating: 0 out of 5 stars0 ratingsProgramming Kotlin Rating: 0 out of 5 stars0 ratingsAdvanced JAVA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratings120 Advanced JavaScript Interview Questions Rating: 0 out of 5 stars0 ratingsJakarta EE for Java Developers: Build Cloud-Native and Enterprise Applications Using a High-Performance Enterprise Java Platform Rating: 0 out of 5 stars0 ratings100+ Solutions in Java: A Hands-On Introduction to Programming in Java (English Edition) Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsSvelte: A Beginner's Guide Rating: 0 out of 5 stars0 ratingsUnleashing the Power of TypeScript Rating: 0 out of 5 stars0 ratingsJava: Tips and Tricks to Programming Code with Java Rating: 0 out of 5 stars0 ratingsMastering Symfony Rating: 0 out of 5 stars0 ratingsJava and Java EE Interview Preparations Rating: 0 out of 5 stars0 ratingsJava with TDD from the Beginning Rating: 0 out of 5 stars0 ratingsJava: Beginner's Guide to Programming Code with Java Rating: 0 out of 5 stars0 ratingsJavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsInstant Node Package Manager Rating: 2 out of 5 stars2/5Testing Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito Rating: 0 out of 5 stars0 ratingsLearning RxJava Rating: 4 out of 5 stars4/5Gradle for Android Rating: 0 out of 5 stars0 ratingsMastering Concurrency Programming with Java 8 Rating: 0 out of 5 stars0 ratingsThe Java Module System Rating: 4 out of 5 stars4/5Testing Angular Applications Rating: 0 out of 5 stars0 ratingsJava Hibernate Cookbook Rating: 0 out of 5 stars0 ratings
Operating Systems For You
Ubuntu Linux Bible Rating: 0 out of 5 stars0 ratingsMake Your PC Stable and Fast: What Microsoft Forgot to Tell You Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsMastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5macOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsWindows 11 For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsThe Mac Terminal Reference and Scripting Primer Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5The Windows Command Line Beginner's Guide: Second Edition Rating: 4 out of 5 stars4/5The Linux Command Line Beginner's Guide Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Windows 11 For Dummies Rating: 0 out of 5 stars0 ratingsExploring Windows 11: The Illustrated, Practical Guide to Using Microsoft Windows Rating: 0 out of 5 stars0 ratingsWindows 11 All-in-One For Dummies Rating: 5 out of 5 stars5/5Linux for Beginners: Linux Command Line, Linux Programming and Linux Operating System Rating: 4 out of 5 stars4/5iPhone Unlocked Rating: 0 out of 5 stars0 ratingsRHCSA Red Hat Enterprise Linux 8 (UPDATED): Training and Exam Preparation Guide (EX200), Second Edition Rating: 5 out of 5 stars5/5Linux Bible Rating: 0 out of 5 stars0 ratingsBash Command Line Pro Tips Rating: 5 out of 5 stars5/5The iPadOS 17: The Complete User Manual to Quick Set Up and Mastering the iPadOS 17 with New Features, Pictures, Tips, and Tricks Rating: 0 out of 5 stars0 ratingsRaspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsHacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Learn Windows PowerShell in a Month of Lunches Rating: 0 out of 5 stars0 ratingsPowerShell: A Comprehensive Guide to Windows PowerShell Rating: 4 out of 5 stars4/5Networking for System Administrators: IT Mastery, #5 Rating: 5 out of 5 stars5/5Learn SQL Server Administration in a Month of Lunches Rating: 3 out of 5 stars3/5
Reviews for Cracking Kotlin Interview
0 ratings0 reviews
Book preview
Cracking Kotlin Interview - Swati Saxena
CHAPTER 1
Core Concepts
Name some features which Kotlin supports but not Java?
Ans. Important Kotlin features that Java doesn’t have are:
Null Safety
Operator Overloading
Coroutines
Range expressions
Smart casts
Companion Objects
Name some features which Kotlin supports but not Java?
Ans. Two strings in Kotlin can be compared as:
(a) Using ==
operator
fun main(args: Array
val a: String = kotlin is easy
val b: String = kotlin is
+ easy
if(a==b){
println( a and b are equal.
)
} else {
println( a and b are not equal.
)
}
}
(b) Using compareTo()
fun main(args: Array
var a: String = apple
var b: String = apple
var result = a.compareTo(b)
if(result==0){
println(Strings ‘$a’ and ‘$b’ are equal.
)
} else if(result < 0){
println(’$a’ is less than ‘$b’ lexically.
)
} else{
println(’$a’ is less than ‘$b’ lexically.
)
}
b = banana
result = a.compareTo(b)
if(result==0){
println(Strings ‘$a’ and ‘$b’ are equal.
)
} else if(result < 0){
println(’$a’ is less than ‘$b’ lexically.
)
} else{
println(’$a’ is less than ‘$b’ lexically.
)
}
// passing ignoreCase to compareTo
a = appLE
b = aPple
println(\nIgnoring Case…
)
result = a.compareTo(b, true) // ignoreCase = true
if(result==0){
println(Strings ‘$a’ and ‘$b’ are equal.
)
} else if(result < 0){
println(’$a’ is less than ‘$b’ lexically.
)
} else{
println(’$a’ is less than ‘$b’ lexically.
)
}
}
Output
Strings ‘apple’ and ‘apple’ are equal.
‘apple’ is less than ‘banana’ lexically.
Ignoring Case…
Strings ‘appLE’ and ‘aPple’ are equal.
Differentiate among ==, ===, .equals() in Kotlin?
Ans.
Structural Equality (‘==’)
== operator is used to compare the data of two variables.
== operator in Kotlin only compares the data or variables, whereas in Java or other languages == is used to compare the references.
The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.
Referential equality (‘===’
=== operator is used to compare the reference of two variable or object.
It will only be true if both the objects and variables pointing to the same object.
The negated counterpart of === in Kotlin is !== which is used to compare if both the values are not equal to each other. For values which are represented as primitive types at runtime (for example, Int), the === equality check is equivalent to the == check.
.equals method
.equals(other: Any?) method is implemented in Any class and can be overridden in any extending class.
.equals method compares the content of the variables or objects just like == operator but it behaves differently in case of Float and Double comparison.
Example:
val first = Integer(10)
val second = Integer(10)
println(first == second) //true
println(first.equals(second)) //true
println(first === second) //false
………….
class Employee (val name: String)
val emp1 = Employee(Swati
)
val emp2 = Employee(Swati
)
println(emp1 == emp2) //false
println(emp1.equals(emp2)) //false
println(emp1 === emp2) //false
println(emp1.name == emp2.name) //true
println(emp1.name.equals(emp2.name)) //true
println(emp1.name === emp2.name) //true
Explain the use of init() in Kotlin?
Ans. A constructor is used to initialize the class properties. It is a special member function which is called when an object is instantiated (created).
In Kotlin, there are two constructors:
(a) Primary constructor:used to initialize a class
(b) Secondary constructor:allows you to put additional initialization logic
The primary constructor is part of the class header.
Example:
class Student(val subject: String, var fee: Int) {
// class body
}
The primary constructor has a constrained syntax, and cannot contain any code.
To put the initilization code (not only code to initialize properties), initializer block is used. It is prefixed with init keyword.
Example:
fun main(args: Array
val student1 = Student(swati
, 25)
}
class Student(Name: String, Age: Int) {
valname:String
var age: Int
// initializer block
init {
name= Name.capitalize()
age = Age
println(First Name = $name
)
println(Age = $age
)
}
}
What is repeat() in kotlin?
Ans. Repeat statement is like while loop, which executes a block of code, N-number of times (without any condition).
If you want to print a statement for N times without condition or looping, repeat() is used in Kotlin.
Example:
fun main(args: Array
repeat(5) {
println(Swati Computers
)
}
}
How does the program written in Kotlin run and what is the entry point of Kotlin programs?
Ans. The Kotlin program once compiled, can run on standard Java Virtual Machine (JVM) like other programming codes. And, like many other programming languages main() function is the entry point of the Kotlin.
State the differences between Val and Var?
Ans.
Val: Val, which is the short form of value, is a constant and it cannot be changed once assigned.
Var: Var, which is the short form of variable, is a storage location that accepts the reassignment of values that have the same data types.
How to instantiate class in Kotlin?
Ans. There is no new
keyword in kotlin to instantiate a class.
You can create class object as:
class XYZ
var a= XYZ()
val b = XYZ()
What is lazy and lateinit in kotlin?
Ans. Both lazy and lafeinitare used to delay the property initialization in kotlin.
Lazy is a method, which can be assign to val only. The value would be created at runtime when it is required.
val x: Int by lazy{100 }
Lateinit is a modifier, which is used to set the value to the var when required.
Lateinit var y: String
How to create static method in kotlin?
Ans. Kotlin does not support static keyword. To create a static method, you can use a companion object.
Example:
In java:
class Static
{
public static