All Exams  >   Software Development  >   JavaScript for Web Development  >   All Questions

All questions of Arrays in JS for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(1, 3));
  • a)
    [2, 3]
  • b)
    [1, 2]
  • c)
    [3, 4]
  • d)
    [2, 4]
Correct answer is option 'A'. Can you explain this answer?

Tejas Ghosh answered
Output:
The output of the code snippet is [2, 3].

Explanation:
The code snippet is using the slice() method to extract a portion of the array arr starting from index 1 (inclusive) and ending at index 3 (exclusive).

Here are the steps performed by the slice() method:

1. The slice() method creates a new array by copying a portion of the original array.
2. The first argument of the slice() method specifies the starting index from where the portion should be extracted.
3. The second argument specifies the ending index (exclusive) up to which the portion should be extracted.
4. In the given code snippet, arr.slice(1, 3) extracts the elements from index 1 (inclusive) to index 3 (exclusive).
5. The elements at index 1 and index 2 are 2 and 3 respectively.
6. Therefore, the extracted portion of the array is [2, 3].

Visualization:
To better understand the code snippet, let's visualize the array and the portion extracted using the slice() method:

Original Array: [1, 2, 3, 4, 5]

slice(1, 3):

- Start extracting from index 1 (inclusive)
- Stop extracting at index 3 (exclusive)

Extracted Portion: [2, 3]

Summary:
The slice() method is used to extract a portion of an array. In the given code snippet, the portion extracted from the array [1, 2, 3, 4, 5] using slice(1, 3) is [2, 3].

What is the output of the following code snippet?
 
```javascript
let arr = [1, 2, 3, 4, 5];
let doubledArr = arr.map((num) => num * 2);
console.log(doubledArr);
```
  • a)
    [1, 2, 3, 4, 5]
  • b)
    [2, 4, 6, 8, 10]
  • c)
    [2, 4, 6, 8, 10, 12]
  • d)
    [1, 4, 9, 16, 25]
Correct answer is option 'B'. Can you explain this answer?

Devika Saha answered
Explanation:

1. Initial Array:
- The initial array `arr` is `[1, 2, 3, 4, 5]`.

2. Mapping Function:
- The `map()` function is used to create a new array by applying a function to each element of the original array.
- In this case, the mapping function `(num) => num * 2` multiplies each element of the original array by 2.

3. Applying Mapping Function:
- When the mapping function is applied to each element of the `arr`, we get the following results:
- 1 * 2 = 2
- 2 * 2 = 4
- 3 * 2 = 6
- 4 * 2 = 8
- 5 * 2 = 10

4. Doubled Array:
- The new array `doubledArr` is `[2, 4, 6, 8, 10]` after applying the mapping function.

5. Output:
- The `console.log(doubledArr)` statement prints the doubled array to the console.
Therefore, the output of the given code snippet is option B) [2, 4, 6, 8, 10].

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.shift());
  • a)
    1
  • b)
    5
  • c)
    4
  • d)
    undefined
Correct answer is option 'A'. Can you explain this answer?

KnowIT answered
The 'shift()' method removes the first element from the array and returns it. In this case, it removes 1 from the array and returns it.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.join("-"));
  • a)
    1,2,3,4,5
  • b)
    12345
  • c)
    1-2-3-4-5
  • d)
    1-2345
Correct answer is option 'C'. Can you explain this answer?

TeamUnknown answered
The 'join()' method converts all the elements of an array into a string and concatenates them, using the specified separator (in this case, "-").

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.pop());
  • a)
    1
  • b)
    5
  • c)
    4
  • d)
    undefined
Correct answer is option 'B'. Can you explain this answer?

KnowIT answered
The 'pop()' method removes the last element from the array and returns it. In this case, it removes 5 from the array and returns it.

What is the output of the following code?
var arr = [1, 2, 3];
console.log(arr.indexOf(3));
  • a)
    -1
  • b)
    0
  • c)
    1
  • d)
    2
Correct answer is option 'D'. Can you explain this answer?

Tanuja Mishra answered
The 'indexOf()' method returns the index of the first occurrence of the specified element in the array. In this case, 3 is at index 2.

What is the output of the following code snippet?
```javascript
let arr = [1, 2, 3, 4, 5];
let index = arr.findIndex((num) => num === 3);
console.log(index);
```
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    -1
Correct answer is option 'B'. Can you explain this answer?

Tom Tattle answered
The 'findIndex()' method returns the index of the first element in the array that satisfies the provided testing function. In this case, it returns the index of the element 3.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.concat([6, 7]));
  • a)
    [1, 2, 3, 4, 5]
  • b)
    [1, 2, 3, 4, 5, 6, 7]
  • c)
    [6, 7]
  • d)
    [2, 3, 4, 5, 6, 7]
Correct answer is option 'B'. Can you explain this answer?

KnowIT answered
The 'concat()' method is used to merge two or more arrays. It returns a new array that contains the elements of the original array followed by the elements of the provided arrays.

Chapter doubts & questions for Arrays in JS - JavaScript for Web Development 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Arrays in JS - JavaScript for Web Development in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev