Code Monkey home page Code Monkey logo

kotlin-interview-questions's Introduction

kotlin-interview-questions

Kotlin is used for:

  • Mobile Application (specially for android)
  • web development
  • server side application
  • Data Science

Why to use kotlin ?

  • Kotlin is fully compatible with java
  • Kotlin works on different platform
  • Kotlin is concise and safe
  • Kotlin is esy to learn, especiallly if you already know java.
  • Kotlin is free to use.
  • Big community/support

How does Kotlin work on Android?

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.

what is primary constructor in Kotlin ?

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.

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

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

What is data class in kotlin ?

we frequently create classess whose main purpose is to hold data. In kotlin that is called a data class and is marked as data

data class User(val name: String, val age: Int)

To ensure consistency and meaningful behaviour of the generated code, data class has to fullfill the following requirement:

  • the primary constructor needs to have at least one parameter;
  • All primary constructor paramenters need to be marked as val and var;
  • Data classess cannot be abstract, open, sealed or inner;

Explain null safety in kotlin ?

Kotlins's system is aimed at eliminating the danger of null references from code, also known as the billion dollar mistake.

One of the most common pitfalls in many programming languages, including java, is that accessing a member of a null reference exception . In java this would be the equivalent of a NullPointerException or NPE for short. In kotlin, the type system distinguishes between reference that can hold null (nullable references) and those that can not (non-null reference). For example, a regular variable of type String can not hold null:

var a: String = "abc"
a = null // compilation error

To allow nulls, we can declare a variable as nullable string, written String?:

var b: String? = "abc"
b = null // ok
print(b)

kotlin-interview-questions's People

Contributors

devendra631997 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.