programming
beginner
10 sample questions
Kotlin MCQ Practice Test
A concise, safe language fully interoperable with Java, widely used for Android development.
Q1. What is the purpose of the 'lateinit' keyword in Kotlin?
-
A. To declare a variable that must be initialized before it is used
-
B. To declare a variable that can be initialized later, but not in the declaration site ✓
-
C. To declare a variable that is guaranteed to be non-null
-
D. To declare a variable that is guaranteed to be null
Explanation: The 'lateinit' keyword in Kotlin is used to declare a variable that can be initialized later, but not in the declaration site. This means that the variable must be initialized before it is used, but it can be initialized in any of the init blocks or in a companion object.
Q2. What is the purpose of the 'runBlocking' coroutine builder in Kotlin?
-
A. To execute a coroutine in a separate thread
-
B. To execute a coroutine in the same thread as the caller ✓
-
C. To execute a coroutine in a new thread pool
-
D. To execute a coroutine with a specific context
Explanation: The 'runBlocking' coroutine builder is used to execute a coroutine in the same thread as the caller. This is useful for creating coroutines that wait for the completion of other coroutines.
Q3. In Kotlin, what is the purpose of the "lateinit" keyword when applied to a property?
-
A. It indicates that the property can be initialized only in the constructor.
-
B. It allows the property to be initialized lazily, on first access. ✓
-
C. It enables the property to be reinitialized after it has been initialized once.
-
D. It specifies that the property can be initialized with a default value.
Explanation: The "lateinit" keyword in Kotlin allows a property to be initialized lazily, which means it can be initialized only when it is first accessed. This is useful when the initialization of a property depends on some external factor, such as a network request or a database query.
Q4. What is the purpose of `::class` in Kotlin?
-
A. To obtain the runtime type of a class or object. ✓
-
B. To get the name of a variable at runtime.
-
C. To get the return type of a function at runtime.
-
D. To get the number of parameters of a function at runtime.
Explanation: The `::class` property in Kotlin is used to access the `KClass` instance representing the class or object at runtime. For example, `Int::class` returns the `KClass` instance for the `Int` type. It can be used to get the type of a variable, class, or object.
Q5. What is the purpose of the "let" expression in Kotlin?
-
A. \u201cLet\u201d is used to declare a variable in a scope. ✓
-
B. \u201cLet\u201d is used to declare a variable in an extension function.
-
C. \u201cLet\u201d is used to declare a variable in a loop.
-
D. \u201cLet\u201d is used to declare a variable in a function.
Explanation: The \u201clet\u201d expression in Kotlin is used to declare a variable in a scope, which can be a function, a loop, or an if statement. This allows the variable to be used within that scope only.
Q6. In Kotlin, what is the purpose of the `[]` operator?
-
A. The `[]` operator is used for array indexing. ✓
-
B. The `[]` operator is used to define a lambda expression.
-
C. The `[]` operator is used for type declaration.
-
D. The `[]` operator is used for function call.
Explanation: In Kotlin, the `[]` operator is primarily used for accessing elements within arrays and lists using their index. For example, `myArray[0]` accesses the first element of the array.
Q7. In Kotlin, what is the purpose of the \\ operator in a string literal?
-
A. To create a multiline string
-
B. To escape special characters in a string ✓
-
C. To denote a raw string
-
D. To create a string with a default value
Explanation: The \\ operator in Kotlin is used to escape special characters in a string literal, allowing you to include characters that would otherwise have a special meaning in the string.
Q8. What is the purpose of the "lateinit" keyword in Kotlin?
-
A. It is used to declare a nullable variable
-
B. It is used to declare a variable that can be initialized later ✓
-
C. It is used to declare a variable that can be reassigned
-
D. It is used to declare a constant variable
Explanation: In Kotlin, the "lateinit" keyword is used to declare a variable that can be initialized later. This keyword is used in scenarios where a variable is declared but its initialization is delayed until a later point in the code.
Q9. What is the purpose of the `!in` operator in Kotlin?
-
A. Used for explicit type casting
-
B. Used to check if a value is not present in a range or collection ✓
-
C. Used for operator overloading
-
D. Used for extension functions
Explanation: The `!in` operator in Kotlin is used to check if a value is NOT a member of a given range or collection. For example, `x !in 1..10` would evaluate to true if x is not between 1 and 10 (inclusive).
Q10. What is the purpose of the 'let' function in Kotlin?
-
A. 'let' is used to declare variables.
-
B. 'let' is used to create type aliases.
-
C. 'let' is used to execute a block of code within the context of an object. ✓
-
D. 'let' is used for function declarations.
Explanation: The 'let' function in Kotlin is a scoping function. It takes a lambda expression as its argument and executes that lambda within the context of the object it's called on. Inside the lambda, the object is available as 'it'.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free