أسئلة مقابلة Kotlin

Kotlin هي أحدث لغة برمجة على منصة JVM من شركة JetBrains. وقد جعلت Google منها اللغة الرسمية لتطوير تطبيقات Android إلى جانب Java. يقول المطورون إنها تتعامل مع المشاكل التي تواجه في برمجة Java. لقد كتبت العديد من البرامج التعليمية الخاصة بـ Kotlin وها أنا أقدم أسئلة مقابلة Kotlin المهمة.

أسئلة المقابلة في Kotlin

أقدم هنا أسئلة مقابلة Kotlin وإجاباتها التي ستساعدك في مقابلاتك في Kotlin. هذه الأسئلة مناسبة للمبتدئين وأيضًا للمبرمجين ذوي الخبرة. هناك أيضًا أسئلة برمجة لتنمية مهارات البرمجة الخاصة بك.

  1. ما هي منصة Kotlin المستهدفة؟ وكيف يكون التوافق بين Kotlin و Java ممكنًا؟

    منصة JVM هي المنصة المستهدفة لـ Kotlin. Kotlin متوافقة بنسبة 100٪ مع Java، حيث يتم إنتاج byte code عند الترجمة لكلا اللغتين. وبالتالي، يمكن استدعاء كود Kotlin من Java والعكس بالعكس.

  2. كيفية إعلان المتغيرات في لغة Kotlin؟ كيف يختلف الإعلان عن Java؟

    هناك اختلافين رئيسيين بين إعلان المتغيرات في Java و Kotlin:

    • نوع الإعلان في Java، يبدو الإعلان كالتالي:

      String s = "Java String";
      int x = 10;
      

      في Kotlin، يبدو الإعلان كالتالي:

      val s: String = "Hi"
      var x = 5
      

      في Kotlin، يبدأ الإعلان بكلمة مفتاحية val أو var تليها النوع الاختياري. يمكن لـ Kotlin اكتشاف النوع تلقائيًا باستخدام التعيين التلقائي للنوع.

    • القيمة الافتراضية يمكن تحقيق الأمر التالي في Java:

      String s:
      

      الإعلان التالي في Kotlin غير صحيح.

      val s: String
      
  3. ما هو الفرق بين تعريف الـ val والـ var؟ كيفية تحويل سلسلة نصية إلى عدد صحيح؟

    المتغيرات من نوع val لا يمكن تغييرها. إنها تشبه الـ final modifiers في لغة جافا. يمكن إعادة تعيين قيمة المتغير من نوع var، ولكن يجب أن تكون القيمة الجديدة من نفس نوع البيانات.

    fun main(args: Array<String>) {
        val s: String = "مرحبا"
        var x = 5
        x = "6".toInt()
    }
    

    نستخدم الطريقة toInt() لتحويل السلسلة النصية إلى عدد صحيح.

  4. ما هي السلامة الفارغة وأنواع القيم القابلة للتفاوض في كوتلن؟ ما هو مشغل إلفيس؟
    كوتلن يضع الكثير من الوزن على سلامة الفارغة وهي نهج لمنع استثناءات الإشارة الفارغة المخيفة عن طريق استخدام أنواع القيم القابلة للتفاوض والتي تشبه String؟ ، Int؟ ، Float؟ إلخ. هذه الأنواع تعمل كنوع الغلاف ويمكن أن تحمل قيم فارغة. لا يمكن إضافة قيمة قابلة للتفاوض إلى قيمة قابلة للتفاوض أخرى أو نوع قيمة أساسية. لاسترداد الأنواع الأساسية ، نحتاج إلى استخدام استدعاءات آمنة تفك الحزمة القابلة للتفاوض. إذا كانت القيمة فارغة بعد فتحها ، يمكننا اختيار تجاهلها أو استخدام قيمة افتراضية بدلاً من ذلك. يتم استخدام مشغل إلفيس لفك حزمة القيمة بأمان من القابلة للتفاوض. يتم تمثيله بواسطة ?: فوق نوع القابلة للتفاوض. سيتم استخدام القيمة على الجانب الأيمن إذا كان النوع القابل للتفاوض يحمل قيمة فارغة.

    var str: String?  = "JournalDev.com"
    var newStr = str?: "Default Value" 
    str = null
    newStr = str?: "Default Value"
    

  5. ما هو const؟ كيف يختلف عن val؟

    بشكل افتراضي ، تُعين خصائص val في وقت التشغيل. إضافة عنصر تعديل const على val سيجعلها ثابتة في وقت الترجمة. لا يمكن استخدام const مع var أو بمفرده. const لا ينطبق على متغير محلي.

  6. هل تسمح Kotlin لنا باستخدام أنواع البيانات الأساسية مثل int و float و double؟

    على مستوى اللغة، لا يمكننا استخدام الأنواع المذكورة أعلاه. ولكن تحتوي بايت كود JVM المترجم على هذه الأنواع بالتأكيد.

  7. ما هو نقطة الدخول لكل برنامج Kotlin؟

    الدالة main هي نقطة الدخول لكل برنامج Kotlin. في Kotlin يمكننا عدم كتابة الدالة main داخل الصف. عند تجميعها، تقوم الآلة الافتراضية لـ JVM بتجميعها ضمن صف. يتم استخدام السلاسل الممررة بتنسيق Array<String> لاسترجاع وسائط سطر الأوامر.

  8. كيف يختلف !! عن ? في فك قيم القيم القابلة للتفكيك؟ هل هناك طريقة أخرى لفك قيم القيم القابلة للتفكيك بأمان؟

    يتم استخدام !! لفك قيمة النوع القابل للتفكيك بقوة للحصول على القيمة. إذا كانت القيمة المُرجعة هي قيمة فارغة، فسيؤدي ذلك إلى تعطل أثناء التشغيل. وبالتالي يجب استخدام عامل !! فقط عندما تكون متأكدًا تمامًا من أن القيمة لن تكون فارغة على الإطلاق. وإلا، ستحصل على استثناء الإشارة الفارغة المخيفة. من ناحية أخرى، ? هو عامل الفيلس الذي يقوم بالتحقق من القيمة بأمان. يمكننا استخدام تعبير اللامبدا let على القيمة القابلة للتفكيك لفكها بأمان كما هو موضح أدناه. هنا يقوم تعبير let بالتحقق من القيمة القابلة للتفكيك بأمان.

  9. كيف يتم تعريف الدالة؟ لماذا تُعرف الدوال في Kotlin بأنها دوال على مستوى الجذر؟

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

    يتم تعريف نوع العائد للدالة بعد الحرف : يمكن تعريف الدوال في Kotlin في جذر ملف Kotlin.

  10. ما هو الفرق بين مشغلات == و === في Kotlin؟

