Kotlin

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?

kotlin fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030) }

1.

`.withIndex()`

2.

`.forEachIndexed()`

3.

`.forEach()`

4.

`.forIndexes()`

Q 1 / 78

Kotlin

When the **Airplane** class is instantiated, it displays **Aircraft = null**, not **Aircraft = C130** why?

kotlin abstract class Aircraft { init { println("Aircraft = ${getName()}") } abstract fun getName(): String } class Airplane(private val name: String) : Aircraft() { override fun getName(): String = name }

1.

Classes are initialized in the same order they are in the file, therefore, Aircraft should appear after Airplane

2.

The code needs to pass the parameter to the base class's primary constructor. Since it does not, it receives a null

3.

Abstract function always returns null

4.

A superclass is initialized before its subclass. Therefore, name has not been set before it is rendered

Q 2 / 78

Kotlin

Kotlin interfaces ad abstract classes are very similar. What is one thing abstract class can do that interfaces cannot?

1.

Only abstract classes are inheritable by subclasses

2.

Only abstract classes can inherit from multiple superclasses

3.

Only abstract classes can have abstract methods

4.

Only abstract classes can store state

Q 3 / 78

Kotlin

Inside an extension function, what is the name of the variable that corresponds to the receiver object

1.

The variable is named **it**

2.

The variable is named **this**

3.

The variable is named **receiver**

4.

The variable is named **default**

Q 4 / 78

Kotlin

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 }

1.

`println(add(5,10).invoke())`

2.

`println(::add.invoke(5, 10))`

3.

`println(::add.invoke{5, 10})`

4.

`println(add.invoke(5,10))`

Q 5 / 78

Kotlin

What is the entry point for a Kotlin application?

1.

`fun static main(){}`

2.

`fun main(){}`

3.

`fun Main(){}`

4.

`public static void main(){}`

Q 6 / 78

Kotlin

You are writing a console app in Kotlin that processes test entered by the user. If the user enters an empty string, the program exits. Which kind of loop would work best for this app? Keep in mind that the loop is entered at least once

1.

a do..while loop

2.

a for loop

3.

a while loop

4.

a forEach loop

Q 7 / 78

Kotlin

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?

kotlin fun showHashCode(obj: Any){ println("${obj.hasCode()}") } fun main() { showHashCode(1) }

1.

While the code runs, it does not produce correct results

2.

The integer is always a class

3.

The compiler runs an implicit `.toClass()` method on the integer

4.

The integer is autoboxed to a Kotlin Int class

Q 8 / 78

Kotlin

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 }

1.

`task.join()`

2.

`task.abort()`

3.

`job.stop()`

4.

`task.cancel()`

Q 9 / 78

Kotlin

You are attempting to assign an integer variable to a long variable, but Kotlin compiler flags it as an error. Why?

1.

You must wrap all implicit conversion in a try/catch block

2.

You can only assign `Long` to an `Int`, not the other way around

3.

There is no implicit conversion from `Int` to `Long`

4.

All integers in Kotlin are of type `Long`

Q 10 / 78

Kotlin

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?

kotlin when (die) { 1 -> println("die is 1") 2 -> println("die is 2") ___ -> printlin("die is between 3 and 6") else -> printlin("dies is unknown") }

1.

`3,4,5,6`

2.

`in 3..6`

3.

`3 : 6`

4.

`{3,4,5,6}`

Q 11 / 78

Kotlin

The function **typeChecker** receiver a parameter **obj** of type **Any**. Based upon the type of **obj**, it prints different messages for Int, String, Double, and Float types; if not any of the mentioned types, it prints "unknown type". What operator allows you to determine the type of an object?

1.

`instanceof`

2.

`is`

3.

`typeof`

4.

`as`

Q 12 / 78

Kotlin

This code does not print any output to the console. What is wrong?

kotlin firstName?.let { println("Greeting $firstname!") }

1.

A null pointer exception is thrown

2.

`firstName` is equal to `null`

3.

`firstName` is equal to an empty string

4.

`firstName` is equal to Boolean `false`

Q 13 / 78

Kotlin

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?

kotlin inline fun simple(x: Int): Int{ return x * x } fun main() { for(count in 1..1000) { simple(count) } }

1.

The code will give a stack overflow error

2.

The compiler warns of insignificant performance impact

3.

The compiler warns of significant memory usage

4.

The code is significantly faster

Q 14 / 78

Kotlin

