MCQs > IT & Programming > Kotlin MCQs > Basic Kotlin MCQs

Basic Kotlin MCQ

1. What is the default visibility modifier in Kotlin?

Answer

Correct Answer: Public

Note: This Question is unanswered, help us to find answer for this one

2. How many different kinds of constructors are available for kotlin classes?

Answer

Correct Answer: Two.

Note: This Question is unanswered, help us to find answer for this one

3. When you can omit constructor keyword from the primary constructor?

Answer

Correct Answer: It can be omitted when the primary constructor does not have any modifiers or annotations.

Note: This Question is unanswered, help us to find answer for this one

4. If a class has one or more secondary constructors, what must each of them do?

Answer

Correct Answer: Each secondary constructor must directly or indirectly delegate to the primary.

Note: This Question is unanswered, help us to find answer for this one

5. You have created an array to hold three strings. When you run the code bellow, the compiler displays an error. Why does the code fail? val names = arrayOf(3) names[3]= "Delta"

Answer

Correct Answer: Arrays use zero-based indexes. The value 3 is outside of the array's bounds

Note: This Question is unanswered, help us to find answer for this one

6. Kotlin classes are final by default. What does final mean?

Answer

Correct Answer: Final means that you cannot extend the class.

Note: This Question is unanswered, help us to find answer for this one

7. You have written a function, sort(), that should accept only collections that implement the Comparable interface. How can you restrict the function? fun sort(list: List): List { return list.sorted()}

Answer

Correct Answer: Add > between the fun keyword and the function name

Note: This Question is unanswered, help us to find answer for this one

8. All classes in Kotlin inherit from which superclass?

Answer

Correct Answer: Any

Note: This Question is unanswered, help us to find answer for this one

9. In a Kotlin program, which lines can be marked with a label

Answer

Correct Answer: Any expression can be marked with a label

Note: This Question is unanswered, help us to find answer for this one

10. Which line converts the binaryStr, whish contain only 0s and 1s, to an integer representing its decimal value? val binaryStr = "00001111"

Answer

Correct Answer: Val myInt = binaryStr.toInt(2)

Note: This Question is unanswered, help us to find answer for this one

11. What is the preferred way to create an immutable variable of type long?

Answer

Correct Answer: Val longInt = 10L

Note: This Question is unanswered, help us to find answer for this one

12. Which statement declares a variable mileage whose value never changes and is inferred to be an integer?

Answer

Correct Answer: Val mileage = 566 (Note: inferred)

Note: This Question is unanswered, help us to find answer for this one

13. What are the two ways to make a coroutine's computation code cancellable?

Answer

Correct Answer: Call the yield() function or check the isActive property.

Note: This Question is unanswered, help us to find answer for this one

14. What is the execution order of init blocks and properties during initialization?

Answer

Correct Answer: The init blocks and properties are executed in the same order they appear in the code.

Note: This Question is unanswered, help us to find answer for this one

15. This code is occasionally throwing a null pointer exception (NPE). How can you change the code so it never throws as NPE? println("length of First Name = ${firstName!!.length}")

Answer

Correct Answer: Replace !!. with ?.

Note: This Question is unanswered, help us to find answer for this one

16. 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)

Answer

Correct Answer: Val c = listOf(*a, *b)

Note: This Question is unanswered, help us to find answer for this one

17. 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?

Answer

Correct Answer: Val sorted = fibonacci().drop(3).take(6).sortedDescending().toList()

Note: This Question is unanswered, help us to find answer for this one

18. 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?

Answer

Correct Answer: Delegates.vetoable()

Note: This Question is unanswered, help us to find answer for this one

19. Your class has a property name that gets assigned later. You do not want it to be a nullable type. Using a delegate, how should you declare it?

Answer

Correct Answer: Var name: String by Delegates.notNull()

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: .sortedDescending()

Note: This Question is unanswered, help us to find answer for this one

21. 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)

Answer

Correct Answer: Println(students.groupBy{ it.lastName }.count())

Note: This Question is unanswered, help us to find answer for this one

22. 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)

Answer

Correct Answer: The sequence lacks a terminal operation.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Y is an implicitly immutable value

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A.rangeTo(b)

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Val len = x?.length ?: -1

Note: This Question is unanswered, help us to find answer for this one

26. What is the correct way to initialize a nullable variable?

Answer

Correct Answer: Val name: String? = null

Note: This Question is unanswered, help us to find answer for this one

27. 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?

Answer

Correct Answer: Delegates.observable()

Note: This Question is unanswered, help us to find answer for this one

28. 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?

Answer

Correct Answer: Println(Signal.SENDING.ordinal)

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Val max3 = a.max(b) (Extension Function is One of the idiomatic Solutions in Kotlin)

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: == determines if two objects have the same value. === determines if two references point to the same object

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Internal

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Val arrs = IntArray(5)

Note: This Question is unanswered, help us to find answer for this one

33. 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?

Answer

Correct Answer: Obj::class

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: You must override the displayJob() method

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Object DatabaseManager {}

Note: This Question is unanswered, help us to find answer for this one

36. What three methods does this class have? Class Person

Answer

Correct Answer: Equals(), hashCode(), and toString()

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Set()

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: In order to inherit from a class, it must be marked open

Note: This Question is unanswered, help us to find answer for this one

39. 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?

Answer

Correct Answer: As?

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A companion object

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: for(z in 1..6) print("$z ")

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: ::removeBadValues

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Println(b?.length ?: 0)

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A null pointer exception is thrown

Note: This Question is unanswered, help us to find answer for this one

45. 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?

Answer

Correct Answer: Is

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: There is no implicit conversion from Int to Long

Note: This Question is unanswered, help us to find answer for this one

47. 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}

Answer

Correct Answer: Task.cancel()

Note: This Question is unanswered, help us to find answer for this one

48. 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

Answer

Correct Answer: A do..while loop

Note: This Question is unanswered, help us to find answer for this one

49. What is the entry point for a Kotlin application?

Answer

Correct Answer: Fun main(){}

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The variable is named this

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Only abstract classes can store state

Note: This Question is unanswered, help us to find answer for this one

search
Kotlin Subjects