πŸ§ͺ Kotlin (programming language) MCQ Quiz Hub

Kotlin MCQ

Choose a topic to test your knowledge and improve your Kotlin (programming language) skills

What is the default visibility modifier in Kotlin?





βœ… Correct Answer: 4

Kotlin interfaces and abstract classes are very similar. What is one thing abstract class can do that interfaces cannot?





βœ… Correct Answer: 4

Inside an extension function, what is the name of the variable that corresponds to the receiver object





βœ… Correct Answer: 2

What is the entry point for a Kotlin application?





βœ… Correct Answer: 2

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





βœ… Correct Answer: 1

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}





βœ… Correct Answer: 4

You are attempting to assign an integer variable to a long variable, but Kotlin compiler flags it as an error. Why?





βœ… Correct Answer: 4

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?





βœ… Correct Answer: 2

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





βœ… Correct Answer: 2

Which line of code shows how to display a nullable string's length and shows 0 instead of null?





βœ… Correct Answer: 2

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





βœ… Correct Answer: 1

Which code snippet correctly shows a for loop using a range to display "1 2 3 4 5 6"?





βœ… Correct Answer: 2

You are upgrading a Java class to Kotlin. What should you use to replace the Java class's static fields?





βœ… Correct Answer: 3

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?





βœ… Correct Answer: 1

Kotlin will not compile this code snippet. What is wrong? class Employee class Manager : Employee()





βœ… Correct Answer: 1

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





βœ… Correct Answer: 3

What three methods does this class have? Class Person





βœ… Correct Answer: 1

Which is the proper way to declare a singleton named DatabaseManager?





βœ… Correct Answer: 1

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





βœ… Correct Answer: 2

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?





βœ… Correct Answer: 4

Which is the correct declaration of an integer array with a size of 5?





βœ… Correct Answer: 2

You have created a class that should be visible only to the other code in its module. Which modifier do you use?





βœ… Correct Answer: 1

Kotlin has two equality operators, == and ===. What is the difference?





βœ… Correct Answer: 4

Which snippet correctly shows setting the variable max to whichever variable holds the greatest value, a or b, using idiomatic Kotlin?





βœ… Correct Answer: 1

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?





βœ… Correct Answer: 4

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?





βœ… Correct Answer: 2

What is the correct way to initialize a nullable variable?





βœ… Correct Answer: 4

Which line of code is a shorter, more idiomatic version of the displayed snippet? val len: Int = if (x != null) x.length else -1





βœ… Correct Answer: 4

The Kotlin .. operator can be written as which function?





βœ… Correct Answer: 3

In this code snippet, why does the compiler not allow the value of y to change? For(y in 1..100) y+=2





βœ… Correct Answer: 2

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)





βœ… Correct Answer: 1

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)





βœ… Correct Answer: 1

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





βœ… Correct Answer: 3

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?





βœ… Correct Answer: 1

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?





βœ… Correct Answer: 4

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)





βœ… Correct Answer: 4