Kotlin (programming language)/Kotlin MCQ Sample Test,Sample questions

Question:
In order to subclass the Person class, what is one thing you must do? 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


Question:
In the file main.kt, you are 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? Val list2 = (80..100).toList().filter(_____)

1.::removeBadValues

2.GlobalScope.removeBadValues()

3.Mainkt.removeBadValues✖

4.RemoveBadValues


Question:
In this code snippet, why does the compiler not allow the value of y to change? 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


Question:
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


Question:
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


Question:
Kotlin interfaces and 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


Question:
Kotlin will not compile this code snippet. What is wrong? 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


Question:
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


Question:
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)


Question:
This code does not print any output to the console. What is wrong? firstName?.let { println("Greeting $firstname!")}

1.FirstName?.let { println(

2.A null pointer exception is thrown

3.FirstName is equal to null

4.FirstName is equal to an empty string


Question:
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)

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.


Question:
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


Question:
What is the default visibility modifier in Kotlin?

1.Protected

2.Private

3.Internal

4.Public


Question:
What is the entry point for a Kotlin application?

1.Fun static main(){}

2.Fun main(){}

3. Fun Main(){}

4.Public static void main(){}


Question:
What three methods does this class have? Class Person

1.Equals(), hashCode(), and toString()

2.Equals(), toHash(), and super()

3.Print(), println(), and toString()

4.Clone(), equals(), and super()


Question:
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 ")


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

1.Change()

2.Modify()

3.Set()

4.Assign()


Question:
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(5)


Question:
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 {}


Question:
Which line of code is a shorter, more idiomatic version of the displayed snippet? 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


Question:
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()


Question:
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)


Question:
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


Question:
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


Question:
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


Question:
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


Question:
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?

1.Enum class Signal { OPEN, CLOSED, SENDING }

2.Println(Signal.SENDING.position())

3.Println(Signal.SENDING.hashCode()

4.Println(Signal.SENDING.ordinal)


Question:
You have an unordered list of high scores. Which is the simple method to sort the highScores in descending order? fun main() { val highScores = listOf(4000, 2000, 10200, 12000, 9030)}

1..sortedByDescending()

2..descending()

3..sortedDescending()

4..sort(


Question:
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


Question:
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}

1.Task.join()

2.Task.abort()

3.Job.stop()

4.Task.cancel()


Question:
You have two arrays, a and b. Which line combines a and b as a list containing the contents of both? 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)


Question:
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()


Question:
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? 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())


Question:
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()


Question:
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


Question:
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


More MCQS

  1. Kotlin MCQ
  2. Kotlin MCQ Set 1
Search
Olete Team
Online Exam TestTop Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on Online Exam Testwebsite is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!