Q1. What is Array?
Answer: Array is a type of data structure.It is used to store more than one
value.that's why we need arrays.
Q2. What is the syntax of an array?
Answer: Arrays are typically defined with square brackets with the size of
the arrays as its argument.
Q3. What is a Homogeneous array in the javascript?
Answer: Arrays are classified as Homogeneous Data Structures
because they store elements of the same type. They can store
numbers, strings, boolean values (true and false), characters, objects,
and so on. But once you define the type of values that your array will
store, all its elements must be of that same type.
Example: var ar=[1,2,3,4,5,6];
var str=[“hi”,”hello”,”good”];
var ar=[true,false];
Q4. What is a heterogeneous array in javascript?
Answer: An array with elements that belong to different datatypes is
called Heterogenous Array.
Example: var ar= [12,”hello”,true,”hi”]
In the above array 12,hello,true these are elements.
Q5. How to find how many elements are there in the array
Answer: Using length property.
Q6.Which data type gives the length?
Answer: Length property is always gives number data type.
Q7. Position (or) Index always starts from?
Answer: Position (or) Index is always starts from “0”
Q8. Position (or) Index is ends with?
Answer: Position (or) Index is always ends with “length-1”
Q9. Which data type gives a position (or) Index?
Answer: Position always gives a number data type.
Q10. How to find the first value in the array?
var ar=[1,2,3,4,5];
console.log(ar[0]);
Answer: 1
Q11. How to find the last value in the array?
var ar=[1,2,3,4,5];
console.log(ar[ar.length-1]);
Answer: 5
Q12. Which data type returns the array.length?
Answer: ar.length is always gives number data type
Q13. What is the output of the below code?
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])
}
Correct Answer: 23,34,54,0,4,7,9,19,24,91
Q15. For the Given array: [23,34,54,0,4,7,9,19,24,91] print all numbers
in an array except the first element using a for loop?
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])
}
Correct Answer: 34,54,0,4,7,9,19,24,91
Q16. For the Given array: [23,34,54,0,4,7,9,19,24,91] print the length of
the array using for loop?
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])
}
Correct Answer: 23,34,54,0,4,7,9,19,24
Q18. For the Given array: [23,34,54,0,4,7,9,19,24,91] Print all the
numbers from last index to first index using a for loop?
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])
}
Correct Answer: 91,24,19,9,7,4,0,54,34,23
Q19. For the Given array: [23,34,54,0,4,7,9,19,24,91] Print only the
last 4 elements of an array using a for loop?
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])
}
Correct Answer: 9,19,24,91
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])
}
}
Correct Answer: 23,34,54,24,91
Q21. For the Given array: [23,34,54,10,4,7,19,24,56,89] print all the
even numbers in an Array using for loop?
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])
}
}
Correct Answer: 34,54,10,4,24,56
Q22. For the Given array: [23,-34,-54,10,-4,7,-4,9,-19,40] print all the
positive numbers in an array using a for loop?
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
Q24. For the Given array: [23,34,54,10,4,7,4,9,29,40] Print only
multiples of 2 OR3 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: 34,54,10,4,4,9,40
Q25. For the Given array: [23,34,54,10,4,7,4,9,29,40]
print the maximum number in an Array using a for loop.
Answer: var ar=[23,34,54,10,4,7,4,9,29,40];
var max= -Infinity
for(var i=0;i<=ar.length-1;i++){
if(ar[i] > max){
max = ar[i]
}
}
console.log(max)
Correct Answer: 54
Q26. For the Given array: [23,34,54,10,4,7] sum of all the numbers in
an Array using for loop? (Take initial value 4);
Answer:
var ar= [23,34,54,10,4,7]
var sum= 4
for(var i=0;i<=ar.length-1;i++){
sum=sum+ar[i]
}
console.log(sum);
Correct Answer: 136
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:
var ar= [23,34,54,10,4,7];
var num=14;
var present =0;
for(var i=0;i<=ar.length-1;i++){
if(ar[i] === num){
present++
}
}
if(present > 0){
console.log(true)
}
else{
console.log(false)
}
Correct Answer: false
Q28. For the Given array: [23,34,54,10,34,7,23,10,34] print the total
number of times an element found in the array. Given number: 23
Answer:
var ar= [23,34,54,10,34,7,23,10,34];
var count = 0;
var num =23
for(var i=0;i<=ar.length-1;i++){
if(ar[i] === num){
count++
}
}
console.log(count)
Correct Answer: 2
Q29. For the Given array: [23,34,54,10,4,7] print Average of a given
element in an Array.
Answer:
var ar= [23,34,54,10,4,7];
var sum=0;
for(var i=0;i<=ar.length-1;i++){
sum=sum+ar[i];
}
avg=sum / ar.length;
console.log(avg)
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].
31. For the given array: [2,4,8,9,12,23,45,23,43,14,89,8,10]. Find the
index of given number:Given number: 9
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