A string is a sequence of characters used to store text. It can include letters, numbers, and symbols.
Click to reveal answer
intermediate
How are strings stored in memory?
Strings are stored as a sequence of characters in contiguous memory locations, often ending with a special marker like a null character '\0' to show the end.
Click to reveal answer
intermediate
What does 'immutable string' mean?
An immutable string means once created, the string cannot be changed. Any change creates a new string in memory.
Click to reveal answer
intermediate
Why is string immutability useful?
It helps avoid unexpected changes, makes strings safe to share, and can improve performance with caching.
Click to reveal answer
intermediate
What is the difference between a string and a character array?
A string is a higher-level concept representing text, often immutable, while a character array is a low-level collection of characters that can be changed.
Click to reveal answer
What marks the end of a string in many programming languages?
ATab character '\t'
BSpace character ' '
CNull character '\0'
DNewline character '\n'
✗ Incorrect
Many languages use the null character '\0' to indicate the end of a string in memory.
If strings are immutable, what happens when you change a character in a string?
AThe original string changes in place
BA new string is created with the change
CThe program crashes
DThe string becomes a character array
✗ Incorrect
Because strings are immutable, changing a character creates a new string with the change.
Which of these is NOT a property of strings in memory?
ACan be shared safely if immutable
BStored as contiguous characters
CMay end with a special marker
DAlways mutable
✗ Incorrect
Strings are often immutable, so they are not always mutable.
What is a key benefit of string immutability?
ASafer sharing between parts of a program
BLess memory usage
CFaster character changes
DStrings can be resized easily
✗ Incorrect
Immutability allows strings to be safely shared without risk of accidental changes.
Which data structure is a low-level way to store text that can be changed?
ACharacter array
BImmutable string
CInteger array
DBoolean list
✗ Incorrect
Character arrays store characters in memory and can be changed directly.
Explain how strings are stored in memory and why immutability matters.
Think about how text is saved and what happens when you try to change it.
You got /5 concepts.
Describe the difference between a string and a character array in terms of memory and mutability.
Compare how each stores characters and whether you can change them.
You got /5 concepts.
Practice
(1/5)
1. What does the term immutable mean when talking about strings in TypeScript?
easy
A. Once created, the string cannot be changed.
B. The string can be changed anytime.
C. The string is stored in a mutable array.
D. The string is stored as a number.
Solution
Step 1: Understand string immutability
Strings in TypeScript cannot be changed after creation. Any modification creates a new string.
Step 2: Compare options with definition
Only Once created, the string cannot be changed. correctly states that strings cannot be changed once created.
Final Answer:
Once created, the string cannot be changed. -> Option A
Quick Check:
Immutable means unchangeable = A [OK]
Hint: Remember: strings can't be changed after creation [OK]
Common Mistakes:
Thinking strings can be changed like arrays
Confusing mutable and immutable
Assuming strings are stored as numbers
2. Which of the following is the correct way to declare a string variable in TypeScript?
easy
A. let name: number = 'Alice';
B. let name = 123;
C. let name: string = 'Alice';
D. let name: string = 123;
Solution
Step 1: Check variable declaration syntax
TypeScript uses let variableName: type = value; syntax.
Step 2: Match type and value
let name: string = 'Alice'; declares a string variable with a string value correctly.
Final Answer:
let name: string = 'Alice'; -> Option C
Quick Check:
Correct string declaration = A [OK]
Hint: Match variable type with value type exactly [OK]
Common Mistakes:
Assigning number to string type
Missing type annotation
Using wrong syntax for declaration
3. What will be the output of the following TypeScript code?
let greeting = 'Hello';
greeting[0] = 'Y';
console.log(greeting);
medium
A. Yello
B. undefined
C. Error: Cannot assign to string index
D. Hello
Solution
Step 1: Understand string immutability in TypeScript
Strings cannot be changed by index assignment; such operations do not modify the string.
Step 2: Analyze the code behavior
Assigning greeting[0] = 'Y' does nothing; greeting remains 'Hello'.
Final Answer:
Hello -> Option D
Quick Check:
Strings immutable, no change = Hello [OK]
Hint: Strings can't be changed by index assignment [OK]
Common Mistakes:
Expecting first letter to change
Thinking string is mutable like array
Assuming error will be thrown
4. What best describes the result of the following TypeScript code snippet:
let str: string = 'Test';
str[1] = 'a';
console.log(str);
medium
A. No error, output is 'Test'
B. TypeError: Cannot assign to string index
C. SyntaxError: Invalid assignment
D. ReferenceError: str is not defined
Solution
Step 1: Check string mutability rules
Strings are immutable; assigning to an index does nothing.
Step 2: Analyze the code behavior
str[1] = 'a' does nothing; str remains 'Test', console.log outputs 'Test'. No error.
Final Answer:
No error, output is 'Test' -> Option A
Quick Check:
Strings immutable, index assignment ignored = 'Test' [OK]
Hint: Strings cannot be changed by index assignment [OK]
Common Mistakes:
Assuming no error and string changes
Confusing syntax error with runtime error
Expecting ReferenceError
5. Given the following code, what will be the output?
let s1 = 'abc';
let s2 = s1 + 'd';
console.log(s1);
console.log(s2);
Choose the correct output considering string immutability and memory representation.
hard
A. abc abc
B. abc abcd
C. abcd abcd
D. Error at runtime
Solution
Step 1: Understand string concatenation and immutability
Concatenating strings creates a new string; original strings remain unchanged.
Step 2: Trace variable values after operations
s1 remains 'abc'; s2 becomes 'abcd'.
Final Answer:
abc abcd -> Option B
Quick Check:
Original unchanged, new string created = abc and abcd [OK]
Hint: Concatenation creates new string; originals stay same [OK]
Common Mistakes:
Expecting s1 to change after concatenation
Thinking concatenation modifies original string
Assuming runtime error on concatenation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.