Array Interview Questions
Array Interview Questions
Answer: Array is a type of data structure.It is used to store more than one
value.that's why we need arrays.
Answer: Arrays are typically defined with square brackets with the size of
the arrays as its argument.
Q5. How to find how many elements are there in the array
Answer: Using length property.
Q6.Which data type gives the length?
var ar=[1,2,3,4,5];
console.log(ar[0]);
Answer: 1
var ar=[1,2,3,4,5];
console.log(ar[ar.length-1]);
Answer: 5
var ar=[12,89,90,34,85,14];
console.log(typeof(ar));
Answer: object
Q14. For the Given array: [23,34,54,0,4,7,9,19,24,91] print all the Array
values using a for loop?
var ar=[23,34,54,0,4,7,9,19,24,91];
for(var i=0;i<=ar.length-1;i++){
console.log(ar[i])
}
var ar=[23,34,54,0,4,7,9,19,24,91];
for(var i=1;i<=ar.length-1;i++){
console.log(ar[i])
}
var ar=[23,34,54,0,4,7,9,19,24,91
console.log(ar.length);
Correct Answer: 10
Q17. For the Given array: [23,34,54,0,4,7,9,19,24,91] Print all elements
in an array except the last element using for loop?
var ar=[23,34,54,0,4,7,9,19,24,91];
for(var i=0;i<ar.length-1;i++){
console.log(ar[i])
}
var ar=[23,34,54,0,4,7,9,19,24,91];
for(var i=ar.length-1;i>=0;i--){
console.log(ar[i])
}
var ar=[23,34,54,0,4,7,9,19,24,91];
for(var i=ar.length-4;i<=ar.length-1;i++){
console.log(ar[i])
}
Q20. For the Given array: [23,34,54,10,4,7] print values that are greater
than the given number. Given number: 20
var ar=[23,34,54,0,4,7,9,19,24,91,20];
for(var i=0;i<=ar.length-1;i++){
if(ar[i] > 20){
console.log(ar[i])
}
}
var ar=[23,34,54,10,4,7,19,24,56,89];
for(var i=0;i<=ar.length-1;i++){
if(ar[i] % 2 === 0){
console.log(ar[i])
}
}
var ar=[23,-34,-54,10,-4,7,-4,9,-19,40];
for(var i=0;i<=ar.length-1;i++){
if(ar[i] > 0){
console.log(ar[i])
}
}
Correct Answer: 23,10,7,9,40
Q23. For the Given array: [23,34,54,10,4,7,4,9,29,40] Print only
multiples of 2 AND 3 from an array using a for loop?
var ar=[23,34,54,10,4,7,4,9,29,40];
for(var i=0;i<=ar.length-1;i++){
if(ar[i] % 3 === 0 && ar[i] % 2 === 0){
console.log(ar[i])
}
}
Correct Answer: 54
var ar=[23,34,54,10,4,7,4,9,29,40];
for(var i=0;i<=ar.length-1;i++){
if(ar[i] % 3 === 0 || ar[i] % 2 === 0){
console.log(ar[i])
}
}
Correct Answer: 34,54,10,4,4,9,40
Answer:
Q27. For the Given array: [23,34,54,10,4,7] search if the given number
is there in an array or not. Given number: 34
Answer:
Answer:
Correct Answer: 2
Answer:
Correct Answer: 22
30. For the given array: [2,4,8,9,12,23,45,23,43,14,89,9,8,10].print the
unique elements.(Remove duplicate elements).
Answers:
var ar=[2,4,8,9,12,23,45,23,43,14,89,9,8,10];
var br=ar.filter((value,index,arr)=>ar.indexOf(value)===index)
console.log(br)
//another way
var ar=[2,4,8,9,12,23,45,23,43,14,89,9,8,10];
var br=[...new Set(ar)];
console.log(br);
Correct Answer: [2, 4, 8, 9, 12, 23, 45, 43, 14, 89, 10].
Answer:
var ar=[2,4,8,9,12,23,45,23,43,14,89,8,10];
var num=9;
for(var i=0;i<=ar.length-1;i++){
if(num===ar[i]){
console.log(i)
}
}
Correct Answer: 3