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 ratingsJava: A complete practical solution 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 ratings
Related to Cracking Kotlin Interview
Related ebooks
Kotlin In-Depth [Vol-I]: A Comprehensive Guide to Modern Multi-Paradigm Language Rating: 0 out of 5 stars0 ratingsJava for Black Jack: Learn the Java Programming Language in One Session by Writing and Running a Java-Based Card Game Simulation Rating: 0 out of 5 stars0 ratingsCrystal Clear Java: 1St Edition Rating: 0 out of 5 stars0 ratingsPractical Java 8: Lambdas, Streams and new resources Rating: 5 out of 5 stars5/5Scala for Java Developers Rating: 5 out of 5 stars5/5Java Programming Rating: 0 out of 5 stars0 ratingsThe Ruby Workshop: Develop powerful applications by writing clean, expressive code with Ruby and Ruby on Rails Rating: 0 out of 5 stars0 ratings100+ Solutions in Java Rating: 0 out of 5 stars0 ratingsJavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsLearn Programming Using C# 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 ratingsJakarta EE for Java Developers: Build Cloud-Native and Enterprise Applications Using a High-Performance Enterprise Java Platform Rating: 0 out of 5 stars0 ratingsPuzzles in Java: Shaping Beginners Rating: 1 out of 5 stars1/5JavaScript : Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsProgramming Concepts in C++ Rating: 0 out of 5 stars0 ratingsJava 9 with JShell Rating: 0 out of 5 stars0 ratingsJava: Best Practices to Programming Code with Java: Java Computer Programming, #3 Rating: 0 out of 5 stars0 ratingsJAVA Programming Simplified: From Novice to Professional - Start at the Beginning and Learn the World of Java Rating: 0 out of 5 stars0 ratingsLearning Functional Data Structures and Algorithms Rating: 0 out of 5 stars0 ratingsJava with TDD from the Beginning Rating: 0 out of 5 stars0 ratingsAndroid Studio Electric Eel Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.1.1 and Kotlin Rating: 0 out of 5 stars0 ratingsMastering C# Concurrency Rating: 0 out of 5 stars0 ratingsFront-end Development with ASP.NET Core, Angular, and Bootstrap Rating: 0 out of 5 stars0 ratingsLearning Concurrent Programming in Scala - Second Edition Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming: Crafting Databases, APIs, and Web Servers for High-Performance Backend Rating: 0 out of 5 stars0 ratingsASP.NET Application Development Fundamentals Rating: 0 out of 5 stars0 ratings
Operating Systems For You
Mastering Swift 5 - Fifth Edition: Deep dive into the latest edition of the Swift programming language, 5th Edition Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Windows 11 All-in-One For Dummies Rating: 5 out of 5 stars5/5Linux Bible Rating: 0 out of 5 stars0 ratingsLinux Command-Line Tips & Tricks Rating: 0 out of 5 stars0 ratingsLearn Windows PowerShell in a Month of Lunches Rating: 0 out of 5 stars0 ratingsWindows 11 For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsUNIX For Dummies Rating: 3 out of 5 stars3/5Windows Registry Forensics: Advanced Digital Forensic Analysis of the Windows Registry Rating: 4 out of 5 stars4/5The Darknet Superpack Rating: 0 out of 5 stars0 ratingsHacking Essentials - The Beginner's Guide To Ethical Hacking And Penetration Testing Rating: 3 out of 5 stars3/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Make Your PC Stable and Fast: What Microsoft Forgot to Tell You Rating: 4 out of 5 stars4/5CompTIA Linux+ Study Guide: Exam XK0-004 Rating: 0 out of 5 stars0 ratingsMastering Bash Rating: 5 out of 5 stars5/5The Windows Command Line Beginner's Guide: Second Edition Rating: 4 out of 5 stars4/5Exploring Windows 11: The Illustrated, Practical Guide to Using Microsoft Windows Rating: 0 out of 5 stars0 ratingsMacs All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5Linux All-in-One For Dummies Rating: 3 out of 5 stars3/5MacOS Ventura Essentials: Support, Troubleshooting & Maintenance Guide for Beginners and Seniors Rating: 0 out of 5 stars0 ratingsMastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Networking for System Administrators: IT Mastery, #5 Rating: 5 out of 5 stars5/5iPhone Unlocked Rating: 0 out of 5 stars0 ratingsWindows Server 2019 & PowerShell All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsOneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/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