Kotlin 100 Questiom
Kotlin 100 Questiom
Answer: The basic data types in Kotlin are Byte, Short, Int, Long, Float,
Double, Boolean, Char,
5. What is the syntax for creating a range of values in Kotlin?
Example:
val range =
1..10
Answer: “val” is used to declare a variable that is immutable (i.e., its value
cannot be changed once it is assigned), while “var” is used to declare a
variable that is mutable.
Example:
var nullableString: String? =
null
Answer: A data class in Kotlin is a class that is designed to hold data and
has some built-in functionality, such as toString(), equals(), and
hashCode().
Answer: The Elvis operator (?:) in Kotlin is used to provide a default value
when a nullable expression is null.
Example:
Interoperability Yes, compatible with Java Yes, fully compatible with itself
Community Support Rapidly growing community Mature and extensive community support
Answer: A sealed class in Kotlin is a class that can have a fixed set of
subclasses. It is often used to represent a restricted hierarchy of classes.
Kotlin shares many similarities with the Java programming language. Like
Java, Kotlin code is compiled into Java bytecode and executed by the Java
Virtual Machine (JVM). When a Kotlin file, such as Main.kt, is compiled, it
generates a class and bytecode file named MainKt.class, which is then
executed by the JVM at runtime.
Answer: There are two types of constructors available in Kotlin, they are:
Primary constructor
Secondary constructor
21. What is the syntax for creating a class with a primary
constructor in Kotlin?
Example:
Answer: The safe call operator (?.) in Kotlin is used to avoid null pointer
exceptions. It allows you to call a method or access a property on an object
only if the object is not null.
Answer: The non-null assertion operator (!!) in Kotlin is used to tell the
compiler that a variable is not null, even if it is not explicitly declared as
such. It should be used with caution, as it can lead to NullPointerExceptions
if the variable is actually null.
Example:
28. What are some features which are available in Kotlin but not in
Java?
Answer: Below are some Kotlin features that are not available in Java:
Null Safety
Operator Overloading
Coroutines
Range expressions
Smart casts
Companion Objects
29. How can we convert a Kotlin source file to a Java source file?
Answer: To convert a Kotlin source file to a Java source file, follow these
steps:
Example:
funCopy code
for (i in 1..count) { println(message) } }“`
In this example, a function named `printMessage` is defined with a default
value of 1 for the `count` parameter.
Procedural Programming
Object-Oriented Programming
Answer: An open class in Kotlin can be inherited from and its methods can
be overridden by subclasses, while a final class cannot be inherited from or
overridden.
35. What is the difference between a public and an internal visibility
modifier in Kotlin?
Answer: A public visibility modifier in Kotlin means that the class or function
can be accessed from anywhere in the code, while an internal visibility
modifier means that the class or function can only be accessed within the
same module.
Example:
In this example, an enum class named `Color` is defined with three possible
values: `RED`, `GREEN`, and `BLUE`.
40. What are the names of some extension methods that Kotlin
provides to java.io.File?
Example:
“`val PI =
3.14159“`
45. What is the difference between an open class and a final class in
Kotlin?
An open class in Kotlin is a class that can be subclassed, while a final class
is a class that cannot be subclassed.
By default, classes in Kotlin are final and must be explicitly marked as
open to allow subclassing.
Example:
“`fun String.printWithExclamation()
{ println(“$this!”) }“`
Example:
Answer: A nullable type in Kotlin is a type that can have a null value, while
a non-null type is a type that cannot have a null value.
Answer: The Safe Call operator in Kotlin is used to safely call a method or
access a property of an object that may be null. It is represented by the ?.
symbol.
Answer: A sealed class in Kotlin is a class that can have a limited number of
subclasses, while an abstract class is a class that cannot be instantiated and
must be subclassed.
70. What is the difference between a private constructor and a
public constructor in Kotlin?
72. What is the difference between the rangeTo operator (..) and the
downTo operator in Kotlin?
The rangeTo operator (..) in Kotlin is used to create a range of values from
a starting value to an ending value.
The downTo operator in Kotlin is used to create a range of values from a
starting value to an ending value in descending order.
73. What is the difference between a sealed class and an enum class
in Kotlin?
A sealed class in Kotlin is a class that restricts the types of its subclasses
and can be used for exhaustive when expressions.
An enum class in Kotlin is a class that represents a set of values that are
named constants.
74. What is the difference between a data class and a regular class
in Kotlin?
Answer: A data class in Kotlin is a class that is designed to hold data, while
a regular class can hold any type of functionality.
89. What is the difference between a safe call operator and a non-
null assertion operator in Kotlin?
Answer: A safe call operator in Kotlin (?.) returns null if the object is null,
while a non-null assertion operator (!!) throws an exception if the object is
null.
Answer: The “init” block is a login block that is executed in the primary
constructor to initialize the object. If needed, it can be overridden in the
secondary constructor, but it will only be executed after the primary
constructor in a chain form.
Initializer blocks
Properties
Open declarations
Nested classes
Inner classes
Answer: Kotlin does not support macros as the language’s developers have
found it challenging to incorporate them into the language.
IB Security Assistant
CISF Constable Mock Tests
UP Police Constable
UP Police Fireman
SSC GD Constable
SSC Stenographer
LT Mock Tests
IT Mock Tests
NABARD DA Prelims
Jobs by Qualification
IT Jobs by Qualification
B.Tech
BE
B.Sc
B.Com
BCA
M.Tech
ME
M.Sc
MCA
M.Com
Govt Jobs by Qualification
10th Pass
Diploma
12th Pass
Engineering
All Graduation
ITI
MS/MD
PG Diploma
Nursing
All PG
Freshersnow.com is one of the best job sites in India. On this website you
can find list of jobs such as IT jobs, government jobs, bank jobs, railway
jobs,