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

Posted Date:-2022-08-10 12:37:21


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

Posted Date:-2022-08-10 12:26:00


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

Posted Date:-2022-08-10 12:54:14


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

Posted Date:-2022-08-10 12:11:31


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

Posted Date:-2022-08-10 12:41:25


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

Posted Date:-2022-08-10 12:10:44


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

Posted Date:-2022-08-10 12:30:07


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

Posted Date:-2022-08-10 12:21:33


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)

Posted Date:-2022-08-10 12:52:48


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

Posted Date:-2022-08-10 12:22:33


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.

Posted Date:-2022-08-10 12:55:17


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

Posted Date:-2022-08-10 12:49:39


Question:
What is the default visibility modifier in Kotlin?

1.Protected

2.Private

3.Internal

4.Public

Posted Date:-2022-08-10 12:09:48


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

Posted Date:-2022-08-10 12:13:22


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

Posted Date:-2022-08-10 12:34:55


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

Posted Date:-2022-08-10 12:27:16


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

1.Change()

2.Modify()

3.Set()

4.Assign()

Posted Date:-2022-08-10 12:32:00


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)

Posted Date:-2022-08-10 12:39:10


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

Posted Date:-2022-08-10 12:36:13


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

Posted Date:-2022-08-10 12:51:07


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

Posted Date:-2022-08-10 12:59:58


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)

Posted Date:-2022-08-10 12:23:55


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

Posted Date:-2022-08-10 12:42:33


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

Posted Date:-2022-08-10 12:20:33


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

Posted Date:-2022-08-10 12:28:16


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

Posted Date:-2022-08-10 12:16:15


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)

Posted Date:-2022-08-10 12:44:34


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(

Posted Date:-2022-08-10 12:57:53


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

Posted Date:-2022-08-10 12:40:02


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

Posted Date:-2022-08-10 12:18:08


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)

Posted Date:-2022-08-10 13:01:15


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

Posted Date:-2022-08-10 12:58:48


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

Posted Date:-2022-08-10 12:56:27


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

Posted Date:-2022-08-10 12:46:03


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

Posted Date:-2022-08-10 12:29:06


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

Posted Date:-2022-08-10 12:38:23


More MCQS

  1. Kotlin MCQ
  2. Kotlin MCQ Set 1
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website 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!