JavaScript Arrays

Last Updated :
Discuss
Comments

Question 1

How do you loop through an array in JavaScript?
 

  • A

    for (i = 0; i < arr.length; i++)

  • B

    while (i < arr.length)

  • C

    do while (i < arr.length)

  • D

    for each (element in arr)

Question 2

Which of the following statements creates a new empty array?

  • A

    let arr = [ ];

  • B

    let arr = new Array(0);

  • C

    let arr = Array();

  • D

    All of the above

Question 3

How do you access an element in an array?

  • A

    By using curly braces { }

  • B

    By using square brackets [ ]

  • C

    By using parentheses ( )

  • D

    By using angle brackets < >

Question 4

Which of the following array methods can be used to add one or more elements to the end of an array?

  • A

    push()

  • B

    pop()

  • C

    shift()

  • D

    unshift()

Question 5

What is the output of the following code snippet?

JavaScript
let arr1 = [1, 2, 3];
let arr2 = arr1;
arr2.push(4);
console.log(arr1);


  • A

    [1, 2, 3]

  • B

    [1, 2, 3, 4]

  • C

    [2, 3, 4]

  • D

    None of the above

Question 6

Which of the following array methods can be used to remove the last element from an array?

  • A

    pop()

  • B

    shift()

  • C

    unshift()

  • D

    splice()

Question 7

Which of the following array methods can be used to remove one or more elements from an array and replace them with new elements?

  • A

    splice()

  • B

    slice()

  • C

    concat()

  • D

    push()

Question 8

What is the output of the following code snippet?

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = arr1.concat(arr2);
console.log(arr3);

  • A

    [1, 2, 3, 4, 5, 6]

  • B

    [4, 5, 6, 1, 2, 3]

  • C

    [1, 4, 2, 5, 3, 6]

  • D

    None of the above

Question 9

What is the output of the following code snippet?
const arr = ["apple", "banana", "orange"];
const newArr = arr.slice(1, 2);

console.log(newArr);

  • A

    ["apple", "banana"]

  • B

    ["banana", "orange"]

  • C

    ["banana"]

  • D

    ["orange"]

Question 10

What is the difference between slice() and splice() array methods?

  • A

    slice() creates a new array, while splice() modifies the original array.

  • B

    splice() creates a new array, while slice() modifies the original array.

  • C

    slice() removes elements from the beginning of the array, while splice() removes elements from the end of the array.

  • D

    splice() adds elements to the beginning of the array, while slice() adds elements to the end of the array.

There are 10 questions to complete.

Take a part in the ongoing discussion