\== is used to compare the values are equal or not. === is used to check if the references are equal or not.
  1. اكتب قائمة بالتعديلات المرئية المتاحة في Kotlin. ما هو التعديل المرئي الافتراضي؟

-   public
-   internal
-   protected
-   private

`public` is the default visibility modifier.
  1. هل تترجم هيكلة التوريث التالية؟

```
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(){

}
```
  1. ما هي أنواع المباني في كوتلن؟ ما هي الاختلافات بينها؟ كيف تعرفها في صنفك؟

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
}

}
```
  1. ما هو كتلة init في كوتلن

`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.
  1. كيف يعمل تضمين السلسلة في كوتلن؟ شرح باستخدام مقتطف الشيفرة؟

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.
  1. ما هو نوع الوسيطة داخل البناء؟

By default, the constructor arguments are `val` unless explicitly set to `var`.
  1. هل new كلمة محجوزة في كوتلن؟ كيف يمكنك إنشاء كائن لفئة في كوتلن؟

**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()
```
  1. ما هو ما يعادل تعبير switch في 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.
  1. ما هي فئات البيانات في كوتلن؟ ما الذي يجعلها مفيدة جدًا؟ كيف يتم تعريفها؟

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.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-data-classes.png)](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-data-classes.png)
  1. ما هي تعريفات التفكيك في كوتلن؟ قم بشرحها بمثال.

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.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-destructuring-declarations.png)](https://journaldev.nyc3.cdn.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.
  1. ما هو الفرق بين الدوال المضمنة والدوال المشطوبة؟ قم بإعطاء مثال لكل منهما.

[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.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-inline-functions.png)](https://journaldev.nyc3.cdn.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.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-infix-notations.png)](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-infix-notations.png)
  1. ما هو الفرق بين الخاصية lazy والخاصية 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
```
  1. كيفية إنشاء فئات Singleton؟

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.
  1. هل 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()`.
  1. ما هو نوع المصفوفة التالية؟

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

The type is Array<Int>.

هذا كل شيء بالنسبة لأسئلة وأجوبة مقابلة Kotlin.

Source:
https://www.digitalocean.com/community/tutorials/kotlin-interview-questions