kotlin⾯试_Kotlin⾯试问题
kotlin⾯试
Kotlin is the latest JVM programming language from the JetBrains. Google has made it the official language for Android Development along with Java. Developers say it addresses the issues faced in Java programming. I have written a lot of Kotlin tutorials and here I am providing important kotlin interview questions.
Kotlin是JetBrains中最新的JVM编程语⾔。 Google使其与Java⼀起成为Android开发的官⽅语⾔。 开发⼈员说,它解决了Java编程⾯临的问题。 我写了很多Kotlin教程,在这⾥我提供了重要的Kotlin⾯试问题。
Kotlin⾯试问题 (Kotlin Interview Questions)
Here I am providing Kotlin Interview Questions and Answers that will help you in your Kotlin Interviews. These Kotlin interview questions are good for beginners as well as experienced programmers. There are coding questions too to brush up your coding skills.
在这⾥,我提供了Kotlin⾯试问答,可以帮助您进⾏Kotlin⾯试。 这些Kotlin⾯试问题对初学者和经验丰
富的程序员都⾮常有⽤。 还有⼀些编码问题可以提⾼您的编码技能。
1. Kotlin的⽬标平台是什么? Kotlin-Java如何互操作? (What’s the Target Platform of Kotlin? How is Kotlin-
Java interoperability possible?)
Java Virtual Machine(JVM) is the Target Platform of Kotlin. Kotlin is 100% interoperable with Java since both, on compilation produce bytecode. Hence Kotlin code can be called from Java and vice-versa.
Java虚拟机(JVM)是K otlin的⽬标平台。 Kotlin可与Java 100%互操作,因为两者在编译时都会产⽣字节码。 因此,可以从Java 调⽤Kotlin代码,反之亦然。
2. 您如何在Kotlin中声明变量?声明与Java声明有何不同? (How do you declare variables in Kotlin? How does
the declaration differ from the Java counterpart?)
There are two major differences between Java and Kotlin variable declaration:
1. The type of declaration
In Java the declaration look like this:
String s = "Java String";
int x = 10;
In Kotlin the declaration looks like:
In Kotlin, the declaration begins with a val and a var followed by the optional type. Kotlin can automatically detect the type using type inference.
2. Default value
The following is possible in Java:
String s:
The following variable declaration in Kotlin is not valid.
Java和Kotlin变量声明之间有两个主要区别:
1. 申报类型
在Java中,声明如下所⽰:
String s = "Java String";
int x = 10;
在Kotlin中,声明如下所⽰:
在Kotlin中,声明以val和var开头,后跟可选类型。 Kotlin可以使⽤类型推断⾃动检测类型。
2. 默认值
Java中可能有以下内容:
String s:
Kotlin中的以下变量声明⽆效。
3. val和var声明有什么区别?如何将字符串转换为Int? (What’s the difference between val and var declaration?
How to convert a String to an Int?)
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.
val变量不能更改。 它们就像Java中的最终修饰符。 可以重新分配var 。 重新分配的值必须具有相同的数据类型。
我们使⽤toInt()⽅法将String转换为Int。
4. 什么是Kotlin中的Null安全性和Nullable类型?猫王算⼦是什么? (What’s Null Safety and Nullable Types in
Kotlin? What is the Elvis Operator?)
Kotlin puts a lot of weight behind null safety which is an approach to prevent the dreaded Null Pointer Exceptions by using nullable types which are like String?, Int?, Float? etc. These act as a wrapper type and can hold null values. A nullable value cannot be added to another nullable or basic type of value.
To retrieve the basic types we need to use safe calls that unwrap the Nullable Types. If on unwrapping, the value is null we can choose to ignore or use a default value instead. The Elvis Operator is used to safely unwrap the value from the Nullable.
It’s represented as ?: over the nullable type. The value on the right hand side would be used if the nullable type holds
a null.
var str: String? = "JournalDev"
var newStr = str?: "Default Value"
str = null
newStr = str?: "Default Value"
Kotlin在null安全性后⾯放了很多权重,这是通过使⽤像String?这样的nullable类型来防⽌可怕的Null Pointer异常的⼀种⽅法String?
, Int? , Float? 等等。它们充当包装器类型,可以容纳空值。 ⽆法将可为空的值添加到其他可为空或基本类型的值中。
要检索基本类型,我们需要使⽤安全的调⽤来拆开可空类型。 如果展开,则该值为null,我们可以选择忽略或使⽤默认值。 Elvis运算符⽤于安全地将值从Nullable中解包。
它在可为空的类型上表⽰为?: 。 如果可为空的类型为null,则将使⽤右侧的值。
var str: String? = "JournalDev"
var newStr = str?: "Default Value"
str = null
newStr = str?: "Default Value"
5. 什么是const ?它与val有何不同? (What’s a const? How does it differ from a val?)
By default val properties are set at runtime. Adding a const modifier on a val would make a compile-time constant.
A const cannot be used with a var or on its own.
A const is not applicable on a local variable.
默认情况下, val属性在运⾏时设置。 在val上添加const修饰符将使编译时常数。
const不能与var⼀起使⽤,也不能单独使⽤。
const不适⽤于局部变量。
6. Kotlin是否允许我们使⽤基本类型,例如int,float,double? (Does Kotlin allow us to use primitive types
such as int, float, double?)
At the language level, we cannot use the above-mentioned types. But the JVM bytecode that’s compiled does certainly have them.
在语⾔级别,我们不能使⽤上述类型。 但是,已编译的JVM字节码确实具有它们。
7. 每个Kotlin计划的切⼊点是什么? (What’s the entry point of every Kotlin Program?)
The main function is the entry point of every Kotlin program. In Kotlin we can choose not to write the main function inside the class. On compiling the JVM implicitly encapsulates it in a class.
The strings passed in the form of Array<String> are used to retrieve the command line arguments.
main功能是每个Kotlin程序的⼊⼝。 在Kotlin中,我们可以选择不在类内部编写main函数。 编译JVM时会将其隐式封装在⼀个类中。
以Array<String>形式传递的Array<String>⽤于检索命令⾏参数。
variable used in lambda8. 怎么样!! 不同于?。在展开可为空的值?还有其他⽅法可以安全地包装可空值吗? (How is !!different from
. in unwrapping the nullable values Is there any other way to unwrap nullable values safely)
!! is used to force unwrap the nullable type to get the value. If the value returned is a null, it would lead to a runtime crash. Hence a !! operator should be only used when you’re absolutely sure that the value won’t be null at all.
Otherwise, you’ll get the dreaded null pointer exception. On the other hand, a ?. is an Elvis Operator that does a safe call.
We can use the lambda expression let on the nullable value to unwrap safely as shown below.
Here the let expression does a safe call to unwrap the nullable type.
!! ⽤于强制拆开可为null的类型以获取值。 如果返回的值为null,则将导致运⾏时崩溃。 因此!! 仅在绝对确定该值不会为null时,才应使⽤operator。 否则,您将得到可怕的空指针异常。 另⼀⽅⾯,?。 是负责安全呼叫的猫王操作员。
我们可以使⽤lambda表达式let对空值如下图所⽰安全解开。
在这⾥,let表达式进⾏安全调⽤以解开可为空的类型。
9. 如何声明函数?为什么Kotlin函数被称为顶级函数? (How is a function declared? Why are Kotlin functions
known as top-level functions?)
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.
在以下位置定义函数的返回类型:
Kotlin中的函数可以在Kotlin⽂件的根声明。
10. Kotlin中==和===运算符有什么区别? (What’s the difference between == and === operators in Kotlin?)
== is used to compare the values are equal or not. === is used to check if the references are equal or not.
==⽤于⽐较值是否相等。 ===⽤于检查引⽤是否相等。
11. 列出Kotlin中可⽤的可见性修改器。默认的可见性修改器是什么? (List down the visibility modifiers
available in Kotlin. What’s the default visibility modifier?)
1. public
2. internal
3. protected
4. private
public is the default visibility modifier.
1. 上市
2. 内部
3. 受保护的
4. 私⼈的
public是默认的可见性修饰符。
12. 以下继承结构是否可以编译? (Does the following inheritance structure compile?)
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.
class A{
}
class B : A(){
}
不⾏ 默认情况下,类在Kotlin中是final。 要使它们成为⾮最终值,您需要添加open修饰符。
13. Kotlin中的构造函数类型是什么?它们有何不同?您如何在课堂上定义它们? (What are the types of
constructors in Kotlin? How are they different? How do you define them in your class?)
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中的构造⽅法有两种:
主要的 -这些是在类标题中定义的。 他们不能保持任何逻辑。 每个类只有⼀个主要的构造函数。
次要的 -它们在类主体中定义。 他们必须委托给主要构造函数(如果存在)。 他们可以保持逻辑。 ⼆级构造器可以有多个。
14. Kotlin中的init块是什么 (What’s init block in 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.
init是Kotlin中的初始化程序块。 ⼀旦实例化了主要构造函数,它就会执⾏。 如果调⽤辅助构造器,则它在主构造器中⼯作,因为它是在链中组成的。
15. 字符串插值在Kotlin中如何⼯作?解释⼀下代码⽚段? (How does string interpolation work in Kotlin?
Explain with a code snippet?)
String interpolation is used to evaluate string templates.
We use the symbol $ to add variables inside a string.
val name = "Journaldev"
val desc = "$name now has Kotlin Interview Questions too. ${name.length}"
Using {} we can compute an expression too.
字符串插值⽤于评估字符串模板。
我们使⽤符号$在字符串中添加变量。
使⽤{}我们也可以计算⼀个表达式。
16. 构造函数内部的参数类型是什么? (What’s the type of arguments inside a constructor?)
By default, the constructor arguments are val unless explicitly set to var.
默认情况下,除⾮显式设置为var否则构造函数参数为val 。
17. 是Kotlin中的新关键字吗?您将如何在Kotlin中实例化类对象? (Is new a keyword in Kotlin? How would you
instantiate a class object in 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()
不⾏ 与Java不同,在Kotlin中,new不是关键字。
我们可以通过以下⽅式实例化⼀个类:
18. Kotlin中的switch表达式等效吗?与开关有何不同? (What is the equivalent of switch expression in Kotlin?
How does it differ from switch?)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论