A string is a sequence of characters used to represent text. It can be thought of as a list or array of characters.
Click to reveal answer
intermediate
How do strings differ in memory representation between languages like C and JavaScript?
In C, strings are arrays of characters ending with a special null character '\0'. In JavaScript, strings are immutable sequences of UTF-16 code units, managed automatically.
Click to reveal answer
beginner
What does it mean that strings are immutable in languages like JavaScript and Python?
Immutable means once a string is created, it cannot be changed. Any modification creates a new string instead of altering the original.
Click to reveal answer
intermediate
How do Unicode and encoding affect string handling across languages?
Different languages handle Unicode and encoding differently. Some use UTF-8, others UTF-16 or UTF-32 internally, affecting how characters are stored and counted.
Click to reveal answer
advanced
Why is string indexing sometimes tricky in languages with Unicode support?
Because some characters use multiple code units (like emojis), indexing by code units may not correspond to visible characters, causing confusion.
Click to reveal answer
In C, how is the end of a string marked?
AWith a null character '\0'
BWith a space character ' '
CWith a newline character '\n'
DWith a special length prefix
✗ Incorrect
C strings end with a null character '\0' to mark the end.
What does it mean that strings are immutable in JavaScript?
AStrings are always empty
BYou cannot change the string after creation
CStrings are stored as arrays
DStrings can be changed anytime
✗ Incorrect
Immutable means strings cannot be changed after creation; changes create new strings.
Which encoding does JavaScript use internally for strings?
AUTF-8
BASCII
CUTF-16
DUTF-32
✗ Incorrect
JavaScript uses UTF-16 encoding internally for strings.
Why can indexing strings be tricky with Unicode characters?
ABecause strings are mutable
BBecause strings are always empty
CBecause strings have no length
DBecause some characters use multiple code units
✗ Incorrect
Some Unicode characters like emojis use multiple code units, making indexing by code units tricky.
In Python, strings are:
AImmutable sequences of characters
BMutable sequences of characters
CArrays of bytes
DNull-terminated arrays
✗ Incorrect
Python strings are immutable sequences of characters.
Explain how strings are stored and handled differently in C and JavaScript.
Think about how the end of a string is marked and how changes affect the string.
You got /4 concepts.
Describe why Unicode support makes string indexing more complex.
Consider how visible characters may not match internal storage units.
You got /4 concepts.
Practice
(1/5)
1. In TypeScript, what is the type of a string when you use single quotes like 'hello' compared to double quotes like "hello"?
easy
A. Both are of type string and behave the same
B. Single quotes create a character, double quotes create a string
C. Single quotes create a string, double quotes create a character
D. Single quotes are invalid syntax in TypeScript
Solution
Step 1: Understand string literals in TypeScript
TypeScript treats both single and double quotes as string delimiters, so both create string values.
Step 2: Confirm no character type in TypeScript
Unlike some languages, TypeScript does not have a separate character type; single quotes do not create characters.
Final Answer:
Both are of type string and behave the same -> Option A
Quick Check:
Single and double quotes create strings = A [OK]
Hint: Single and double quotes both create strings in TypeScript [OK]
Common Mistakes:
Thinking single quotes create characters
Confusing TypeScript with languages like C
Assuming single quotes are invalid
2. Which of the following is the correct way to declare a multi-line string in TypeScript?
easy
A. Using double quotes with + operator: const s = "line1" + "line2";
B. Using single quotes with \n: const s = 'line1\nline2';
C. Using backticks: const s = `line1\nline2`;
D. Using triple quotes: const s = '''line1\nline2''';
Solution
Step 1: Identify multi-line string syntax in TypeScript
TypeScript supports template literals using backticks (`) which allow multi-line strings directly.
Step 2: Check other options for correctness
Single and double quotes require explicit \n for new lines; triple quotes are invalid in TypeScript.
Final Answer:
Using backticks: const s = `line1\nline2`; -> Option C
Quick Check:
Backticks allow multi-line strings = A [OK]
Hint: Use backticks for multi-line strings in TypeScript [OK]
Common Mistakes:
Using triple quotes like Python
Expecting single or double quotes to support multi-line directly
Forgetting to escape newlines in single/double quotes
3. What will be the output of the following TypeScript code?
const s = `Hello\nWorld`;
console.log(s.length);
medium
A. Error
B. 12
C. 10
D. 11
Solution
Step 1: Understand the string content
The string is a template literal with an actual newline character between Hello and World, not the two-character escape sequence.
Step 2: Count characters including the newline
Characters: H(1) e(2) l(3) l(4) o(5) \n is a single newline character (counts as 1), W(6) o(7) r(8) l(9) d(10). So length is 11.
Final Answer:
11 -> Option D
Quick Check:
Template literals interpret \n as newline character = A [OK]
Hint: Template literals interpret \n as a single newline character [OK]
Common Mistakes:
Assuming \n is two characters in length
Counting only visible characters
Expecting runtime error due to escape
4. Identify the error in this TypeScript code snippet:
const greeting = 'Hello;
console.log(greeting);
medium
A. Missing closing single quote for the string
B. Incorrect use of backticks instead of single quotes
C. Missing semicolon after console.log statement
D. No error, code runs fine
Solution
Step 1: Check string syntax
The string starts with a single quote but does not have a closing single quote before the newline.