What is the basic type used to represent text in TypeScript?
The string type is used to represent text in TypeScript. It holds sequences of characters.
Click to reveal answer
beginner
Are strings in TypeScript mutable or immutable?
Strings in TypeScript are immutable. Once created, their content cannot be changed directly.
Click to reveal answer
beginner
How can you create a multi-line string in TypeScript?
Use template literals with backticks (``) to create multi-line strings easily.
Click to reveal answer
beginner
What happens when you use the + operator with strings in TypeScript?
The + operator concatenates (joins) two or more strings into one string.
Click to reveal answer
intermediate
How does TypeScript handle string type checking?
TypeScript enforces that variables declared as string can only hold string values, helping catch errors at compile time.
Click to reveal answer
Which of the following is the correct way to declare a string variable in TypeScript?
Alet name: string = 'Alice';
Blet name = 123;
Clet name: number = 'Alice';
Dlet name: boolean = true;
✗ Incorrect
Option A correctly declares a variable 'name' with type string and assigns a string value.
What will the following code output? let greeting = 'Hello'; greeting[0] = 'J'; console.log(greeting);
AJello
BError
CHello
Dundefined
✗ Incorrect
Strings are immutable, so changing a character by index does nothing. The original string 'Hello' remains.
How do you create a string that spans multiple lines in TypeScript?
AUsing double quotes with \n
BUsing backticks (template literals)
CUsing single quotes with \n
DUsing parentheses
✗ Incorrect
Backticks allow multi-line strings directly without needing escape characters.
What is the result of 'Hello' + ' ' + 'World' in TypeScript?
A'Hello World'
BError
C'Hello+World'
D'HelloWorld'
✗ Incorrect
The + operator concatenates strings, so it joins them with the space in between.
Which of these is NOT a string method in TypeScript?
Aincludes()
BtoUpperCase()
Cslice()
Dpush()
✗ Incorrect
push() is an array method, not a string method.
Explain how strings behave in TypeScript regarding mutability and concatenation.
Think about whether you can change a letter inside a string directly and how to combine strings.
You got /3 concepts.
Describe how TypeScript helps ensure variables hold string values and what happens if you assign a different type.
Consider what TypeScript does before running your code to catch mistakes.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is the correct way to declare a string variable in TypeScript?
easy
A. let name: string = "Alice";
B. let name = 123;
C. let name: number = "Alice";
D. let name: boolean = true;
Solution
Step 1: Understand TypeScript string type declaration
In TypeScript, to declare a string variable, use let variableName: string = value; where value is text in quotes.
Step 2: Check each option's type and value
let name: string = "Alice"; uses string type and assigns a text value "Alice" correctly. Others assign wrong types or values.
Final Answer:
let name: string = "Alice"; -> Option A
Quick Check:
Correct string declaration = let name: string = "Alice"; [OK]
Hint: String variables use : string and quotes [OK]
Common Mistakes:
Assigning number or boolean to string type
Missing type annotation when required
Using wrong quotes for strings
2. Which of the following is the correct syntax to create a template string in TypeScript?
easy
A. let greeting = `Hello, ${name}!`;
B. let greeting = Hello, ${name}!;
C. let greeting = "Hello, ${name}!";
D. let greeting = 'Hello, ${name}!';
Solution
Step 1: Identify template string syntax
Template strings use backticks ` and allow embedding variables with ${variable}.
Step 2: Check each option's quotes and variable embedding
let greeting = `Hello, ${name}!`; correctly uses backticks and ${name} syntax. Options A and C use single or double quotes which do not support variable embedding. let greeting = Hello, ${name}!; misses quotes entirely.
Final Answer:
let greeting = `Hello, ${name}!`; -> Option A
Quick Check:
Template strings use backticks = let greeting = `Hello, ${name}!`; [OK]
Hint: Use backticks ` for template strings with ${} [OK]
Common Mistakes:
Using single or double quotes for templates
Forgetting backticks
Not wrapping string in quotes
3. What is the output of the following TypeScript code?