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

14/15

Page

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

What is the type of arr in the following code?
val arr = arrayof(1, 2, 3)

Select an option to see the answer and solution.

From the Supervisor subclass, how do you call the Employee class's display() method?
open class Employee(){
  open fun display() = println("Employee display()")
}
class Supervisor : Employee() {
  override fun display() {
    println("Supervisor display()")
  }
}

Select an option to see the answer and solution.

The code below shows a typical way to show both index and value in many languages, including Kotlin. Which line of code shows a way to get both index and value more idiomatically?
var ndx = 0;
for (value in 1..5){
  println("value")
  ndx++
}

Select an option to see the answer and solution.

This code does not print any output to the console. What is wrong?
firstName?.let {
  println("Greeting $firstname!")
}

Select an option to see the answer and solution.

What is the equivalent of the following Java expression in Kotlin?
int x = a ? b : c 

Select an option to see the answer and solution.

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?
enum class Signal { OPEN, CLOSED, SENDING }

Select an option to see the answer and solution.

What is the output of the following code?
val list : List<Int> = listof(1, 2, 3)
list.add(4)
print(list)

Select an option to see the answer and solution.

What does the following code print?
val listA = mutableListOf(1, 2, 3)
val listB = listA.add(4)
print(listB)

Select an option to see the answer and solution.

What is the difference between the declarations of COLOR and SIZE?
class Record{
  companion object {
    const val COLOR = "Red"
    val SIZE = "Large"
  }
}

Select an option to see the answer and solution.

In the file main.kt, you ae filtering a list of integers and want to use an already existing function, removeBadValues. What is the proper way to invoke the function from filter in the line below?
val list2 = (80..100).toList().filter(_____)

Select an option to see the answer and solution.

What is the difference between a and b?
var a: String? = "KotlinProgramming"
var b: String = 'KotlinProgramming"

Select an option to see the answer and solution.

Which is true for the following simple class declaration?
class Person (val name: String)

Select an option to see the answer and solution.

The code below compiled and executed without issue before the addition of the line declaring errorStatus. Why does this line break the code?
sealed class Status(){
  object Error : Status()
  class Success : Status()
}
fun main(){
  var successStatus = Status.Success()
  var errorStatus = Status.Error()
}

Select an option to see the answer and solution.

The code below is expected to display the numbers from 1 to 10, but it does not. Why?
val seq = sequence { yieldAll(1..20) }
  .filter { it < 11 }
  println(seq)

Select an option to see the answer and solution.

You have an unordered list of high scores. Which is the simple method to sort the highScores in descending order?
fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030)}

Select an option to see the answer and solution.

What three methods does this class have?
class Person

Select an option to see the answer and solution.

In this code snippet, why does the compiler not allow the value of y to change?
for(y in 1..100) y+=2

Select an option to see the answer and solution.

Both const and @JvmField create constants. What can const do that @JvmField cannot?
class Detail {
  companion object {
    const val COLOR = "Blue"
    @JvmField val SIZE = "Really Big"
  }
}

Select an option to see the answer and solution.

You have two arrays, a and b. Which line combines a and b as a list containing the contents of both?
val a = arrayOf(1, 2, 3) val b = arrayOf(100, 200, 3000)

Select an option to see the answer and solution.

When the Airplane class is instantiated, it displays Aircraft = null, not Aircraft = C130 why?
abstract class Aircraft {
  init { println("Aircraft = ${getName()}") }
  abstract fun getName(): String
}
class Airplane(private val name: String) : Aircraft() {
  override fun getName(): String = name
}

Select an option to see the answer and solution.