100 points
3

Want to solve programming problems and get paid for it? If that sounds interesting to you then contact us.

Q1. What is kotlin?

Answer: Kotin is an open-source programming language that executes using Java virtual machine as its platform.it was developed by JetBrains.it is comparatively simpler than Java and offers several features which Java does not have a hold on. it is a combination of procedural and object-oriented programming language.

Source : learning.shine.com

Q2. Key features of kotlin which are not available in java?

Answer: There are some key features that are not available in java.

  • Extension Functions
  • Operator Overloading
  • Smart casts
  • Data classes
  • Range expressions
  • Null Safety
  • Coroutines
  • Companion Objects

Source : educba.com

Q3.What’s the difference between val and var declaration? How to convert a String to an Int?

Answer:  val variables cannot be changed. They’re like final modifiers in Java. A var can be reassigned. The reassigned value must be of the same data type.


fun main(args: Array<String>) {
    val s: String = "Hi"
    var x = 5
    x = "6".toInt()
}

We use the toInt() method to convert the String to an Int.

Source : journaldev.com

Q4. How does Kotlin work on Android?

Answer:  Just like Java, the Kotlin code is also compiled into the Java bytecode and is executed at runtime by the Java Virtual Machine i.e. JVM. When a Kotlin file named Main.kt is compiled then it will eventually turn into a class and then the bytecode of the class will be generated. The name of the bytecode file will be MainKt.class and this file will be executed by the JVM.

Source : blog.mindorks.com

Q5. How is a function declared? Why are Kotlin functions known as top-level functions?

Answer: 


fun sumOf(a: Int, b: Int): Int{
    return a + b
}

A function’s return type is defined after the :
Functions in Kotlin can be declared at the root of the Kotlin file.

Source : journaldev.com

Q6. How to ensure null safety in Kotlin?

Answer: One of the major advantages of using Kotlin is null safety. 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 assign null values to a variable, 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

Source : blog.mindorks.com

Q7.Explain functions in Kotlin?

Answer:  Kotlin functions are opening class functions that are stored in variables and data structures and can be overtaken as arguments and returned from other higher-order functions.

Sample function declaration and usage in Kotlin:

Fun double  (x:Int) : Int {

Return 2* x

}

Val result= double (2)

Source : learning.shine.com

Q8. Does Kotlin Allow Calling Java Functions?

Answer: Yes, Kotlin allows the programmer to call existing Java procedures from within a Kotlin program. Functions such as the getter and setter duo are represented as properties. Kotlin assigns Unit to each void value that comes from a Java function. Thus, the Unit in Kotlin is simply void in Java code.

You will need to escape some Kotlin keywords in Java though. Since keywords like is, in, and object are valid identifiers in Java, you’ll need to escape them in Java libraries using the backtick (`) character. Additionally, Kotlin implements features like platform types and annotations to ensure null safety when calling external Java functions.

Source : ubuntupit.com

Q9. In the java.io.file list, the extension methods kotlin provides?

Answer: Some extension method provides:

  • bufferedReader(): Reading file to BufferedReader
  • readBytes(): Reading file to ByteArray
  • readText(): Reading file to single String
  • forEachLine(): Use for reading a file line by line in Kotlin
  • readLines(): Reading file to List

Source : learning.shine.com

Q10. What is a primary constructor in Kotlin?

Answer: The primary constructor is part of the class header. Unlike Java, you don't need to declare a constructor in the body of the class. Here's an example:

class Person(val firstName: String, var age: Int) {
    // class body
}

The main idea is by removing the constructor keyword, our code gets simplified and easy to understand.

Source: programiz.com

Q11. What is a purpose of Companion Objects in Kotlin?

Answer: Unlike Java or C#, Kotlin doesn’t have static members or member functions. If you need to write a function that can be called without having a class instance but needs access to the internals of a class, you can write it as a member of a companion object declaration inside that class.

class EventManager {

    companion object FirebaseManager {
    }  
}

val firebaseManager = EventManager.FirebaseManager

The companion object is a singleton. The companion object is a proper object on its own, and can have its own supertypes - and you can assign it to a variable and pass it around. If you're integrating with Java code and need a true static member, you can annotate a member inside a companion object with @JvmStatic.

Source: gist.github.com

Q12. What type of programming styles does Kotlin support?

Answer: 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

Source : javainuse.com

Q13. What are the high order functions?

Answer: High order functions consider functions as a parameter and produce a function.

Source : mindmajix.com

Q14. What is the entry point to a Kotlin program ? 

Answer: Like most of the other procedural languages, main() function is the entry point to a Kotlin program.

An Example for main() function is

fun main(args: Array<String>) {
    val user1 = User(name="Yogi", age=27)
    printUser(user1)
}
  
fun printUser(user: User){
    println(user)
}
  
data class User(val name: String, val age: Int);

Source : tutorialkart.com

Q15. How to convert a String into an int in the kotlin?

Answer: To convert a string value to the string value to it in kotlin we use point() method.

Let us see an example: 

function main(args: array)

{

    val s: str]ng = "Kotlin"

    var y = 10

    y = "10".toint()

}

Source : mindmajix.com

100 points
3