top-50-array-interview-questions
top-50-array-interview-questions
com/
An Array is declared similar to how a variable is declared, but you need to add [] after the type.
We can declare Java array as a field, static field, a local variable, or parameter, like any other
variable. An array is a collection of variables of that type. Here are a few more Java array
declaration examples:
String [] stringArray;
MyClass [] myClassArray;
Advantages:
We can put in place other data structures like stacks, queues, linked lists, trees, graphs,
etc. in Array.
Arrays can sort multiple elements at a time.
1 / 10
https://career.guru99.com/
Disadvantages:
We have to declare Size of an array in advance. However, we may not know what size
we need at the time of array declaration.
The array is static structure. It means array size is always fixed, so we cannot increase
or decrease memory allocation.
No we cannot change the array size. Though there are similar data types available which allow
a change in size.
2 / 10
https://career.guru99.com/
If 2 arrays are of the sam size & data type then comparison can be done using "Arrays.equal ()"
[crayon-5f3019250fa2f685432487/]
9) How to sort an Array?
It is possible using the built static method that is Arrays. sort ().
[crayon-5f3019250fa31432096008/]
10) Can we declare array size as a negative number?
It is a runtime exception. For example, we can store only string elements in a String Array. If
anybody tries to insert integer element in this String Array, then we will get ArrayStoreException
at run time.
No it is not possible.
Example:-
[crayon-5f3019250fa32552046716/]
14) Is there any difference between int[] a and int a[]?
15) There are 2 int type array data type. One is containing 50 elements, and another one
is containing 30 elements. Can we assign the array of 50 elements to an array of 30
elements?
3 / 10
https://career.guru99.com/
Yes we can assign provided they should the same type. The compiler will check the only type of
the array, not the size.
[crayon-5f3019250fa34969439822/]
16) int a[] = new int[3]{1, 2, 3} – is it a right way to declare arrays in java?
No. We should not mention the size of the array when we are providing the array elements.
Jagged Arrays are Arrays are containing arrays of different length. Jagged arrays are also
known as multidimensional arrays.
It will occur when the program tries to access invalid index of an array. Index higher than the
size of the array or negative index.
20) Can you explain different steps of declaring multidimensional arrays in Java?
[crayon-5f3019250fa36688705908/]
21) How do we search a specific element in an array?
We can use Arrays.binarySearch() method. This method uses binary search algorithm.
If any two elements found equal or same, we declare them as duplicates. Please find below
code for the same
[crayon-5f3019250fa38861913814/]
4 / 10
https://career.guru99.com/
In Java, memory for arrays is always allocated on the heap as arrays in Java are objects.
Array is an object. To retirve class name we can use getClass().getName() method on the
object.
28) "int a[] = new int[3]{1, 2, 3}" – This a legal way of defining the arrays?
No. We should not declare the size of the array when we are providing the array elements.
Yes, we can make an array volatile in Java, but we only make the variable which is pointing to
array volatile.
5 / 10
https://career.guru99.com/
System.out.print(myArr);
Printing myArr will print garbage value. It is not same as printing myArr[0].
Index starts from Zero(0), so the first element is stored in location zero and the last element will
be Array.length - 1.
34) Can you tell me the differences between Array and ArrayList?
Array ArrayList
The array is static with a fixed size which ArrayList is not static but dynamic in size. As
cannot be changed once delared. elements added to an ArrayList, its capacity or
size grows automaticically.
It contains both primitive data types and objects ArrayList does not contain the primitive data
of a class. types but contains object entries.
Array does not have Generics feature. ArayList has Generics feature.
35) We know that Arrays are objects so why cannot we write strArray.length()?
Arrays are object references like classes in Java. We can use the methods of Object like
toString () and another one hashCode () against Array. Length is a data item of an array and so
it is not a method. That's why we are using strArray.length.
Solutions to solve this problem is to calculate sum of all numbers in the array
and compare with an expected sum, the difference would be the missing number.
6 / 10
https://career.guru99.com/
total = n*(n+1)/2
Subtract all the numbers from sum and
you will get the missing number.
According to below logic sumOfNnumberss is 7*(7+1)/2=28
sumOfElements = 1+2+3+5+6+7=24
If myNames contains that value then it will return true otherwise false.
both the methods return true if the value is available otherwise false.
First Method
After that it will test if an array contains any value then it will return true otherwise false.
[crayon-5f3019250fa43386231610/]
Second Method
This method loop through an array and use equal() method to search element.
This actually performs a linear search over an array in Java. It will return true if an array has
provided value.
[crayon-5f3019250fa44156828435/]
39) How to get largest and smallest number in an array?
7 / 10
https://career.guru99.com/
[crayon-5f3019250fa46571484755/]
40) How to do the intersection of two sorted arrays?
Ans: Logic: print the element if the element is present or available in both the arrays.
Use two index variables i and j, after that initial the values i = 0, j = 0
If arr01 [i] is smaller than arr02 [j] then increment i.
If arr01 [i] is greater than arr02 [j] then increment j.
If both are same then print any of them and increment both i and j.
[crayon-5f3019250fa48167102889/]
41) How to get top two numbers from an array?
We will iterate this array and compare each number with max01 and max02,
If current number is greater than max1 then assign max01 = number and max02 =
max1.
Else if it is only greater than max02 then we will only update max02 with the current
number.
At the end of an iteration, max01 and max02 points to top two numbers from given
array.
[crayon-5f3019250fa4a542538369/]
42) How to cut or remove an element from the array?
Ans: Logic is: We can remove or cut an element using Apache Commons ArrayUtils based on
an index.
8 / 10
https://career.guru99.com/
[crayon-5f3019250fa4d162704028/]
44) How do you find the second largest element in an array of integers?
[crayon-5f3019250fa51306567151/]
46) How do you separate zeros from non-zeros in an array?
Initializing counter to 0
Traversing inputArray from left to right
If inputArray[i] is non-zero
Assigning inputArray[i] to inputArray[counter]
Incrementing the counter by 1
Assigning zero to remaining elements
[crayon-5f3019250fa52049161636/]
47) Write a program to insert an element and in the specific position in the array?
[crayon-5f3019250fa54756396148/]
48) How to get the index of an array element?
9 / 10
https://career.guru99.com/
In each step, it checks the input key value with the value of the middle element of an
array.
If the keys match then it will return the position. In another case, if the key is less than
the middle element's key,
Then it will repeat the while loop. If the remaining array searched and it reduced to zero
it will return -1 means not found
[crayon-5f3019250fa56796229455/]
49 Can we extend an array after initialization?
Ans: Logic is: We can extend an array after initialization by adding a new array.
Ans: This example fill(initialize all the elements of the array in one short) an array by using
100,100,100,100,100,100
100,100,100,50,50,50
10 / 10
Powered by TCPDF (www.tcpdf.org)