51.
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)
}

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

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

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

55.
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"
  }
}

56.
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
}

57.
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.first} ${user.last}")
}

Read More Section(Kotlin Program)

Each Section contains maximum 100 MCQs question on Kotlin Program. To get more questions visit other sections.