MCQs on Working with Arrays and Tuples | TypeScript

Dive into TypeScript with these 30 multiple-choice questions focusing on arrays and tuples. Understand the nuances of typed arrays, multi-dimensional structures, and tuple use cases for efficient coding.


Topic 1: Arrays (Typed and Multi-Dimensional) (20 Questions)

  1. How do you declare a typed array of numbers in TypeScript?
    A) let arr: number[];
    B) let arr = [number];
    C) let arr: Array;
    D) let arr: Array<number>;
  2. Which of the following is the correct way to define an array of strings?
    A) let arr: string;
    B) let arr: string[];
    C) let arr: [string];
    D) let arr: Array;
  3. How would you access the second element in an array named numbers?
    A) numbers[0]
    B) numbers[1]
    C) numbers(1)
    D) numbers{1}
  4. Which method is used to add an element at the end of an array?
    A) array.push()
    B) array.pop()
    C) array.shift()
    D) array.unshift()
  5. How can you remove the last element from an array in TypeScript?
    A) array.pop()
    B) array.shift()
    C) array.push()
    D) array.splice()
  6. What does the map() function do in TypeScript arrays?
    A) Modifies the original array
    B) Creates a new array with transformed elements
    C) Finds a single element
    D) Sorts the array
  7. How do you declare a multi-dimensional array in TypeScript?
    A) let arr: number[][];
    B) let arr = [[], []];
    C) let arr: [number, number];
    D) let arr: Array[][];
  8. What is the output of console.log([1, 2, 3].length);?
    A) 0
    B) 2
    C) 3
    D) Undefined
  9. How can you combine two arrays in TypeScript?
    A) array1.concat(array2)
    B) array1 + array2
    C) array1.append(array2)
    D) array1.join(array2)
  10. Which method checks if an array contains a specific element?
    A) array.has()
    B) array.includes()
    C) array.indexOf()
    D) array.contains()
  11. How do you convert an array to a string in TypeScript?
    A) array.toString()
    B) array.join(',')
    C) array.convert()
    D) array.stringify()
  12. What is the method to sort an array of numbers in ascending order?
    A) array.sort()
    B) array.reverse()
    C) array.order()
    D) array.ascend()
  13. Which method can be used to iterate over every element in an array?
    A) array.map()
    B) array.forEach()
    C) array.find()
    D) array.filter()
  14. How do you create an empty array in TypeScript?
    A) let arr = {};
    B) let arr = new Array();
    C) let arr = [];
    D) Both B and C
  15. What does the filter() method return?
    A) The first matching element
    B) An array of matching elements
    C) The length of the array
    D) A boolean value
  16. Which of the following creates a tuple of two elements?
    A) [string, number]
    B) (string, number)
    C) string[]
    D) {string, number}
  17. Can an array in TypeScript hold multiple data types?
    A) Yes, with any[]
    B) No, arrays are strictly typed
    C) Only with special syntax
    D) Yes, by default
  18. How would you find the index of a value in an array?
    A) array.findIndex()
    B) array.search()
    C) array.lookup()
    D) array.indexOf()
  19. What method is used to remove the first element of an array?
    A) array.pop()
    B) array.shift()
    C) array.remove()
    D) array.delete()
  20. Which function can check if every element in an array meets a condition?
    A) array.every()
    B) array.all()
    C) array.each()
    D) array.some()

Topic 2: Tuples (Definition and Use Cases) (10 Questions)

  1. How do you declare a tuple in TypeScript?
    A) let tuple: [string, number];
    B) let tuple = {string, number};
    C) let tuple = (string, number);
    D) let tuple = [string, string];
  2. What happens if you try to add an extra element to a fixed-size tuple?
    A) Error: Tuple length is fixed
    B) The element is added without any issues
    C) Tuple converts to a normal array
    D) The element is ignored
  3. How can you access the second element of a tuple let t: [number, string] = [5, 'TypeScript'];?
    A) t[1]
    B) t(1)
    C) t[2]
    D) t.get(1)
  4. Can tuples in TypeScript have optional elements?
    A) Yes, using ?
    B) No, tuples have fixed lengths
    C) Only in arrays
    D) Yes, with an extra type definition
  5. Which of the following correctly defines a tuple with mixed types?
    A) let tuple: [number, string] = [42, 'hello'];
    B) let tuple = (42, 'hello');
    C) let tuple: [string, string] = ['a', 'b'];
    D) let tuple: number[] = [42, 43];
  6. What is a common use case for tuples in TypeScript?
    A) For storing key-value pairs
    B) To represent a fixed set of values of different types
    C) For storing unlimited elements
    D) To sort elements
  7. How do you update the value of a tuple element?
    A) tuple[index] = newValue;
    B) tuple.update(newValue);
    C) tuple.push(newValue);
    D) tuple.add(newValue);
  8. Which function would you use to check if a tuple is an instance of an array?
    A) Array.isArray(tuple)
    B) tuple.isArray()
    C) typeof tuple === 'array'
    D) tuple.includes()
  9. Can you use array methods on tuples in TypeScript?
    A) Yes, tuples are a type of array
    B) No, tuples are completely different
    C) Only if cast to an array
    D) Only on numeric tuples
  10. What is the output of the following code?typescriptCopy codelet t: [number, string] = [10, 'hello']; console.log(t.length); A) 1
    B) 2
    C) 3
    D) undefined

Answer Key

QnoAnswer (Option with the text)
1A) let arr: number[];
2B) let arr: string[];
3B) numbers[1]
4A) array.push()
5A) array.pop()
6B) Creates a new array with transformed elements
7A) let arr: number[][];
8C) 3
9A) array1.concat(array2)
10B) array.includes()
11A) array.toString()
12A) array.sort()
13B) array.forEach()
14D) Both B and C
15B) An array of matching elements
16A) [string, number]
17A) Yes, with any[]
18D) array.indexOf()
19B) array.shift()
20A) array.every()
21A) let tuple: [string, number];
22A) Error: Tuple length is fixed
23A) t[1]
24A) Yes, using ?
25A) let tuple: [number, string] = [42, ‘hello’];
26B) To represent a fixed set of values of different types
27A) tuple[index] = newValue;
28A) Array.isArray(tuple)
29A) Yes, tuples are a type of array
30B) 2

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top