undefined

kotlin for (_____) { println("There are $count butterflies.") }

1.

`count in 1..10`

2.

`count in 2..10 step 2`

3.

`count in 1..10 % 2`

4.

`var count=2; count <= 10; count+=2`

Q 15 / 78

Kotlin

What value is printed by println()?

kotlin val set = setOf("apple", "pear", "orange", "apple") println(set.count())

1.

3

2.

4

3.

1

4.

5

Q 16 / 78

Kotlin

Which line of code shows how to display a nullable string's length and shows 0 instead of null?

1.

`println(b!!.length ?: 0)`

2.

`println(b?.length ?: 0)`

3.

`println(b?.length ?? 0)`

4.

`println(b == null? 0: b.length)`

Q 17 / 78

Kotlin

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?

kotlin val list2 = (80..100).toList().filter(_____)

1.

`::removeBadValues`

2.

`GlobalScope.removeBadValues()`

3.

`Mainkt.removeBadValues`

4.

`removeBadValues`

Q 18 / 78

Kotlin

Which code snippet correctly shows a for loop using a range to display "1 2 3 4 5 6"?

1.

`for(z in 1..7) println("$z ")`

2.

`for(z in 1..6) print("$z ")`

3.

`for(z in 1 to 6) print("$z ")`

4.

`for(z in 1..7) print("$z ")`

Q 19 / 78

Kotlin

You are upgrading a Java class to Kotlin. What should you use to replace the Java class's static fields?

1.

an anonymous object

2.

a static property

3.

a companion object

4.

a backing field

Q 20 / 78

Kotlin

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?

1.

`as?`

2.

`??`

3.

`is`

4.

`as`

Q 21 / 78

Kotlin

Kotlin will not compile this code snippet. What is wrong?

kotlin class Employee class Manager : Employee()

1.

In order to inherit from a class, it must be marked **open**

2.

In order to inherit from a class, it must be marked **public**

3.

In order to inherit from a class, it must be marked **sealed**

4.

In order to inherit from a class, it must be marked **override**

Q 22 / 78

Kotlin

Which function changes the value of the element at the current iterator location?

1.

`change()`

2.

`modify()`

3.

`set()`

4.

`assign()`

Q 23 / 78

Kotlin

From the Supervisor subclass, how do you call the Employee class's display() method?

kotlin open class Employee(){ open fun display() = println("Employee display()") } class Supervisor : Employee() { override fun display() { println("Supervisor display()") } }

1.

`Employee.display() `

2.

`::display()`

3.

`super.display()`

4.

`override.display()`

Q 24 / 78

Kotlin

The code below compiled and executed without issue before the addition of the line declaring errorStatus. Why does this line break the code?

kotlin sealed class Status(){ object Error : Status() class Success : Status() } fun main(){ var successStatus = Status.Success() var errorStatus = Status.Error() }

1.

`StatusError` is an object, not a class and cannot be instantiated

2.

Only one instance of the class `Status` can be instantiated at a time

3.

`Status.Error` must be declared as an immutable type

4.

`Status.Error` is pribate to class and cannot be declared externally

Q 25 / 78

Kotlin

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)

1.

You cannot assign a sequence to a variable

2.

To produce result, a sequence must have terminal operation. In this case, it needs a `.toList()`

3.

The `.filter{ it < 11 }` should be `.filter{ it > 11 }`

4.

The `yieldAll(1..20)` should be `yieldAll(1..10)`

Q 26 / 78

Kotlin

What three methods does this class have?

kotlin class Person

1.

`equals(), hashCode(), and toString()`

2.

`equals(), toHash(), and super()`

3.

`print(), println(), and toString()`

4.

`clone(), equals(), and super()`

Q 27 / 78

Kotlin

Which is the proper way to declare a singleton named DatabaseManager?

1.

`object DatabaseManager {}`

2.

`singleton DatabaseManager {}`

3.

`static class DatabaseManager {}`

4.

`data class DatabaseManager {}`

Q 28 / 78

Kotlin

In order to subclass the Person class, what is one thing you must do?

kotlin abstract class Person(val name: String) { abstract fun displayJob(description: String) }

1.

The subclass must be marked sealed

2.

You must override the `displayJob()` method

3.

You must mark the subclass as final

4.

An abstract class cannot be extended, so you must change it to open

Q 29 / 78

Kotlin

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

1.

Use import as to change the type name

2.

