MCQs on Kotlin Standard Library | Kotlin

Kotlin’s Standard Library offers powerful tools for efficient development. Master string manipulation, extensions, and scope functions like let, apply, run, also, and with with these 30 MCQs!


Kotlin Standard Library – MCQs

1. String Manipulation (10 Questions)

  1. Which method can be used to check if a string contains a substring in Kotlin?
    a) contains()
    b) hasSubstring()
    c) substringContains()
    d) checkContains()
  2. What does the toUpperCase() function do in Kotlin?
    a) Converts a string to lowercase
    b) Converts a string to uppercase
    c) Reverses the string
    d) Removes all spaces
  3. Which function is used to trim leading and trailing spaces from a string in Kotlin?
    a) trimSpaces()
    b) trim()
    c) removeSpaces()
    d) strip()
  4. What is the result of the following code?
    val str = "Kotlin" println(str.substring(1, 4)) a) Kot
    b) otl
    c) otl
    d) tin
  5. How can you replace all occurrences of a character in a string in Kotlin?
    a) replace()
    b) replaceAll()
    c) substitute()
    d) replacer()
  6. Which function can be used to split a string by a delimiter in Kotlin?
    a) splitBy()
    b) split()
    c) splitInto()
    d) divide()
  7. How would you check if a string starts with a specific substring?
    a) startsWith()
    b) isStartWith()
    c) beginsWith()
    d) beginningWith()
  8. Which Kotlin method can you use to convert a string to a list of characters?
    a) toList()
    b) splitToList()
    c) asList()
    d) charList()
  9. What does the plus() function do to strings in Kotlin?
    a) Concatenates two strings
    b) Converts a string to uppercase
    c) Replaces spaces with dashes
    d) Removes vowels from a string
  10. Which function is used to find the index of a character in a string?
    a) findIndexOf()
    b) indexOf()
    c) getIndex()
    d) index()

2. Extensions (10 Questions)

  1. What does Kotlin’s extension function allow you to do?
    a) Add new properties to existing classes
    b) Change the behavior of existing methods
    c) Add methods to existing classes without modifying them
    d) Both a and b
  2. How do you define an extension function for the String class?
    a) fun String.someFunction() { ... }
    b) fun someFunction() { ... }
    c) String.someFunction() { ... }
    d) extend String.someFunction() { ... }
  3. Which of the following is true about Kotlin’s extension functions?
    a) They modify the original class
    b) They can access private properties of the class
    c) They do not modify the class itself
    d) They are faster than regular methods
  4. How can you call an extension function in Kotlin?
    a) stringInstance.someFunction()
    b) someFunction(stringInstance)
    c) String.someFunction()
    d) Both a and b
  5. Can extension functions be defined for interfaces in Kotlin?
    a) Yes
    b) No
  6. What will the following code do?
    fun String.printLength() { println(this.length) } "Hello".printLength()
    a) Prints 5
    b) Prints Hello
    c) Prints length
    d) Compilation error
  7. What is the primary advantage of using extension functions?
    a) They increase execution speed
    b) They allow for cleaner, more readable code
    c) They modify original class behavior
    d) They remove the need for inheritance
  8. Can extension functions be overloaded in Kotlin?
    a) Yes
    b) No
  9. How can an extension function access the properties of the receiver object?
    a) Through this keyword
    b) By using receiver
    c) By passing the receiver as a parameter
    d) By using the super keyword
  10. Which of the following is not valid in an extension function?
    a) Accessing class members directly
    b) Modifying class properties
    c) Adding new constructors
    d) Invoking existing methods

3. Scope Functions (10 Questions)

  1. Which of the following scope functions is used to initialize an object and return it?
    a) apply()
    b) let()
    c) run()
    d) with()
  2. What is the primary difference between let and apply?
    a) let returns the receiver object, while apply returns the result of the lambda expression
    b) let and apply work identically
    c) apply returns the receiver object, while let returns the result of the lambda expression
    d) apply is used for nullable types only
  3. How does the also function differ from let in Kotlin?
    a) also can modify the receiver object
    b) also returns the result of the lambda
    c) also is used for side effects
    d) also returns Unit
  4. Which scope function is most commonly used for chaining multiple actions on an object?
    a) let
    b) apply
    c) with
    d) run
  5. What is the result of the following code?kotlinCopy codeval x = StringBuilder("Kotlin") val result = x.apply { append(" is fun") } println(result) a) Kotlin is fun
    b) Kotlin
    c) is fun
    d) Compilation error
  6. Which scope function is used to run a block of code on a non-null object?
    a) run
    b) let
    c) apply
    d) with
  7. How would you describe the purpose of the with function?
    a) It is used to call multiple functions on an object and return a value
    b) It initializes objects
    c) It returns the receiver object
    d) It is used for side effects
  8. What is the correct use case for the run scope function?
    a) For modifying properties of the receiver object
    b) For performing actions on an object and returning a result
    c) For initializing objects
    d) For accessing receiver properties
  9. Which scope function is best used for performing operations on a nullable object?
    a) let
    b) apply
    c) run
    d) also
  10. What will be the output of the following code?kotlinCopy codeval result = StringBuilder("Kotlin") result.run { append(" is powerful") } println(result) a) Kotlin is powerful
    b) Kotlin
    c) is powerful
    d) Compilation error

Answers

QNoAnswer (Option with the text)
1a) contains()
2b) Converts a string to uppercase
3b) trim()
4b) otl
5a) replace()
6b) split()
7a) startsWith()
8a) toList()
9a) Concatenates two strings
10b) indexOf()
11c) Add methods to existing classes without modifying them
12a) fun String.someFunction() { … }
13c) They do not modify the class itself
14d) Both a and b
15a) Yes
16a) Prints 5
17b) They allow for cleaner, more readable code
18a) Yes
19a) Through this keyword
20c) Adding new constructors
21a) apply()
22c) apply returns the receiver object, while let returns the result of the lambda expression
23c) also is used for side effects
24b) apply
25a) Kotlin is fun
26a) run
27a) It is used to call multiple functions on an object and return a value
28b) For performing actions on an object and returning a result
29a) let
30a) Kotlin is powerful

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top