0
0
Kotlinprogramming~5 mins

String type and immutability in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that a String is immutable in Kotlin?
It means once a String is created, its content cannot be changed. Any modification creates a new String.
Click to reveal answer
beginner
How do you create a String in Kotlin?
You create a String by enclosing text in double quotes, for example: val greeting = "Hello".
Click to reveal answer
beginner
Can you change a character inside a Kotlin String directly?
No, you cannot change characters inside a String directly because Strings are immutable. You must create a new String if you want changes.
Click to reveal answer
beginner
What happens when you use the plus operator (+) to add text to a String in Kotlin?
A new String is created that combines the original and the added text. The original String stays unchanged.
Click to reveal answer
intermediate
Why is String immutability useful in programming?
It helps avoid unexpected changes, makes programs safer, and allows sharing Strings without copying.
Click to reveal answer
In Kotlin, what happens if you try to change a character in a String directly?
AYou get a compile-time error because Strings are immutable.
BThe character changes successfully.
CThe program crashes at runtime.
DThe String automatically converts to a mutable type.
How do you create a new String by adding text to an existing String in Kotlin?
AModify the original String directly.
BUse the plus (+) operator to combine them.
CUse the minus (-) operator.
DCall a special mutableString() function.
Which of these is true about Kotlin Strings?
AThey are immutable and cannot be changed after creation.
BThey are mutable and can be changed anytime.
CThey are stored as arrays of characters that can be edited.
DThey automatically update when variables change.
Why might immutability of Strings improve program safety?
ABecause immutability allows Strings to be deleted automatically.
BBecause Strings run faster when mutable.
CBecause Strings cannot be changed unexpectedly, reducing bugs.
DBecause it allows direct memory access.
What is the correct way to declare a String variable in Kotlin?
Aname := "Alice"
Bvar name = 'Alice'
CString name = "Alice"
Dval name = "Alice"
Explain what immutability means for the String type in Kotlin and why it matters.
Think about what happens if you try to change a letter inside a String.
You got /3 concepts.
    Describe how you would add text to an existing String in Kotlin and what happens behind the scenes.
    Remember Strings are immutable, so adding text creates something new.
    You got /3 concepts.