A string in C# is a sequence of characters stored as an object. It is immutable, meaning once created, it cannot be changed.
Click to reveal answer
beginner
What does it mean that strings are immutable in C#?
Immutability means that once a string is created, its content cannot be changed. Any modification creates a new string object.
Click to reveal answer
intermediate
How is a string stored in memory in C#?
Strings are stored as a sequence of UTF-16 characters in the managed heap. Each character uses 2 bytes, and the string object includes metadata like length.
Click to reveal answer
intermediate
What is the role of the string pool in C#?
The string pool is a special area in memory where identical string literals are stored once to save memory. When a string literal is used multiple times, it references the same object in the pool.
Click to reveal answer
intermediate
Why should you use StringBuilder instead of string for many modifications?
Because strings are immutable, modifying them repeatedly creates many new objects. StringBuilder allows efficient modifications by using a mutable buffer.
Click to reveal answer
What happens when you change a character in a C# string?
AThe string is deleted
BThe original string is modified in place
CThe string becomes mutable
DA new string object is created with the change
✗ Incorrect
Strings are immutable, so any change creates a new string object.
Where are string literals stored in C#?
AOn the stack
BIn the string pool in managed heap
CIn CPU registers
DIn unmanaged memory
✗ Incorrect
String literals are stored in the string pool, a special area in the managed heap.
How many bytes does each character in a C# string use?
A2 bytes
B4 bytes
C1 byte
DDepends on the character
✗ Incorrect
C# strings use UTF-16 encoding, so each character uses 2 bytes.
Why is StringBuilder preferred for many string changes?
AIt stores strings on the stack
BIt is immutable
CIt uses less memory by modifying in place
DIt automatically pools strings
✗ Incorrect
StringBuilder uses a mutable buffer to efficiently modify strings without creating new objects.
What does immutability of strings help with?
AThread safety and predictable behavior
BFaster string modifications
CReducing string length
DChanging characters directly
✗ Incorrect
Immutability ensures strings cannot change unexpectedly, making code safer and easier to understand.
Explain how strings are stored and managed in memory in C#.
Think about how characters are encoded and where string objects live.
You got /4 concepts.
Describe why strings are immutable in C# and how this affects string manipulation.
Consider what happens when you try to change a string.
You got /4 concepts.
Practice
(1/5)
1. What does the string type in C# represent in memory?
easy
A. A list of integers representing ASCII codes
B. A sequence of characters stored as Unicode values
C. A mutable array of bytes
D. A pointer to a memory address without data
Solution
Step 1: Understand string storage in C#
C# strings store characters as Unicode, allowing international text.
Step 2: Compare options with string behavior
Only A sequence of characters stored as Unicode values correctly describes strings as sequences of Unicode characters.
Final Answer:
A sequence of characters stored as Unicode values -> Option B
Quick Check:
string = Unicode characters [OK]
Hint: Remember C# strings are Unicode sequences, not byte arrays [OK]
Common Mistakes:
Thinking strings are mutable arrays
Confusing ASCII with Unicode
Assuming strings are just pointers
2. Which of the following is the correct way to declare and initialize a string in C#?
easy
A. string s = "hello";
B. string s = hello;
C. string s = 'hello';
D. string s = "hello";
char[] arr = s;
Solution
Step 1: Check string literal syntax
In C#, string literals use double quotes, so "hello" is correct.
Step 2: Validate each option
string s = 'hello'; uses single quotes (for chars), B misses quotes, D tries to assign string to char array directly (invalid).
Final Answer:
string s = "hello"; -> Option A
Quick Check:
Strings use double quotes in C# [OK]
Hint: Use double quotes for strings, single quotes for chars [OK]
Common Mistakes:
Using single quotes for strings
Forgetting quotes around text
Assigning string directly to char array
3. What will be the output of the following C# code?