Arrays and its meyhod MCQS IMPORTANT
What is the output of the following code?
Const arr = [1, 2, 3, 4, 5];
Const result = arr.reduce((acc,current) => acc + current,0);
Console.log(result);
A) 15
B) 20
C) 25
D) 30
Correct answer
A) 15
2
What is the output of following code?
Const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
Const result = arr.filter(num => num % 2 === 0);
Console.log(result);
A) [1,3,5]
B) [1, 2, 3, 4, 5]
C) [2, 4, 6, 8, 10]
D) [2, 5, 6, 9, 4]
Correct answer
B) [2, 4, 6, 8, 10]
What is the output of following code?
Const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Const result = arr.slice(2,7);
Console.log(result);
A) [1,3,6]
B) [3,4,5,6,7]
C) [3,4,5,6]
D) [2,3,4,5,6]
Correct answer
C) [3,4,5,6,7]
What is the output of the following code?
Const arr = [1, 2, 3, 4, 5];
Const result = arr.filter(num => num % 2 === 0).map(num => num * 2).reduce((acc,current)=> acc + current, 0)
Console.log(result);
A) 20
B) 12
C) 30
D) 40
Correct answer
B) 12
What is the output of following code?
Const arr = [“a”,”b”,”c”];
Const result = arr.join(“ -!- “);
Const result1 = arr.join(“ =/= “);
Console.log(result);
Console.log(result1);
A) A,b,c
B) [a =/= b =/= c=/= ] 2 [a -!- b -!- c -!- ]
C) [a -!- b -!- c -!- ] [a =/= b =/= c=/= ]
D) [a , b , c ]
Correct answer
B) [a -!- b -!- c -!- ] [a =/= b =/= c=/= ]
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Console.log(arr.includes(6));
A) true
B) false
C) 0
C) -1
Correct answer
B) false
Which method is use to concatenate two or more array?
A) merge()
B) join()
C) concat()
D) push()
Correct answer
D) Concat()
What is the output of following code?
Const arr = [1,2,3,4,5]
Const result = arr.map(num => num * 2)
Console.log(result);
A) [1, 2, 3]
B) [2, 4, 6, 8, 10]
C) [2, 4, 6, 8]
D) [3, 4, 5]
Correct answer
B) [2, 4, 6, 8, 10]
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.unshift(0, -1);
Arr.pop();
Console.log(arr);
//* A) [0, -1, 1, 2, 3, 4]
//* A) [-1, 0, 1, 2, 3, 4]
//* A) [0, -1, 1, 2, 3, 5]
//* A) [-1, 0, 1, 2, 4, 5]
Correct answer
//* A) [0, -1, 1, 2, 3, 4]
What is the output of following code?
Const arr1 = [1,2]
Const arr2 = [9,10]
Const result = arr1.concat(arr2)
Console.log(result);
A) [1, 9, 2, 10]
B) [1, 2, 3, 4]
C) [1, 2, 9, 10]
D) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Correct answer
C) [1, 2, 9, 10]
What is the output of following code?
Const arr1 = [1, 2]
Const arr2 = [3, 4]
Const result =arr1.concat(arr2).join(“ – “)
Console.log(result)
A) [1,2,3,4]
B) [1 – 2 – 3 – 4]
C) 1 – 2 – 3 – 4
D) [ “1 – 2 – 3 – 4”]
Correct answer
B) 1 – 2 – 3 – 4
Which element is used to transform each element of an array?
A) filter()
B) reduce()
C) map()
D) join()
Correct answer
C) Map()
Which is the output of the following code ?
Const names:string[] = [“a”,”b”,”c”,”d”,”e”,”c”,”e”,”f”,”c”]
Const ans1:boolean = names.includes(“c”,3,)
Const ans2:boolean = names.includes(“c”,7,)
Console.log(ans1);
Console.log(ans2);
A) false false
B) true true
C) false true
D) True false
Correct answer
B) true true
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.push(6, 7);
Arr.shift();
Console.log(arr);
A) [2, 3, 4, 5, 6, 7]
B) [6, 7, 2, 3, 4, 5]
C) [1, 3, 4, 5, 6, 7]
E) [7, 2, 3, 4, 5, 6]
Correct answer
A) [2, 3, 4, 5, 6, 7]
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.fill(0, 1, 3);
Console.log(arr);
A) [1, 0, 0, 0, 5]
B) [1, 0, 3, 4, 5]
C) [1, 0, 0, 4, 5]
D) [1, 2, 3, 4, 0]
Correct answer
B) [1, 0, 0, 4, 5]
What is the purpose of the fill() method in an array?
A) To add elements to the end of an array
B) To remove elements from an array
C) To fill an array with a specified value….
D) To sort an array
Correct answer
C) To fill an array with a specified value….
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Console.log(arr.includes(3));
A) true
B) false
C) 0
D) -1
Correct answer
A) True
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.push(6, 7);
Arr.shift();
Arr.splice(2, 1);
Console.log(arr);
A) [2, 3, 5, 6, 7]
B) [2, 4, 5, 6, 7]
C) [2, 3, 6, 7]
D) [7, 2, 3, 5, 6]
Correct answer
A) [2, 3, 5, 6, 7]
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.unshift(0, -1);
Arr.splice(2, 2);
Console.log(arr);
A) [-1, 0, 1, 4, 5]
B) [0, -1, 4, 5]
C) [0, -1, 1, 2, 5]
D) [0, -1, 3, 4, 5] right option
What is the output of the following code:
Let arr = [1, 2, 3, 4, 5];
Arr.slice(1, 3);
Console.log(arr);
A) [1, 2, 3, 4, 5]
B) [2, 3]
C) [1, 4, 5]
D) [1, 2, 4, 5]
Correct answer
B) [1, 4, 5]
Loop QUIZ PRACTICE
1. What is the purpose of the while loop in TypeScript?
a) To execute a block of code repeatedly while a condition is true
b) To execute a block of code only once
c) To skip a block of code
d) To end a program
Answer: a) To execute a block of code repeatedly while a condition is true
2. What is the difference between while and do-while loops in TypeScript?
a) while loops check the condition before executing the loop body, while do-while loops check the condition after executing the loop body
b) while loops execute the loop body at least once, while do-while loops may not execute the loop body at all
c) while loops are used for conditional statements, while do-while loops are used for unconditional statements
d) There is no difference between while and do-while loops in TypeScript
Answer: a) while loops check the condition before executing the loop body, while do-while loops check the condition after executing the loop body
3. What is the purpose of the for loop in TypeScript?
a) To execute a block of code repeatedly for a specified number of iterations
b) To execute a block of code only once
c) To skip a block of code
d) To end a program
Answer: a) To execute a block of code repeatedly for a specified number of iterations
4. What is the syntax for a basic for loop in TypeScript?
a) for (init; cond; incr) { ... }
b) for (init; cond; incr) => { ... }
c) for (init; cond; incr) -> { ... }
d) for (init; cond; incr) > { ... }
Answer: a) for (init; cond; incr) { ... }
5. What is the purpose of the break statement in a loop in TypeScript?
a) To exit the loop immediately
b) To skip to the next iteration of the loop
c) To execute the loop body again
d) To end the program
Answer: a) To exit the loop immediately
6. What is the purpose of the continue statement in a loop in TypeScript?
a) To exit the loop immediately
b) To skip to the next iteration of the loop
c) To execute the loop body again
d) To end the program
Answer: b) To skip to the next iteration of the loop
7. What is an infinite loop in TypeScript?
a) A loop that executes a block of code repeatedly for a specified number of iterations
b) A loop that executes a block of code repeatedly while a condition is true
c) A loop that executes a block of code repeatedly forever
d) A loop that executes a block of code only once
Answer: c) A loop that executes a block of code repeatedly forever
8. How do you create an infinite loop in TypeScript using a while loop?
a) while (true) { ... }
b) while (false) { ... }
c) while (cond) { ... }
d) while (init; cond; incr) { ... }
Answer: a) while (true) { ... }
9. How do you create an infinite loop in TypeScript using a for loop?
a) for (;;) { ... }
b) for (init; cond; incr) { ... }
c) for (init; true; incr) { ... }
d) for (init; false; incr) { ... }
Answer: a) for (;;) { ... }
10. What is the purpose of the label statement in a loop in TypeScript?
a) To specify a label for a loop
b) To specify a condition for a loop
c) To specify an initialization for a loop
d) To specify an increment for a loop
Answer: a) To specify a label for a loop