Kotlin היא שפת תכנות JVM המתקדמת מבית JetBrains. גוגל הכריזה עליה כשפת התכנות הרשמית לפיתוח אנדרואיד יחד עם ג'אווה. מפתחים אומרים שהיא פותרת את הבעיות הנתקלות בתכנות בג'אווה. כתבתי המון מדריכים ב-Kotlin וכאן אני מספק שאלות ראיון חשובות ב-Kotlin.
שאלות ראיון ב-Kotlin
כאן אני מציע שאלות ראיון ב-Kotlin ותשובות שיעזרו לך בראיונות ב-Kotlin. שאלות אלו מתאימות למתחילים וגם למתכנתים מנוסים. יש גם שאלות קוד שיעזרו לך לשפר את מיומנויות התכנות שלך.
-
מהו הפלטפורמה היעד של Kotlin? איך אפשרית האינטרואופרביליטי בין Kotlin ל-Java?
Java Virtual Machine (JVM) היא הפלטפורמה היעד של Kotlin. Kotlin תומכת באינטרואופרביליטי ב-100% עם Java מאחר ושני השפות, בעת ההמרה, מייצרות בייטקוד. לכן, קוד Kotlin יכול להיות קרוא מ-Java ולהיפך.
-
איך מצהירים משתנים ב-Kotlin? איך ההצהרה דומה להצהרה ב-Java?
ישנם שני הבחנים מרכזיים בין ההצהרה על משתנה ב-Java לבין ההצהרה ב-Kotlin:
-
סוג ההצהרה ב-Java ההצהרה נראית כך:
String s = "Java String"; int x = 10;
ב-Kotlin, ההצהרה נראית כך:
val s: String = "Hi" var x = 5
ב-Kotlin, ההצהרה מתחילה עם
val
ו־var
שנבדקים באופן אוטומטי באמצעות ניחוש סוג המשתנה. -
ערך ברירת המחדל הבא ייתכן ב-Java:
String s:
הצהרת משתנה כזו ב-Kotlin אינה תקינה.
val s: String
-
-
מה ההבדל בין הכרזה על val ו-var? איך ממירים מחרוזת למספר שלם?
val
משתנים אינם ניתנים לשינוי. הם דומים למודיפיקטורים סופיים בג'אווה.var
יכול להיות מוקצה מחדש. הערך המוקצה מחדש חייב להיות מאותו סוג נתונים.fun main(args: Array<String>) { val s: String = "שלום" var x = 5 x = "6".toInt() }
אנו משתמשים בשיטה
toInt()
כדי להמיר את המחרוזת למספר שלם. -
מה זה בטיחות נאל וסוגים מקובלים ב-Kotlin? מהו אופרטור האלביס?
Kotlin מדגישה בצורה חזקה את בטיחות ה-Null, שהיא גישה למניעת חריגות מצב Null Pointer על ידי שימוש בסוגי משתנים מקבילים כמו
String?
,Int?
,Float?
וכו'. אלו פועלים כסוג תוף ויכולים להכיל ערכים של Null. לא ניתן להוסיף ערך Null לסוג אחר מסוג בסיסי או מסוג משתנה עם ערך בסיסי. כדי לשחזר את הסוגים הבסיסיים, עלינו להשתמש בקרואים בצורה בטוחה שמפשטים את סוגי המשתנים המקובלים. אם בעת פתיחת הסוג המקובל, הערך הוא Null, אנו יכולים לבחור להתעלם או להשתמש בערך ברירת מחדל. אופרטור האלביס משמש לפתיחה בצורה בטוחה של הערך מהסוג המקובל. הוא מיוצג כ?:
מעל לסוג המקובל. הערך בצד ימין יועמד במקום אם הסוג המקובל מכיל ערך Null.var str: String? = "JournalDev.com" var newStr = str?: "ערך ברירת מחדל" str = null newStr = str?: "ערך ברירת מחדל"
-
מהו
const
? במה הוא שונה מ־val
?כברירת מחדל, מאפייני
val
מוגדרים בזמן ריצה. הוספת המילה המפתח const ל־val
יגרום לו להיות קבוע בזמן הקומפילציה.const
אינו יכול להיות בשימוש עםvar
או לבדו.const
אינו רלוונטי למשתנה מקומי. -
האם Kotlin מאפשרת לנו להשתמש בסוגי נתונים פרימיטיביים כמו int, float, double?
ברמת השפה, אין לנו אפשרות להשתמש בסוגי הנתונים האמורים לעיל. אבל בייצוג הבינארי של JVM שמומר, הם כן קיימים.
-
מהו נקודת הכניסה של כל תוכנית Kotlin?
הפונקציה
main
היא נקודת הכניסה של כל תוכנית Kotlin. ב-Kotlin יש אפשרות לבחור שלא לכתוב את הפונקציה הראשית בתוך המחלקה. בעת ההמרה ל-JVM, היא מתואמת אוטומטית במחלקה. המחרוזות שמועברות בצורתArray<String>
משמשות לאחזור את ארגומנטי שורת הפקודה. -
כיצד
!!
שונה מ?. בפתיחת הערכים הניתנים למחיקה? האם ישנה דרך אחרת לפתוח ערכים ניתנים למחיקה בצורה בטוחה?!!
משמש לפתיחת ערך ניתן למחיקה בכדי לקבל את הערך. אם הערך שיחזור הוא ערך ריק, זה יגרום להתרסקות בזמן ריצה. לכן, אופרטור!!
צריך להשתמש רק כאשר אתה מוכן לקבל את הערך האפשרי שלא יהיה ריק כלל. אחרת, תקבל את יוצא הדופן של יוצא הדופן הריק. בניגוד, ?. הוא אופרטור אלוויס שעושה קריאה בטוחה. אנו יכולים להשתמש בביטוי הלמבדהlet
על הערך הניתן למחיקה כדי לפתוח בצורה בטוחה כפי שמוצג למטה.כאן ביטוי ה־let עושה קריאה בטוחה לפתיחת סוג הנתונים הניתן למחיקה.
-
איך מוגדרת פונקציה? למה פונקציות ב־Kotlin ידועות כפונקציות רמה עליונה?
fun sumOf(a: Int, b: Int): Int{ return a + b }
סוג ההחזרה של הפונקציה מוגדר אחרי התו
:
. פונקציות ב־Kotlin יכולות להיות מוגדרות בראש הקובץ של Kotlin. -
מה ההבדל בין אופרטורי == ו־=== ב־Kotlin?
\== is used to compare the values are equal or not. === is used to check if the references are equal or not.
- 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(){
}
```
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
}
}
```
`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.
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`.
**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()
```
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.
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. [](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-data-classes.png)
Destructuring Declarations is a smart way to assign multiple values to variables from data stored in objects/arrays. [](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.
[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. [](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. [](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2018/04/kotlin-interview-questions-infix-notations.png)
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.
**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.
Source:
https://www.digitalocean.com/community/tutorials/kotlin-interview-questions