Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  Assignment: Arrays in JavaScript

Assignment: Arrays in JavaScript | JavaScript for Web Development - Software Development PDF Download

Multiple Choice Questions (MCQs)


Q.1. Which of the following methods can be used to add elements to an array in JavaScript?
(a) push()
(b) pop()
(c) shift()
(d) unshift()

Ans. (a)

Q.2. What is the correct way to access the value at index 3 in an array named "myArray"?
(a) myArray(3)
(b) myArray[3]
(c) myArray{3}
(d) myArray.3

Ans. (b)

Q.3. Which of the following methods can be used to remove the first element from an array in JavaScript?
(a) push()
(b) pop()
(c) shift()
(d) unshift()

Ans. (c)

Q.4. What will the following code output?

let myArray = [1, 2, 3, 4, 5];

console.log(myArray.length);

(a) 5
(b) 4
(c) 6
(d) 0

Ans. (a)

Q.5. Which array method is used to combine two or more arrays in JavaScript?
(a) push()
(b) pop()
(c) concat()
(d) join()

Ans. (c)

Higher Order Thinking Questions (HOTS)


Q.1. Write a JavaScript function that takes an array of numbers as input and returns the sum of all the numbers in the array.

function sumArray(numbers) {

  let sum = 0;

  for (let i = 0; i < numbers.length; i++) {

    sum += numbers[i];

  }

  return sum;

}

Q.2. Explain the difference between the slice() and splice() methods in JavaScript arrays.

The slice() method returns a shallow copy of a portion of an array into a new array without modifying the original array. The splice() method, on the other hand, can be used to add or remove elements from an array, modifying the original array.

Q.3. Write a JavaScript function that takes an array of strings as input and returns a new array with the length of each string in the original array.

function getStringLengths(strings) {

  let lengths = [];

  for (let i = 0; i < strings.length; i++) {

    lengths.push(strings[i].length);

  }

  return lengths;

}

Q.4. Write a JavaScript function that takes two arrays as input and returns a new array containing only the elements that are common to both arrays.

function getCommonElements(array1, array2) {

  let commonElements = [];

  for (let i = 0; i < array1.length; i++) {

    if (array2.includes(array1[i])) {

      commonElements.push(array1[i]);

    }

  }

  return commonElements;

}

Q.5. Explain the concept of callback functions in JavaScript and provide an example of how they can be used with array methods.

In JavaScript, a callback function is a function that is passed as an argument to another function and is executed later when a certain condition is met or an event occurs.
Example:

let numbers = [1, 2, 3, 4, 5];

numbers.forEach(function(number) {

  console.log(number);

});

Fill in the Blanks

1. The index of the first element in an array is __________.

Ans. 0

2. The indexOf() method in JavaScript returns the index of the __________ occurrence of a specified element in an array.

Ans. first

3. The forEach() method in JavaScript is used to __________.

Ans. iterate over an array and perform a function on each element

4. The toString() method in JavaScript converts an array to a __________.

Ans. string

5. The reverse() method in JavaScript __________ the order of the elements in an array.

Ans. reverses

True or False


1. In JavaScript, an array can store elements of different data types. (True/False)

Ans. True

2. The length property of an array in JavaScript returns the number of elements in the array. (True/False)

Ans. True

3. The concat() method in JavaScript modifies the original array. (True/False)

Ans. False

4. The join() method in JavaScript combines the elements of an array into a string. (True/False)

Ans. True

5. The sort() method in JavaScript can be used to sort an array of numbers in descending order. (True/False)

Ans. False

Hands-On Questions


Q.1. Write a JavaScript function named findMax that takes an array of numbers as input and returns the maximum number in the array.

function findMax(numbers) {

  let max = numbers[0];

  for (let i = 1; i < numbers.length; i++) {

    if (numbers[i] > max) {

      max = numbers[i];

    }

  }

  return max;

}

Q.2. Write a JavaScript function named removeDuplicates that takes an array as input and returns a new array with duplicate elements removed.

function removeDuplicates(array) {

  return [...new Set(array)];

}

Q.3. Write a JavaScript function named rotateArray that takes an array and a number n as input and rotates the elements of the array n positions to the right.

function rotateArray(array, n) {

  n = n % array.length;

  let rotatedArray = array.slice(-n).concat(array.slice(0, -n));

  return rotatedArray;

}

Q.4. Write a JavaScript function named sumEvenIndices that takes an array of numbers as input and returns the sum of numbers at even indices (0, 2, 4, etc.).

function sumEvenIndices(numbers) {

  let sum = 0;

  for (let i = 0; i < numbers.length; i += 2) {

    sum += numbers[i];

  }

  return sum;

}

Q.5. Write a JavaScript function named findCommonElements that takes two arrays as input and returns a new array containing only the elements that are common to both arrays.

function findCommonElements(array1, array2) {

  let commonElements = [];

  for (let i = 0; i < array1.length; i++) {

    if (array2.includes(array1[i])) {

      commonElements.push(array1[i]);

    }

  }

  return commonElements;

}

The document Assignment: Arrays in JavaScript | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

past year papers

,

Viva Questions

,

shortcuts and tricks

,

ppt

,

practice quizzes

,

Exam

,

Important questions

,

mock tests for examination

,

Objective type Questions

,

Sample Paper

,

Assignment: Arrays in JavaScript | JavaScript for Web Development - Software Development

,

MCQs

,

Assignment: Arrays in JavaScript | JavaScript for Web Development - Software Development

,

pdf

,

video lectures

,

Extra Questions

,

study material

,

Free

,

Semester Notes

,

Assignment: Arrays in JavaScript | JavaScript for Web Development - Software Development

,

Previous Year Questions with Solutions

;