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

13/15

Page

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

Which of the following is the correct way of declaring objects for class Book in Kotlin?

Select an option to see the answer and solution.

Which of the following listed view hierarchies is not valid?

Select an option to see the answer and solution.

What is the correct way to create an ArrayList in Kotlin?

Select an option to see the answer and solution.

Which is a valid function declaration in Kotlin?

Select an option to see the answer and solution.

What is the correct way to declare a variable of integer type in Kotlin?

Select an option to see the answer and solution.

What is the correct syntax to convert the String "42" to a Long in Kotlin?

Select an option to see the answer and solution.

What does this code do?
foo()()

Select an option to see the answer and solution.

What value is printed by println()?
val set = setOf("apple", "pear", "orange", "apple")
println(set.count())

Select an option to see the answer and solution.

How can you retrieve the value of the property codeName without referring to it by name or destructuring?
data class Project(var codeName: String, var version: String)
fun main(){
  val proj = Project("Chilli Pepper", "2.1.0")
}

Select an option to see the answer and solution.

You would like to print each score on its own line with its cardinal position. Without using var or val, which method allows iteration with both the value and its position?
fun main() {
  val highScores = listOf(4000, 2000, 10200, 12000, 9030)
}

Select an option to see the answer and solution.

You pass an integer to a function expecting type Any. It works without issue. Why is a primitive integer able to work with a function that expects an object?
fun showHashCode(obj: Any){
  println("${obj.hasCode()}")
}
fun main() {
  showHashCode(1)
}

Select an option to see the answer and solution.

Kotlin will not compile this code snippet. What is wrong?
class Employee
class Manager : Employee()

Select an option to see the answer and solution.

Which line of code is a shorter, more idiomatic version of the displayed snippet?
val len: Int = if (x != null) x.length else -1

Select an option to see the answer and solution.

How do you fill in the blank below to display all of the even numbers from 1 to 10 with least amount of code?
for (_____) {
  println("There are $count butterflies.")
}

Select an option to see the answer and solution.

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?
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"
  }
}

Select an option to see the answer and solution.

Your application has an add function. How could you use its invoke methods and display the results?
fun add(a: Int, b: Int): Int {
  return a + b
}

Select an option to see the answer and solution.

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?
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.last}")
}

Select an option to see the answer and solution.

What does the following code print?
val a : String? = null
val b: String = "Hello World"
println(a==b)

Select an option to see the answer and solution.

What is the equivalent of the following Java code in Kotlin?
int num = 10;

Select an option to see the answer and solution.

What is the output of the following code?
fun main() {
    val x = 5
    val y = 10
    println(x + y)
}

Select an option to see the answer and solution.