kotlin fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030) }
1.
`.withIndex()`
2.
`.forEachIndexed()`
3.
`.forEach()`
4.
`.forIndexes()`
Q 1 / 78
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
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
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 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
1.
`fun static main(){}`
2.
`fun main(){}`
3.
`fun Main(){}`
4.
`public static void main(){}`
Q 6 / 78
1.
a do..while loop
2.
a for loop
3.
a while loop
4.
a forEach loop
Q 7 / 78
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 val task = launch { // long running job }
1.
`task.join()`
2.
`task.abort()`
3.
`job.stop()`
4.
`task.cancel()`
Q 9 / 78
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 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
1.
`instanceof`
2.
`is`
3.
`typeof`
4.
`as`
Q 12 / 78
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 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 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 val set = setOf("apple", "pear", "orange", "apple") println(set.count())
1.
3
2.
4
3.
1
4.
5
Q 16 / 78
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 val list2 = (80..100).toList().filter(_____)
1.
`::removeBadValues`
2.
`GlobalScope.removeBadValues()`
3.
`Mainkt.removeBadValues`
4.
`removeBadValues`
Q 18 / 78
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
1.
an anonymous object
2.
a static property
3.
a companion object
4.
a backing field
Q 20 / 78
1.
`as?`
2.
`??`
3.
`is`
4.
`as`
Q 21 / 78
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
1.
`change()`
2.
`modify()`
3.
`set()`
4.
`assign()`
Q 23 / 78
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 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 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 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
1.
`object DatabaseManager {}`
2.
`singleton DatabaseManager {}`
3.
`static class DatabaseManager {}`
4.
`data class DatabaseManager {}`
Q 28 / 78
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 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
1.
`obj.classInfo()`
2.
`obj.typeInfo()`
3.
`obj::class.simpleName`
4.
`obj::class`
Q 31 / 78
1.
`val arrs[5]: Int`
2.
`val arrs = IntArray(5)`
3.
`val arrs: Int[5]`
4.
`val arrs = Array<Int>(5)`
Q 32 / 78
1.
`internal`
2.
`private`
3.
`public`
4.
`protected`
Q 33 / 78
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
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
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
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 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
1.
`Delegates.watcher()`
2.
`Delegates.observable()`
3.
`Delegates.rx()`
4.
`Delegates.observer()`
Q 39 / 78
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
1.
`val name = null`
2.
`var name: String`
3.
`val name: String`
4.
`val name: String? = null`
Q 41 / 78
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 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 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 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 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
1.
`a.from(b)`
2.
`a.range(b)`
3.
`a.rangeTo(b)`
4.
`a.to(b)`
Q 47 / 78
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 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 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 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 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 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 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 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 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 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 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 fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030)
1.
`.sortedByDescending()`
2.
`.descending()`
3.
`.sortedDescending()`
4.
`.sort("DESC")`
Q 59 / 78
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
1.
`Delegates.vetoable()`
2.
`Delegates.cancellable()`
3.
`Delegates.observer()`
4.
`Delegates.watcher()`
Q 61 / 78
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 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 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
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 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
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 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
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 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
1.
`val mileage:Int = 566`
2.
`var mileage:Int = 566`
3.
`val mileage = 566` (Note: inferred)
4.
`const int mileage = 566`
Q 71 / 78
1.
`var longInt = 10L`
2.
`const long longInt = 10`
3.
`val longInt = 10L`
4.
`val longInt:Long = 10`
Q 72 / 78
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
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
1.
`Default`
2.
`Super`
3.
`Any`
4.
`Object`
Q 75 / 78
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
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
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