웹사이트 검색

Kotlin 인터뷰 질문


Kotlin은 JetBrains의 최신 JVM 프로그래밍 언어입니다. Google은 Java와 함께 Android 개발의 공식 언어로 만들었습니다. 개발자들은 Java 프로그래밍에서 직면한 문제를 해결한다고 말합니다. 나는 많은 Kotlin 튜토리얼을 작성했으며 여기에서 중요한 Kotlin 인터뷰 질문을 제공하고 있습니다.

Kotlin 인터뷰 질문

여기서는 Kotlin 면접에 도움이 될 Kotlin 면접 질문과 답변을 제공합니다. 이 Kotlin 인터뷰 질문은 숙련된 프로그래머뿐만 아니라 초보자에게도 유용합니다. 코딩 기술을 연마하기 위한 코딩 질문도 있습니다.

\== is used to compare the values are equal or not. === is used to check if the references are equal or not.

Kotlin에서 사용할 수 있는 가시성 수정자를 나열합니다. 기본 가시성 수정자는 무엇입니까?

-   public
-   internal
-   protected
-   private

`public` is the default visibility modifier.

다음 상속 구조가 컴파일됩니까?

```
class A{
}

class B : A(){

}
```

**NO**. By default classes are final in Kotlin. To make them non-final, you need to add the `open` modifier.

```
open class A{
}

class B : A(){

}
```

Kotlin의 생성자 유형은 무엇입니까? 그들은 어떻게 다릅니 까? 수업에서 어떻게 정의합니까?

Constructors in Kotlin are of two types: **Primary** - These are defined in the class headers. They cannot hold any logic. There's only one primary constructor per class. **Secondary** - They're defined in the class body. They must delegate to the primary constructor if it exists. They can hold logic. There can be more than one secondary constructors.

```
class User(name: String, isAdmin: Boolean){

constructor(name: String, isAdmin: Boolean, age: Int) :this(name, isAdmin)
{
    this.age = age
}

}
```

Kotlin의 초기화 블록이란?

`init` is the initialiser block in Kotlin. It's executed once the primary constructor is instantiated. If you invoke a secondary constructor, then it works after the primary one as it is composed in the chain.

Kotlin에서 문자열 보간은 어떻게 작동하나요? 코드 스니펫으로 설명하시겠습니까?

String interpolation is used to evaluate string templates. We use the symbol $ to add variables inside a string.

```
val name = "Journaldev.com"
val desc = "$name now has Kotlin Interview Questions too. ${name.length}"
```

Using `{}` we can compute an expression too.

생성자 내부의 인수 유형은 무엇입니까?

By default, the constructor arguments are `val` unless explicitly set to `var`.

new는 Kotlin의 키워드입니까? Kotlin에서 클래스 객체를 어떻게 인스턴스화하시겠습니까?

**NO**. Unlike Java, in Kotlin, new isn't a keyword. We can instantiate a class in the following way:

```
class A
var a = A()
val new = A()
```

Kotlin에서 switch 표현식과 동일한 것은 무엇입니까? 스위치와 어떻게 다른가요?

when is the equivalent of `switch` in `Kotlin`. The default statement in a when is represented using the else statement.

```
var num = 10
    when (num) {
        0..4 -> print("value is 0")
        5 -> print("value is 5")
        else -> {
            print("value is in neither of the above.")
        }
    }
```

`when` statments have a default break statement in them.

Kotlin의 데이터 클래스는 무엇입니까? 그것들이 그렇게 유용한 이유는 무엇입니까? 그것들은 어떻게 정의됩니까?

In Java, to create a class that stores data, you need to set the variables, the getters and the setters, override the `toString()`, `hash()` and `copy()` functions. In Kotlin you just need to add the `data` keyword on the class and all of the above would automatically be created under the hood.

```
data class Book(var name: String, var authorName: String)

fun main(args: Array<String>) {
val book = Book("Kotlin Tutorials","Anupam")
}
```

Thus, data classes saves us with lot of code. It creates component functions such as `component1()`.. `componentN()` for each of the variables. [![kotlin interview questions data classes](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-data-classes.png)](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-data-classes.png)

Kotlin의 구조 분해 선언이란 무엇입니까? 예를 들어 설명하십시오.

Destructuring Declarations is a smart way to assign multiple values to variables from data stored in objects/arrays. [![kotlin interview questions destructuring declarations](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-destructuring-declarations.png)](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-destructuring-declarations.png) Within paratheses, we've set the variable declarations. Under the hood, destructuring declarations create component functions for each of the class variables.

인라인 함수와 중위 함수의 차이점은 무엇입니까? 각각의 예를 제시하십시오.

[Inline functions](/community/tutorials/kotlin-inline-function-reified) are used to save us memory overhead by preventing object allocations for the anonymous functions/lambda expressions called. Instead, it provides that functions body to the function that calls it at runtime. This increases the bytecode size slightly but saves us a lot of memory. [![kotlin interview questions inline functions](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-inline-functions.png)](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-inline-functions.png) [infix functions](/community/tutorials/kotlin-functions) on the other are used to call functions without parentheses or brackets. Doing so, the code looks much more like a natural language. [![kotlin interview questions infix notations](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-infix-notations.png)](https://journaldev.nyc3.digitaloceanspaces.com/2018/04/kotlin-interview-questions-infix-notations.png)

게으른 것과 lateinit의 차이점은 무엇입니까?

Both are used to delay the property initializations in Kotlin `lateinit` is a modifier used with var and is used to set the value to the var at a later point. `lazy` is a method or rather say lambda expression. It's set on a val only. The val would be created at runtime when it's required.

```
val x: Int by lazy { 10 }
lateinit var y: String
```

싱글톤 클래스를 만드는 방법은 무엇입니까?

To use the singleton pattern for our class we must use the keyword `object`

```
object MySingletonClass
```

An `object` cannot have a constructor set. We can use the init block inside it though.

Kotlin에 static 키워드가 있나요? Kotlin에서 정적 메서드를 만드는 방법은 무엇입니까?

**NO**. Kotlin doesn't have the static keyword. To create static method in our class we use the `companion object`. Following is the Java code:

```
class A {
  public static int returnMe() { return 5; }
}

```

The equivalent Kotlin code would look like this:

```
class A {
  companion object {
     fun a() : Int = 5
  }
}
```

To invoke this we simply do: `A.a()`.

다음 배열의 유형은 무엇입니까?

```
val arr = arrayOf(1, 2, 3);
```

The type is Array<Int>.

여기까지가 Kotlin 면접 질문과 답변의 전부입니다.