Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

String Basics and Memory Representation in DSA Typescript - Interactive Practice

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

Complete the code to get the length of the string.

DSA Typescript
const str = "hello";
const length = str.[1];
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method like length() instead of the property length.
Confusing length with size or count.
2fill in blank
medium

Complete the code to get the character at index 2.

DSA Typescript
const str = "world";
const char = str.[1](2);
Drag options to blanks, or click blank then click option'
AcharCodeAt
Bslice
CcharAt
Dsubstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using charCodeAt which returns a number, not a character.
Using substring or slice which return substrings, not single characters.
3fill in blank
hard

Fix the error in the code to convert the string to uppercase.

DSA Typescript
const str = "test";
const upper = str.[1]();
Drag options to blanks, or click blank then click option'
Atouppercase
BtoUpperCase
Cuppercase
DtoUpper
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing like 'touppercase'.
Using incomplete method names like 'toUpper'.
4fill in blank
hard

Fill both blanks to create a substring from index 1 to 4.

DSA Typescript
const str = "example";
const sub = str.[1]([2], 4);
Drag options to blanks, or click blank then click option'
Asubstring
B1
C2
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using slice with wrong start index.
Confusing start and end indexes.
5fill in blank
hard

Fill all three blanks to create a new string with the first letter capitalized.

DSA Typescript
const str = "hello";
const capitalized = str.[1](0, 1).[2]() + str.[3](1);
Drag options to blanks, or click blank then click option'
Asubstring
BtoUpperCase
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using slice instead of substring for the first part.
Not converting the first letter to uppercase.
Using substring for the last part instead of slice.

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

  1. Step 1: Understand string immutability

    Strings in TypeScript cannot be changed after creation. Any modification creates a new string.
  2. Step 2: Compare options with definition

    Only Once created, the string cannot be changed. correctly states that strings cannot be changed once created.
  3. Final Answer:

    Once created, the string cannot be changed. -> Option A
  4. 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

  1. Step 1: Check variable declaration syntax

    TypeScript uses let variableName: type = value; syntax.
  2. Step 2: Match type and value

    let name: string = 'Alice'; declares a string variable with a string value correctly.
  3. Final Answer:

    let name: string = 'Alice'; -> Option C
  4. 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

  1. Step 1: Understand string immutability in TypeScript

    Strings cannot be changed by index assignment; such operations do not modify the string.
  2. Step 2: Analyze the code behavior

    Assigning greeting[0] = 'Y' does nothing; greeting remains 'Hello'.
  3. Final Answer:

    Hello -> Option D
  4. 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

  1. Step 1: Check string mutability rules

    Strings are immutable; assigning to an index does nothing.
  2. Step 2: Analyze the code behavior

    str[1] = 'a' does nothing; str remains 'Test', console.log outputs 'Test'. No error.
  3. Final Answer:

    No error, output is 'Test' -> Option A
  4. 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

  1. Step 1: Understand string concatenation and immutability

    Concatenating strings creates a new string; original strings remain unchanged.
  2. Step 2: Trace variable values after operations

    s1 remains 'abc'; s2 becomes 'abcd'.
  3. Final Answer:

    abc
    abcd
    -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard