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)
Which method can be used to check if a string contains a substring in Kotlin? a) contains() b) hasSubstring() c) substringContains() d) checkContains()
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
Which function is used to trim leading and trailing spaces from a string in Kotlin? a) trimSpaces() b) trim() c) removeSpaces() d) strip()
What is the result of the following code? val str = "Kotlin" println(str.substring(1, 4)) a) Kot b) otl c) otl d) tin
How can you replace all occurrences of a character in a string in Kotlin? a) replace() b) replaceAll() c) substitute() d) replacer()
Which function can be used to split a string by a delimiter in Kotlin? a) splitBy() b) split() c) splitInto() d) divide()
How would you check if a string starts with a specific substring? a) startsWith() b) isStartWith() c) beginsWith() d) beginningWith()
Which Kotlin method can you use to convert a string to a list of characters? a) toList() b) splitToList() c) asList() d) charList()
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
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)
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
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() { ... }
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
How can you call an extension function in Kotlin? a) stringInstance.someFunction() b) someFunction(stringInstance) c) String.someFunction() d) Both a and b
Can extension functions be defined for interfaces in Kotlin? a) Yes b) No
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
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
Can extension functions be overloaded in Kotlin? a) Yes b) No
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
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)
Which of the following scope functions is used to initialize an object and return it? a) apply() b) let() c) run() d) with()
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
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
Which scope function is most commonly used for chaining multiple actions on an object? a) let b) apply c) with d) run
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
Which scope function is used to run a block of code on a non-null object? a) run b) let c) apply d) with
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
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
Which scope function is best used for performing operations on a nullable object? a) let b) apply c) run d) also
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
QNo
Answer (Option with the text)
1
a) contains()
2
b) Converts a string to uppercase
3
b) trim()
4
b) otl
5
a) replace()
6
b) split()
7
a) startsWith()
8
a) toList()
9
a) Concatenates two strings
10
b) indexOf()
11
c) Add methods to existing classes without modifying them
12
a) fun String.someFunction() { … }
13
c) They do not modify the class itself
14
d) Both a and b
15
a) Yes
16
a) Prints 5
17
b) They allow for cleaner, more readable code
18
a) Yes
19
a) Through this keyword
20
c) Adding new constructors
21
a) apply()
22
c) apply returns the receiver object, while let returns the result of the lambda expression
23
c) also is used for side effects
24
b) apply
25
a) Kotlin is fun
26
a) run
27
a) It is used to call multiple functions on an object and return a value
28
b) For performing actions on an object and returning a result