Create subtypes with shorter names

3.

Create interfaces with shorter names

4.

Create extension classes with shorter names

Q 30 / 78

Kotlin

Your function is passed by a parameter obj of type Any. Which code snippet shows a way to retrieve the original type of obj, including package information?

1.

`obj.classInfo()`

2.

`obj.typeInfo()`

3.

`obj::class.simpleName`

4.

`obj::class`

Q 31 / 78

Kotlin

Which is the correct declaration of an integer array with a size of 5?

1.

`val arrs[5]: Int`

2.

`val arrs = IntArray(5)`

3.

`val arrs: Int[5]`

4.

`val arrs = Array<Int>(5)`

Q 32 / 78

Kotlin

You have created a class that should be visible only to the other code in its module. Which modifier do you use?

1.

`internal`

2.

`private`

3.

`public`

4.

`protected`

Q 33 / 78

Kotlin

Kotlin has two equality operators, == and ===. What is the difference?

1.

`==` determines if two primitive types are identical. `===` determines if two objects are identical

2.

`==` determines if two references point to the same object. `===` determines if two objects have the same value

3.

`==` determines if two objects have the same value. `===` determines if two strings have the same value

4.

`==` determines if two objects have the same value. `===` determines if two references point to the same object

Q 34 / 78

Kotlin

Which snippet correctly shows setting the variable max to whichever variable holds the greatest value, a or b, using idiomatic Kotlin?

1.

`val max3 = a.max(b)` (Extension Function is One of the idiomatic Solutions in Kotlin)

2.

`val max = a > b ? a : b`

3.

`val max = if (a > b) a else b`

4.

`if (a > b) max = a else max = b`

Q 35 / 78

Kotlin

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 }

1.

`println(Signal.SENDING.position())`

2.

`println(Signal.SENDING.hashCode())`

3.

`println(Signal.SENDING)`

4.

`println(Signal.SENDING.ordinal)`

Q 36 / 78

Kotlin

Both const and @JvmField create constants. What can const do that @JvmField cannot?

java class Detail { companion object { const val COLOR = "Blue" @JvmField val SIZE = "Really Big" } }

1.

`const` is compatible with Java, but `@JvmField` is not

2.

The compiler will inline const so it is faster and more memory efficient

3.

Virtually any type can be used with const but not `@JvmField`

4.

const can also be used with mutable types

Q 37 / 78

Kotlin

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

1.

Replace `open` with `closed`

2.

Replace `open` with `sealed`

3.

Replace `open` with `private`

4.

Replace `open` with `public`

Q 38 / 78

Kotlin

You would like to know each time a class property is updated. Which code snippet shows a built-in delegated property that can accomplish this?

1.

`Delegates.watcher()`

2.

`Delegates.observable()`

3.

`Delegates.rx()`

4.

`Delegates.observer()`

Q 39 / 78

Kotlin

Why doesn't this code compile?

kotlin 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) }

1.

infix function must be marked public

2.

In Kotlin, add is a keyword

3.

Extension functions use `it`, not `this`, as the default parameter name

4.

infix functions cannot have default values

Q 40 / 78

Kotlin

What is the correct way to initialize a nullable variable?

1.

`val name = null`

2.

`var name: String`

3.

`val name: String`

4.

`val name: String? = null`

Q 41 / 78

Kotlin

Which line of code is a shorter, more idiomatic version of the displayed snippet?

kotlin val len: Int = if (x != null) x.length else -1

1.

`val len = x?.let{x.len} else {-1}`

2.

`val len = x!!.length ?: -1`

3.

`val len:Int = (x != null)? x.length : -1`

4.

`val len = x?.length ?: -1`

Q 42 / 78

Kotlin

You are creating a Kotlin unit test library. What else should you add to make the following code compile without error?

kotlin fun String.shouldEqual(value: String) = this == value fun main(){ val msg = "test message" println(msg shouldEqual "test message") }

1.

The extension function should be marked public

2.

Add the prefix operator to the shouldMatch extension function

3.

The code is not legal in Kotlin (should be `println(msg.shouldEqual("test message"))`)

4.

Add the prefix infix to the shouldMatch extension function

Q 43 / 78

Kotlin

What is the difference between the declarations of COLOR and SIZE?

kotlin class Record{ companion object { const val COLOR = "Red" val SIZE = "Large" } }

1.

Since `COLOR` and `SIZE` are both immutable, they are identical internally

2.

