Kotlin Test 1 — Questions and Answers
Question 1: What are the default classes in Kotlin?
- final (Correct answer)
- public
- abstract
- sealed
Correct answer: final
Explanation: <br> equals(), hashCode(), and toString() are the three methods in Any (). As a result, all Kotlin classes include these methods. Kotlin classes are final by default, which means they can't be passed down. The open keyword can be used to make a class inheritable:
Question 2: In Kotlin, which of the following options is used to handle null exceptions?
- Sealed Class
- Range
- Lambda function
- Elvis Operator (Correct answer)
Correct answer: Elvis Operator
Explanation: <br> In Kotlin, the Elvis operator is used to remove null pointer exceptions. It can be used to check if a variable is null or not, and if it is, it allows us to utilize a single default value.
Question 3: Kotlin was developed by who?
- JetBrains (Correct answer)
- Adobe
- Microsoft
Correct answer: JetBrains
Explanation: <br> JetBrains s.r.o. is a Czech software development business that creates project management and software development solutions.
Question 4: Kotlin was created under the _______ license.
- Apache 2.0 (Correct answer)
- Apache 1.1
- Apache 1.0
- None of the above
Correct answer: Apache 2.0
Explanation: <br> The JetBrains team invented Kotlin. The language was initially launched in February 2016 after a project began in 2010 to develop the language. The Apache 2.0 license was used to produce Kotlin.
Question 5: What is the definition of an immutable variable?
- A variable used for string interpolation
- A variable that cannot change, read-only (Correct answer)
- A variable that can be changed
- None of the above
Correct answer: A variable that cannot change, read-only
Explanation: <br> A variable is immutable in programming when its value cannot change after it is created. When you manipulate a string, you are already using immutable variables without realizing it.
Question 6: In Kotlin, how do you get the length of a string?
- length(str)
- str.lengthOf
- str.length (Correct answer)
- None of the above
Correct answer: str.length
Explanation: <br> In Kotlin, the String.length property can be used to determine the length of a string. The length property of a string returns the total number of characters in the string. You may need to determine the length of a string in situations such as string validation, checking if a string is empty, checking if a string is longer than it should be, and so on.
Question 7: In Kotlin, there are two types of constructors
- Parameterized & constant Constructor
- Primary & Secondary constructor (Correct answer)
- Default & No-arg constructor
- None of the above
Correct answer: Primary & Secondary constructor
Explanation: <br> There are two constructors in Kotlin: primary constructor and secondary constructor. Primary constructor is a straightforward approach to initialize a class, while secondary constructor allows you to include additional initialization logic.
Question 8: A Kotlin application's entry point is the
- system function
- main function (Correct answer)
- access function
- user function
Correct answer: main function
Explanation: <br> Both procedural and object-oriented programming are supported in Kotlin. If you've worked with procedural languages before, you're probably aware that main () is the program's entry point. In the same way, the main () function (or method) in a Kotlin file represents the starting point for a Kotlin program.
Question 9: Is the static keyword available in Kotlin?
- No (Correct answer)
- Yes
Correct answer: No
Explanation: <br> In contrast to Java, Kotlin does not contain a static keyword. However, this does not preclude us from writing Kotlin methods that perform the same functions as static Java methods.
What are the default classes in Kotlin?