Vidyalelo
Computer Science · all questions

Kotlin Programming MCQ for Competitive Exams, Interviews, and Certifications
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

292

Questions

15/15

Page

Pick an option on a question to see the right answer and solution.

You have written a snippet of code to display the results of the roll of a six-sided die. When the die displays from 3 to 6 inclusive, you want to display a special message. Using a Kotlin range, what code should you add?
when (die) {
  1 -> println("die is 1")
  2 -> println("die is 2")
  ___ -> printlin("die is between 3 and 6")
  else -> printlin("dies is unknown")
}

Select an option to see the answer and solution.

Why does not this code snippet compile?
class Cat (name: String) {
  fun greet() { println("Hello ${this.name}") }
}

fun main() {
  val thunderCat = Cat("ThunderCat")
  thunderCat.greet()
}

Select an option to see the answer and solution.

What is the output of the following code?
fun main() {
    val num = 15
    when (num) {
        10 -> println("Ten")
        15 -> println("Fifteen")
        20 -> println("Twenty")
        else -> println("Other")
    }
}

Select an option to see the answer and solution.

What is the output of the following code?
fun main() {
    val list = listOf(1, 2, 3, 4, 5)
    for (num in list) {
        println(num)
    }
}

Select an option to see the answer and solution.

Why doesn't this code compile?
val addend = 1
infix fun Int.add(added: Int=1) = this + addend
fun main(){
  val msg = "Hello"
  println( msg shouldMatch "Hello")    
  println( 10 multiply 5 + 2)
  println( 10 add 5)
}

Select an option to see the answer and solution.

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?
val task = launch {
  // long running job
}

Select an option to see the answer and solution.

In order to subclass the Person class, what is one thing you must do?
abstract class Person(val name: String) {
  abstract fun displayJob(description: String)
}

Select an option to see the answer and solution.

What is to in the example below:
val test = 33 to 42

Select an option to see the answer and solution.

You have a function simple() that is called frequently in your code. You place the inline prefix on the function. What effect does it have on the code?
inline fun simple(x: Int): Int{
  return x * x
}

fun main() {
  for(count in 1..1000) {
    simple(count)
  }
}

Select an option to see the answer and solution.

This code snippet compiles without error, but never prints the results when executed. What could be wrong?
Val result = generateSequence(1) { it + 1 }.toList(); Println(result)

Select an option to see the answer and solution.

You are creating a Kotlin unit test library. What else should you add to make the following code compile without error?
fun String.shouldEqual(value: String) = this == value
fun main(){
  val msg = "test message"
  println(msg shouldEqual "test message")
}

Select an option to see the answer and solution.

This function generates Fibonacci sequence. Which function is missing?
fun fibonacci() = sequence {
  var params = Pair(0, 1)
  while (true) {
    ___(params.first)
    params = Pair(params.second, params.first + params.second)
  }
}

Select an option to see the answer and solution.