0
0
Typescriptprogramming~20 mins

Generic array syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using generic array syntax?
Consider the following TypeScript code snippet. What will be logged to the console?
Typescript
const numbers: Array<number> = [1, 2, 3];
const doubled = numbers.map(x => x * 2);
console.log(doubled);
ATypeError at runtime
B[2, 4, 6]
C[1, 2, 3]
DSyntaxError during compilation
Attempts:
2 left
💡 Hint
Think about what the map function does to each element in the array.
Predict Output
intermediate
1:30remaining
What is the type of this generic array in TypeScript?
Given this declaration, what is the type of the variable 'words'?
Typescript
let words: Array<string> = ['hello', 'world'];
AArray of strings
BArray of numbers
CTuple of strings
DArray of any type
Attempts:
2 left
💡 Hint
Look at the generic type inside the angle brackets.
🔧 Debug
advanced
2:00remaining
Why does this TypeScript code cause a compilation error?
Examine the code below. Why does it cause a TypeScript compilation error?
Typescript
const mixed: Array<number> = [1, 2, '3'];
ABecause '3' is a string, not a number, violating the generic type Array<number>
BBecause arrays cannot be declared with generic syntax
CBecause the array is missing a type annotation
DBecause the array is empty
Attempts:
2 left
💡 Hint
Check the types of all elements inside the array.
Predict Output
advanced
2:00remaining
What is the output of this TypeScript code using generic array syntax with union types?
What will be the output of this code snippet?
Typescript
const items: Array<number | string> = [1, 'two', 3];
const filtered = items.filter(item => typeof item === 'number');
console.log(filtered);
A['two']
B[1, 'two', 3]
C[1, 3]
DTypeError at runtime
Attempts:
2 left
💡 Hint
The filter keeps only numbers from the array.
🧠 Conceptual
expert
2:30remaining
How many elements are in the resulting array after this TypeScript code runs?
Consider this code snippet. How many elements does the 'result' array contain after execution?
Typescript
function createArray<T>(length: number, value: T): Array<T> {
  return new Array(length).fill(value);
}
const result = createArray<string>(3, 'hi');
AUndefined
B1
C0
D3
Attempts:
2 left
💡 Hint
The fill method fills the array with the given value for the specified length.