Kotlin Quiz
Fast practice, instant feedback. Timer auto-submits when time’s up.
Avg score: 18% Most missed: “Which statement declares a variable mileage whose value never changes and is inf…”

Kotlin MCQs For LinkedIn Skill Assessments.

Kotlin Quiz
Time left 00:00
25 Questions

1. Which line of code shows how to display a nullable string's length and shows 0 instead of null?
2. Which line of code shows how to call a Fibonacci function, bypass the first three elements, grab the next six, and sort the elements in descending order?
3. You are attempting to assign an integer variable to a long variable, but Kotlin compiler flags it as an error. Why?
4. Given the code below, how can you write the line this.moveTo('LA') more concisely?
kotlin
data class Student (val name: String, var location: String) {
fun moveTo (newLoc: String) { location = newLoc }
}
fun main() {
Student ('Snow', 'Cologne').run {
this.moveTo ('LA')
}
5. You have a when expression for all of the subclasses of the class Attribute. To satisfy the when, you must include an else clause. Unfortunately, whenever a new subclass is added, it returns unknown. You would prefer to remove the else clause so the compiler generates an error for unknown subtypes. What is one simple thing you can do to achieve this?
kotlin
open class Attribute
class Href: Attribute()
class Src: Attribute()
class Alt: Attribute()
fun getAttribute(attribute: Attribute) : String {
return when (attribute) {
is Href -> 'href'
is Alt -> 'alt'
is Src -> 'src'
else -> 'unknown'
}
}
6. What is the difference between the declarations of COLOR and SIZE?
kotlin
class Record{
companion object {
const val COLOR = 'Red'
val SIZE = 'Large'
}
}
7. You have started a long-running coroutine whose job you have assigned to a variable named 'task'. If the need arose, how could you abort the coroutine?
kotlin
val task = launch {
// long running job
}
8. What is the preferred way to create an immutable variable of type long?
9. What are the two ways to make a coroutine's computation code cancellable?
10. This function generates Fibonacci sequence. Which function is missing?
kotlin
fun fibonacci() = sequence {
var params = Pair(0, 1)
while (true) {
___(params.first)
params = Pair(params.second, params.first + params.second)
}
}
11. Your code need to try casting an object. If the cast is not possible, you do not want an exception generated, instead you want null to be assigned. Which operator can safely cast a value?
12. What is the entry point for a Kotlin application?
13. What value is printed by println()?
kotlin
val set = setOf('apple', 'pear', 'orange', 'apple')
println(set.count())
14. Kotlin interfaces and abstract classes are very similar. What is one thing abstract class can do that interfaces cannot?
15. The code below is expected to display the numbers from 1 to 10, but it does not. Why?
kotlin
val seq = sequence { yieldAll(1..20) }
.filter { it < 11 }
println(seq)
16. Kotlin will not compile this code snippet. What is wrong?
kotlin
class Employee
class Manager : Employee()
17. The code snippet compile and runs without issue, but does not wait for the coroutine to show the 'there' message. Which line of code will cause the code to wait for the coroutine to finish before exiting?
kotlin
fun main() = runBlocking {
val task = GlobalScope.launch {
delay(1000L)
println('there')
}
println('Hello,')
}
18. The code snippet below translates a database user to a model user. Because their names are both User, you must use their fully qualified names, which is cumbersome. You do not have access to either of the imported classes' source code. How can you shorten the type names?
kotlin
import com.tekadept.app.model.User
import com.tekadept.app.database.User
class UserService{
fun translateUser(user: com.tekadept.app.database.User): User =
com.tekadept.app.model.User('${user.first} ${user.last}')
}
19. You have created a class that should be visible only to the other code in its module. Which modifier do you use?
20. What is the output of this code?
kotlin
val quote = 'The eagle has landed.'
println('The length of the quote is $quote.length')
21. All classes in Kotlin inherit from which superclass?
22. Which line converts the binaryStr, whish contain only 0s and 1s, to an integer representing its decimal value?
kotlin
val binaryStr = '00001111'
23. You have an enum class Signal that represents the state of a network connection. You want to print the position number of the SENDING enum. Which line of code does that?
java
enum class Signal { OPEN, CLOSED, SENDING }
24. Which is the proper way to declare a singleton named DatabaseManager?
25. Your application has an 'add' function. How could you use its 'invoke' methods and display the results?
kotlin
fun add(a: Int, b: Int): Int {
return a + b
}

⚡ Recently practiced quizzes in this topic
Live quiz activity