0
0
Kotlinprogramming~10 mins

String type and immutability in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a string variable named greeting.

Kotlin
val greeting: String = [1]
Drag options to blanks, or click blank then click option'
A'Hello, Kotlin!'
BHello, Kotlin!
C"Hello, Kotlin!"
DString("Hello, Kotlin!")
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Not enclosing the string in quotes at all.
2fill in blank
medium

Complete the code to get the length of the string stored in name.

Kotlin
val length = name.[1]
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() as if it were a method.
Using size or count which are not properties of String.
3fill in blank
hard

Fix the error in the code that tries to change a character in a string.

Kotlin
var text = "Kotlin"
text[1] = 'C'
Drag options to blanks, or click blank then click option'
A{0}
B[0]
C(0)
D<0>
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign a character to a string index (strings are immutable).
Using parentheses or braces instead of square brackets.
4fill in blank
hard

Fill both blanks to create a new string by replacing the first character of original with 'C'.

Kotlin
val original = "Kotlin"
val modified = [1] + original.[2](1)
Drag options to blanks, or click blank then click option'
A"C"
Bsubstring
CsubSequence
DremoveRange
Attempts:
3 left
💡 Hint
Common Mistakes
Using subSequence instead of substring which returns CharSequence, not String.
Trying to modify the original string directly.
5fill in blank
hard

Fill all three blanks to create a new string with all letters uppercase and check if it equals "KOTLIN".

Kotlin
val word = "kotlin"
val upperWord = word.[1]()
val isEqual = upperWord [2] "KOTLIN"
println(isEqual)  // prints [3]
Drag options to blanks, or click blank then click option'
Auppercase
B==
Ctrue
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals() method without parentheses.
Using equals instead of == for comparison.
Expecting the print output to be false.