Both are immutable, but the use of the keyword const makes `COLOR` slower and less space efficient than `SIZE`

3.

const makes `COLOR` faster, but not compatible with Java. Without const, `SIZE` is still compatible with Java

4.

Both are immutable, but the use of the keyword const makes `COLOR` faster and more space efficient than `SIZE`

Q 44 / 78

Kotlin

Why does not this code snippet compile?

kotlin class Cat (name: String) { fun greet() { println("Hello ${this.name}") } } fun main() { val thunderCat = Cat("ThunderCat") thunderCat.greet() }

1.

Because name is a class parameter, not a property-it is unresolved `main()`.

2.

In order to create an instance of a class, you need the keyword `new`

3.

The reference to name needs to be scoped to the class, so it should be `this.name`

4.

Classes cannot be immutable. You need to change `var` to `val`

Q 45 / 78

Kotlin

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?

kotlin var ndx = 0; for (value in 1..5){ println("$ndx - $value") ndx++ }

1.

`for( (ndx, value) in (1..20).withIndex() ){`

2.

`for( (ndx, value) in (1..20).pair() ){`

3.

`for( Pair(ndx, value) in 1..20 ){`

4.

`for( (ndx, value) in *(1..20) ){`

Q 46 / 78

Kotlin

The Kotlin .. operator can be written as which function?

1.

`a.from(b)`

2.

`a.range(b)`

3.

`a.rangeTo(b)`

4.

`a.to(b)`

Q 47 / 78

Kotlin

How can you retrieve the value of the property codeName without referring to it by name or destructuring?

kotlin data class Project(var codeName: String, var version: String) fun main(){ val proj = Project("Chilli Pepper", "2.1.0") }

1.

`proj.0`

2.

`proj[0]`

3.

`proj[1]`

4.

`proj.component1()`

Q 48 / 78

Kotlin

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

1.

`with()`

2.

`yield()`

3.

`skip()`

4.

`return()`

Q 49 / 78

Kotlin

In this code snippet, why does the compiler not allow the value of y to change?

kotlin for(y in 1..100) y+=2

1.

`y` must be declared with `var` to be mutable

2.

`y` is an implicitly immutable value

3.

`y` can change only in a while loop

4.

In order to change `y`, it must be declared outside of the loop

Q 50 / 78

Kotlin

You have created a data class, Point, that holds two properties, x and y, representing a point on a grid. You want to use the hash symbol for subtraction on the Point class, but the code as shown will not compile. How can you fix it?

