You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
funmain() {
val highScores =listOf(4000, 2000, 10200, 12000, 9030)
}
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 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
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?
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?
Q10. You are attempting to assign an integer variable to a long variable, but 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")
}
Q12. 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?
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?
inlinefunsimple(x:Int): Int{
return x * x
}
funmain() {
for(count in1..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 least amount of code?
for (_____) {
println("There are $count butterflies.")
}
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 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?
Q21. 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?
You must override the displayJob() 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 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?
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?
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?
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?
Because name is a class parameter, not a property-it is unresolved main().
In order 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 in1..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?
Q50. In this code snippet, why does the compiler not allow the value of y to change?
for(y in1..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
In order to change y, it must be declared outside of the loop
Q51. 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?
data classPoint(valx:Int, valy:Int)
operatorfun Point.plus(other:Point) =Point(x + other.x, y + other.y)
operatorfun Point.hash(other:Point) =Point(x - other.x, y - other.y)
funmain() {
val point1 =Point(10, 20)
val point2 =Point(20, 30)
println(point1 + point2)
println(point1 # point2)
}
You cannot; the hash symbol is not a valid operator.
You should replace the word hash with octothorpe, the actual name for the symbol.
You should use minus instead of hash, then type alias the minus symbol.
You need to replace operator with the word infix.
Q52. This code snippet compiles without error, but never prints the results when executed. What could be wrong?
val result = generateSequence(1) { it +1 }.toList()
println(result)
The sequence lacks a terminal operation.
The sequence is infinite and lacks an intermediate operation to make it finite.
The expression should begin with generateSequence(0).