Javascript objective interview practice question on arrays. In these objective questions i covered mostly used javascript array methods such push,pop,slice,shift,sort etc.
Javascript Interview Questions on Arrays
Question 1. What’s the correct syntax for Array Declaration.
a) var city = new Array(“delhi”, “agra”, “akot”, “aligarh”);
b) var city = [“delhi”, “agra”, “akot”, “aligarh”];
c) Both a and b
d) None of them
Javascript Objective Interview Questions on Arrays
Question 2 What’s the output of the following code.
1 2 3 |
var city = new Array("delhi", "agra", "akot", "aligarh"); city.push('palampur'); console.log(city); |
a) [ “palampur” , “delhi”, “agra”, “akot”, “aligarh”]
b) [“delhi”, “agra”, “akot”, “aligarh”, “palampur”]
c) [“delhi”, “agra”, “akot”, “palampur”, “aligarh”]
d) None of them
Question 3 Output of the following code is.
1 2 |
var city = new Array("delhi", "agra", "akot", "aligarh","palampur"); console.log(city.slice(2)); |
a) [“delhi”,”agra”,”akot”]
b) [“akot”, “aligarh”, “palampur”]
c) [“delhi”,”agra”,”akot” , “aligarh”,”palampur”]
d) None
Question 4
1 2 |
var city = new Array("delhi", "agra", "akot", "aligarh","palampur"); console.log(city.shift()); |
a) agra
b) akot
c) delhi
d) aligarh
Question 5
1 2 |
var city = new Array("delhi", "agra", "akot", "aligarh","palampur"); console.log(city.pop()); |
a) delhi
b) palampur
c) agra
d) akot
Question 6
1 2 |
var city = ["delhi", "agra", "akot", "aligarh"]; console.log(city.sort()); |
a) [“agra”, “akot”, “aligarh”, “delhi”]
b) [“delhi”, “agra”, “akot”, “aligarh”]
c) [“aligarh”, “agra”, “akot”, “delhi”]
d) [“akot”, “agra”, “delhi”, “aligarh”]
Explanation and Answers
Question 1
Answer C) Both a and b.
You can declare an array in javascript by using any of the below method. For explanation you check post on Javascript Array Methods.
1 2 |
var city = new Array("delhi", "agra", "akot", "aligarh"); var city = ["delhi", "agra", "akot", "aligarh"]; |
Question 2
Answer b.
push() method in javascript add a new item at the end of an array.
Question 3
Answer b) [“akot”, “aligarh”, “palampur”]
Question 4
Answer C)
shift method removes first item of an array.
Question 5
Answer b)
pop() method removes the last element of an array.
Question 6
Answer a)