kotlin data class Point(val x: Int, val y: Int) operator fun Point.plus(other: Point) = Point(x + other.x, y + other.y) operator fun Point.hash(other: Point) = Point(x - other.x, y - other.y) fun main() { val point1 = Point(10, 20) val point2 = Point(20, 30) println(point1 + point2) println(point1 # point2) }

1.

You cannot; the hash symbol is not a valid operator.

2.

You should replace the word hash with octothorpe, the actual name for the symbol.

3.

You should use `minus` instead of hash, then type alias the minus symbol. // Note: How can you fix it?

4.

You need to replace operator with the word `infix`.

Q 51 / 78

Kotlin

This code snippet compiles without error, but never prints the results when executed. What could be wrong?

kotlin val result = generateSequence(1) { it + 1 }.toList() println(result)

1.

The sequence lacks a terminal operation.

2.

The sequence is infinite and lacks an intermediate operation to make `it` finite.

3.

The expression should begin with `generateSequence(0)`.

4.

The `it` parameter should be replaced with `this`.

Q 52 / 78

Kotlin

An error is generated when you try to compile the following code. How should you change the call to printStudents to fix the error?

kotlin fun main() { val students = arrayOf("Abel", "Bill", "Cindy", "Darla") printStudents(students) } fun printStudents(vararg students: String) { for(student in students) println(student) }

1.

`printStudents(students.toList())`

2.

`printStudents(students!!)`

3.

`printStudents(*students)`

4.

`printStudents(students[])`

Q 53 / 78

Kotlin

Both y and z are immutable references pointing to fixed-size collections of the same four integers. Are there any differences?

kotlin val y = arrayOf(10, 20, 30, 40) val z = listOf(10, 20, 30, 40)

1.

You can modify the contents of the elements in `y` but not `z`.

2.

There are not any differences. `y` and `z` are a type alias of the same type.

3.

You add more elements to `z` since it is a list.

4.

You can modify the contents of the elements in `z` but not `y`.

Q 54 / 78

Kotlin

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,") }

1.

`task.complete()`

2.

`task.wait()`

3.

`task.join()`

4.

`task.cancel()`

Q 55 / 78

Kotlin

You would like to group a list of students by last name and get the total number of groups. Which line of code accomplishes this, assuming you have a list of the Student data class?

kotlin data class Student(val firstName: String, val lastName: String)

1.

`println(students.groupBy{ it.lastName }.count())`

2.

`println(students.groupBy{ it.lastName.first() }.fold().count())`

3.

`println(students.groupingBy{ it.lastName.first() }.count())`

4.

`println(students.groupingBy{ it.lastName.first() }.size())`

Q 56 / 78

Kotlin

Class BB inherits from class AA. BB uses a different method to calculate the price. As shown, the code does not compile. What changes is needed to resolve the compilation error?

kotlin open class AA() { var price: Int = 0 get() = field + 10 } class BB() : AA() { var price: Int = 0 get() = field + 20 }

1.

You need to add a lateinit modifier to `AA.price`.

2.

You simply need to add an override modifier to `BB.price`.

3.

You need to add an open modifier to `AA.price` and an override modifier to `BB.price`.

4.

You need to add a public modifier to `AA.price` and a protected modifier to `BB.price`.

Q 57 / 78

Kotlin

What is the output of this code?

kotlin val quote = "The eagle has landed." println("The length of the quote is $quote.length")

1.

The length of the quote is The eagle has landed.

2.

A compilation error is displayed.

3.

The length of the quote is `21`

4.

The length of the quote is `The eagle has landed..length`

Q 58 / 78

Kotlin

You have an unordered list of high scores. Which is the simple method to sort the highScores in descending order?

kotlin fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030)

1.

`.sortedByDescending()`

2.

`.descending()`

3.

`.sortedDescending()`

4.

`.sort("DESC")`

Q 59 / 78

Kotlin

Your class has a property name that gets assigned later. You do not want it to be a nullable type. Using a delegate, how should you declare it?

1.

`lateinit var name: String` // lateinit is modifier not delegate

2.

`var name: String by lazy`

3.

`var name: String by Delegates.notNull()`

4.

`var name: String? = null`

Q 60 / 78

Kotlin

You want to know each time a class property is updated. If the new value is not within range, you want to stop the update. Which code snippet shows a built-in delegated property that can accomplish this?

1.

`Delegates.vetoable()`

2.

`Delegates.cancellable()`

3.

`Delegates.observer()`

4.

`Delegates.watcher()`

Q 61 / 78

Kotlin

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?

1.

`val sorted = fibonacci().skip(3).take(6).sortedDescending().toList()`

2.

`val sorted = fibonacci().skip(3).take(6).sortedByDescending().toList()`

3.

`val sorted = fibonacci().skip(3).limit(6).sortedByDescending().toList()`

4.

`val sorted = fibonacci().drop(3).take(6).sortedDescending().toList()`

Q 62 / 78

Kotlin

You have two arrays, a and b. Which line combines a and b as a list containing the contents of both?

kotlin val a = arrayOf(1, 2, 3) val b = arrayOf(100, 200, 3000)

1.

`val c = list of (a, b)`

2.

`val c = a + b`

3.

`val c = listOf(a+b)`

4.

`val c = listOf(*a, *b)`

5.

undefined

Q 63 / 78

Kotlin

This code is occasionally throwing a null pointer exception (NPE). How can you change the code so it never throws as NPE?

kotlin println("length of First Name = ${firstName!!.length}")

1.

Replace `!!.` with `?.`

2.

Replace `!!.` with `?:.`

3.

Surround the line with a try/catch block.

4.

Replace `!!.` with `?.let`.

Q 64 / 78

Kotlin

What is the execution order of init blocks and properties during initialization?

1.

All of the properties are executed in order of appearance, and then the init blocks are executed.

2.

The init blocks and properties are executed in the same order they appear in the code.

3.

All of the init blocks are executed in order of appearance, and then the properties are executed.

4.

The order of execution is not guaranteed, so code should be written accordingly.

Q 65 / 78

Kotlin

Both const and @JvmField create constants. What can @JvmField do that const cannot?

kotlin class Styles { companion object { const val COLOR = "Blue" @JvmField val SIZE = "Really big" } }

1.

`const` works only with strings and primitives. `@JvmField` does not have that restriction.

2.

`@JvmField` works as a top-level variable, but `const` works only in a class.

3.

`@JvmField` is compatible with Java, but `const` is not.

4.

`@JvmField` is always inlined for faster code.

Q 66 / 78

Kotlin

What are the two ways to make a coroutine's computation code cancellable?

1.

Call the `yield()` function or check the `isActive` property.

2.

Call the `cancelled()` function or check the `isActive` property.

3.

Call the `stillActive()` function or check the `isCancelled` property.

4.

Call the `checkCancelled()` function or check the `isCancelled` property.

Q 67 / 78

Kotlin

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

1.

`moveTo( "LA")`

2.

`::moveTo("LA")`

3.

`moveTo("LA")`

4.

`it.moveTo("LA")`

Q 68 / 78

Kotlin

For the Product class you are designing, you would like the price to be readable by anyone, but changeable only from within the class. Which property declaration implements your design?

var price: Int = 0 public get() private set var price: Int = 0 private set var price: Int = 0 val set val price: Int=0

1.

Option 1

2.

Option 2 ([reference](https://kotlinlang.org/docs/properties.html#getters-and-setters))

3.

Option 3

4.

Option 4

Q 69 / 78

Kotlin

What will happen when you try to build and run this code snippet?

kotlin class SpecialFunction : () -> Unit { override fun invoke() { println("Invoked from an instance.") } } fun main() { try { SpecialFunction()() } catch (ex: Exception) { println("An error occurred") } }

1.

A syntax error occurs due to the line `SpecialFunction()()`.

2.

The message "An Error occurred" is displayed.

3.

The message "Invoked from an instance." is displayed. // the second "()" is equals to .invoke()

4.

A compile error occurs. You cannot override the `invoke()` method.

Q 70 / 78

Kotlin

Which statement declares a variable mileage whose value never changes and is inferred to be an integer?

1.

`val mileage:Int = 566`

2.

`var mileage:Int = 566`

3.

`val mileage = 566` (Note: inferred)

4.

`const int mileage = 566`

Q 71 / 78

Kotlin

What is the preferred way to create an immutable variable of type long?

1.

`var longInt = 10L`

2.

`const long longInt = 10`

3.

`val longInt = 10L`

4.

`val longInt:Long = 10`

Q 72 / 78

Kotlin

Which line converts the binaryStr, whish contain only 0s and 1s, to an integer representing its decimal value?

kotlin val binaryStr = "00001111"

1.

`val myInt = toInt(binaryStr)`

2.

`val myInt = binaryStr.toInt("0b")`

3.

`val myInt = binaryStr.toInt()`

4.

`val myInt = binaryStr.toInt(2)`

Q 73 / 78

Kotlin

In a Kotlin program, which lines can be marked with a label

1.

`Any program line can be marked with a label`

2.

`Any statement can be marked with a label`

3.

`Any expression can be marked with a lable`

4.

`Only the beginning of loops can be marked with a label`

Q 74 / 78

Kotlin

All classes in Kotlin inherit from which superclass?

1.

`Default`

2.

`Super`

3.

`Any`

4.

`Object`

Q 75 / 78

Kotlin

You have written a function, sort(), that should accept only collections that implement the `Comparable` interface. How can you restrict the function?

kotlin fun sort(list: List<T>): List <T> { return list.sorted() }

1.

`Add <T -> Comparable<T>> between the `fun` keyword and the function name`

2.

`Add Comparable<T> between the `fun` keyword and the function name`

3.

`Add <T : Comparable<T>> between the `fun` keyword and the function name`

4.

`Add <T where Comparable<T>> between the `fun` keyword and the function name`

Q 76 / 78

Kotlin

Kotlin classes are final by default. What does final mean?

1.

final means that you cannot use interfaces with this class.

2.

final means that this is the only file that can use the class.

3.

final means that you cannot extend the class.

4.

final classes cannot be used in the finally section of a try/catch block.

Q 77 / 78

Kotlin

You have created an array to hold three strings. When you run the code bellow, the compiler displays an error. Why does the code fail?

val names = arrayOf<String>(3) names[3]= "Delta"

1.

Arrays use zero-based indexes. The value 3 is outside of the array's bounds

2.

You accessed the element with an index but should have used.set().

3.

You declared the array with val but should have used var

4.

You cannot changes the value of an element of an array. You should have used a mutable list.

Q 78 / 78