Q1. 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)
}
- .withIndex() (reference)
- .forEachIndexed()
- .forEach()
- .forIndexes()
Q2. 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
}
- Classes are initialized in the same order they are in the file, therefore, Aircraft should appear after Airplane
- The code needs to pass the parameter to the base class's primary constructor. Since it does not, it receives a null
- The abstract function always returns null
- A superclass is initialized before its subclass. Therefore, the name has not been set before it is rendered
Q3. Kotlin interfaces ad abstract classes are very similar. What is one thing abstract class can do that interfaces cannot?
- Only abstract classes are inheritable by subclasses
- Only abstract classes can inherit from multiple superclasses
- Only abstract classes can have abstract methods
- Only abstract classes can store state reference
Q4. Inside an extension function, what is the name of the variable that corresponds to the receiver object?
- The variable is named it
- The variable is named this reference
- The variable is named receiver
- The variable is named default
Q5. Your application has an additional function. How could you use its invoke methods and display the results?
fun add(a: Int, b: Int): Int {
return a + b
}
- println(add(5,10).invoke())
- println(::add.invoke(5, 10)) (reference)
- println(::add.invoke{5, 10})
- println(add.invoke(5,10))
Q6. What is the entry point for a Kotlin application?
- fun static main(){}
- fun main(){}
- fun Main(){}
- public static void main(){}
Q7. You are writing a console app in Kotlin that processes tests 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
- a do..while loop (reference)
- a for loop
- a while loop
- a forEach loop
Q8. 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)
}
- While the code runs, it does not produce correct results
- The integer is always a class
- The compiler runs an implicit .toClass() method on the integer
- The integer is autoboxed to a Kotlin Int class
Q9. 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?
val task = launch {
// long running job
}
- task.join()
- task.abort()
- job.stop()
- task.cancel() (reference)
Q10. You are attempting to assign an integer variable to a long variable, but the Kotlin compiler flags it as an error. Why?
- You must wrap all implicit conversion in a try/catch block
- You can only assign Long to an Int, not the other way around
- There is no implicit conversion from Int to Long (reference)
- All integers in Kotlin are of type Long
Q11. 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?
when (die) {
1 -> println("die is 1")
2 -> println("die is 2")
___ -> printlin("die is between 3 and 6")
else -> printlin("dies is unknown")
}
- 3,4,5,6
- in 3..6 (reference)
- 3 : 6
- {3,4,5,6}
Q12. The function type checker receives 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 object?
- instance of
- is (reference)
- typeof
- as
Q13. This code does not print any output to the console. What is wrong?
firstName?.let {
println("Greeting $firstname!")
}
- A null pointer exception is thrown
- firstName is equal to null (reference)
- firstName is equal to an empty string
- firstName is equal to Boolean false
Q14. 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?
inline fun simple(x: Int): Int{
return x * x
}
fun main() {
for(count in 1..1000) {
simple(count)
}
}
- The code will give a stack overflow error
- The compiler warns of insignificant performance impact (reference)
- The compiler warns of significant memory usage
- The code is significantly faster
Q15.How do you fill in the blank below to display all of the even numbers from 1 to 10 with the least amount of code?
for (_____) {
println("There are $count butterflies.")
}
- count in 1..10
- count in 2..10 step 2 (reference)
- count in 1..10 % 2
- var count=2; count <= 10; count+=2
Q16. What value is printed by println()?
val set = setOf("apple", "pear", "orange", "apple")
println(set.count())
Q17. Which line of code shows how to display a nullable string's length and shows 0 instead of null?
- println(b!!.length ?: 0)
- println(b?.length ?: 0)
- println(b?.length ?? 0)
- println(b == null? 0: b.length)
Q18. In the file main.kt, you are filtering a list of integers and want to use an already existing function, remove values. What is the proper way to invoke the function from the filter in the line below?
val list2 = (80..100).toList().filter(_____)
- ::removeBadValues (reference)
- GlobalScope.removeBadValues()
- Mainkt.removeBadValues
- removeBadValues
Q19. Which code snippet correctly shows a for loop using a range to display "1 2 3 4 5 6"?
- for(z in 1..7) println("$z ")
- for(z in 1..6) print("$z ") (reference)
- for(z in 1 to 6) print("$z ")
- for(z in 1..7) print("$z ")
Q20. You are upgrading a Java class to Kotlin. What should you use to replace the Java class's static fields?
- an anonymous object
- a static property
- a companion object (reference)
- a backing field
Q21. Your code needs 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?
Q22. Kotlin will not compile this code snippet. What is wrong?
class Employee
class Manager: Employee()
- To inherit from a class, it must be marked open (reference)
- To inherit from a class, it must be marked public
- To inherit from a class, it must be marked sealed
- To inherit from a class, it must be marked override
Q23. Which function changes the value of the element at the current iterator location?
- change()
- modify()
- set() (reference)
- assign()
Q24. 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()")
}
}
- Employee.display()
- ::display()
- super.display() (reference)
- override.display()
Q25. The code below was compiled and executed without issue before the addition of the line declaring error status. 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()
}
- StatusError is an object, not a class, and cannot be instantiated (reference)
- Only one instance of the class Status can be instantiated at a time
- Status. The error must be declared as an immutable type
- Status. Error is private to the class and cannot be declared externally
Q26. 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)
- You cannot assign a sequence to a variable
- To produce a result, a sequence must have a terminal operation. In this case, it needs a .toList() (reference)
- The .filter{ it < 11 } should be .filter{ it > 11 }
- The yieldAll(1..20) should be yieldAll(1..10)
Q27. What three methods does this class have?
class Person
- equals(), hashCode(), and toString() (reference)
- equals(), toHash(), and super()
- print(), println(), and toString()
- clone(), equals(), and super()
Q28. Which is the proper way to declare a singleton named DatabaseManager?
- object DatabaseManager {} (reference)
- singleton DatabaseManager {}
- static class DatabaseManager {}
- data class DatabaseManager {}
Q29. To subclass the Person class, what is one thing you must do?
abstract class Person(val name: String) {
abstract fun displayJob(description: String)
}
- The subclass must be marked sealed
- You must override the display job() method (reference)
- You must mark the subclass as final
- An abstract class cannot be extended, so you must change it to open
Q30. The code snippet below translates a database user to a model user. Because their names are both Users, you must use their fully qualified names, which is cumbersome. You do not have access to either of the imported classes' source codes. 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}")
}
- Use import to change the type name (reference)
- Create subtypes with shorter names
- Create interfaces with shorter names
- Create extension classes with shorter names
Q31. 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?
- obj.classInfo()
- obj.typeInfo()
- obj::class.simpleName
- obj::class (reference)
Q32. Which is the correct declaration of an integer array with a size of 5?
- val arrs[5]: Int
- val arrs = IntArray(5) (reference)
- val arrs: Int[5]
- val arrs = Array<Int>(5)
Q33. You have created a class that should be visible only to the other code in its module. Which modifier do you use?
- internal (reference)
- private
- public
- protected
Q34. Kotlin has two equality operators, == and ===. What is the difference?
- == determines if two primitive types are identical. === determines if two objects are identical
- == determines if two references point to the same object. === determines if two objects have the same value
- == determines if two objects have the same value. === determines if two strings have the same value
- == determines if two objects have the same value. === determines if two references point to the same object (reference)
Q35. Which snippet correctly shows setting the variable max to whichever variable holds the greatest value, a or b, using idiomatic Kotlin?
- val max3 = a.max(b)
- val max = a > b ? a : b
- val max = if (a > b) a else b
- if (a > b) max = a else max = b
Q36. 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 }
- println(Signal.SENDING.position())
- println(Signal.SENDING.hashCode())
- println(Signal.SENDING)
- println(Signal.SENDING.ordinal) (reference)
Q37. 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"
}
}
- const is compatible with Java, but @JvmField is not
- The compiler will inline const so it is faster and more memory efficient (reference)
- Virtually any type can be used with const but not @JvmField
- const can also be used with mutable types
Q38. 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"
}
}
- Replace open with closed
- Replace open with sealed (reference)
- Replace open with private
- Replace open with the public
Q39. 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?
- Delegates.watcher()
- Delegates.observable() (reference)
- Delegates.rx()
- Delegates.observer()
Q40. Why doesn't this code compile?
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)
}
- infix function must be marked public
- In Kotlin, add is a keyword
- Extension functions use it, not this, as the default parameter name
- infix functions cannot have default values (reference)
Q41. What is the correct way to initialize a nullable variable?
- val name = null
- var name: String
- val name: String
- val name: String? = null
Q42. Which line of code is a shorter, more idiomatic version of the displayed snippet?
val len: Int = if (x != null) x.length else -1
- val len = x?.let{x.len} else {-1}
- val len = x!!.length ?: -1
- val len:Int = (x != null)? x.length : -1
- val len = x?.length ?: -1
Q43. You are creating a Kotlin unit test library. What else should you add to make the following code compile without error?
fun String.shouldEqual(value: String) = this == value
fun main(){
val msg = "test message"
println(msg shouldEqual "test message")
}
- The extension function should be marked public
- Add the prefix operator to the shouldMatch extension function
- The code is not legal in Kotlin (should be println(msg.shouldEqual("test message")))
- Add the prefix infix to the shouldMatch extension function
Q44. What is the difference between the declarations of COLOR and SIZE?
class Record{
companion object {
const val COLOR = "Red"
val SIZE = "Large"
}
}
- Since COLOR and SIZE are both immutable, they are identical internally
- Both are immutable, but the use of the keyword const makes COLOR slower and less space-efficient than SIZE
- const makes COLOR faster but is not compatible with Java. Without const, SIZE is still compatible with Java
- Both are immutable, but the use of the keyword const makes COLOR faster and more space-efficient than SIZE (reference)
Q45. Why does not this code snippet compile?
class Cat (name: String) {
fun greet() { println("Hello ${this.name}") }
}
fun main() {
val thunderCat = Cat("ThunderCat")
thunderCat.greet()
}
- Because the name is a class parameter, not a property-it is unresolved main().
- To create an instance of a class, you need the keyword new
- The reference to name needs to be scoped to the class, so it should be this. name
- Classes cannot be immutable. You need to change var to Val
Q46. 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("$ndx - $value")
ndx++
}
- for( (ndx, value) in (1..20).withIndex() ){ (reference)
- for( (ndx, value) in (1..20).pair() ){
- for( Pair(ndx, value) in 1..20 ){
- for( (ndx, value) in *(1..20) ){
Q47. The Kotlin .. operator can be written as which function?
- a.from(b)
- a.range(b)
- a.rangeTo(b) (reference)
- a.to(b)
Q48. 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")
}
- proj.0
- proj[0]
- proj[1]
- proj.component1() (reference)
Q49. This function generates the Fibonacci sequence. Which function is missing?
fun fibonacci() = sequence {
var params = Pair(0, 1)
while (true) {
___(params.first)
params = Pair(params.second, params.first + params.second)
}
}
- with()
- yield() (reference)
- skip()
- return()
Q50. In this code snippet, why does the compiler not allow the value of y to change?
for(y in 1..100) y+=2
- y must be declared with var to be mutable
- y is an implicitly immutable value
- y can change only in a while loop
- To change y, it must be declared outside of the loop
- ===================
- Related searches
- LinkedIn assessment quiz answers
- LinkedIn quiz answers 2020
- LinkedIn Solidworks assessment answers
- LinkedIn assessment answers 2020
- LinkedIn quiz answers 2021
- LinkedIn VBA assessment answers
- LinkedIn WordPress assessment answers
- LinkedIn assessment answers GitHub
No comments:
Post a Comment