[Go to site: main page, start]

0% found this document useful (0 votes)
6 views2 pages

Java Array Interview Questions

The document provides a series of basic Java interview questions related to array manipulation, including finding maximum and minimum elements, calculating sum and average, identifying the second largest and smallest elements, checking if an array is sorted, reversing an array, performing linear search, counting even and odd numbers, counting positive and negative numbers, copying arrays, and finding the frequency of each element. Each question is accompanied by sample Java code demonstrating the solution. This resource serves as a practical guide for candidates preparing for technical interviews focused on array operations in Java.

Uploaded by

Harsh Dhake
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Array Interview Questions

The document provides a series of basic Java interview questions related to array manipulation, including finding maximum and minimum elements, calculating sum and average, identifying the second largest and smallest elements, checking if an array is sorted, reversing an array, performing linear search, counting even and odd numbers, counting positive and negative numbers, copying arrays, and finding the frequency of each element. Each question is accompanied by sample Java code demonstrating the solution. This resource serves as a practical guide for candidates preparing for technical interviews focused on array operations in Java.

Uploaded by

Harsh Dhake
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Level Array Interview Questions with Java Code

1. Find the maximum and minimum element in an array


int[] arr = {3, 7, 2, 9, 4};
int max = arr[0], min = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
[Link]("Max = " + max + ", Min = " + min);

2. Calculate sum and average of array elements


int[] arr = {5, 10, 15};
int sum = 0;
for (int num : arr) sum += num;
double avg = (double) sum / [Link];
[Link]("Sum = " + sum + ", Average = " + avg);

3. Find second largest / second smallest element


int[] arr = {3, 8, 1, 5, 9};
int largest = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
second = largest;
largest = num;
} else if (num > second && num != largest) {
second = num;
}
}
[Link]("Second Largest = " + second);

4. Check if array is sorted (ascending)


int[] arr = {1, 2, 3, 4, 5};
boolean isSorted = true;
for (int i = 1; i < [Link]; i++) {
if (arr[i] < arr[i - 1]) {
isSorted = false;
break;
}
}
[Link]("Array is sorted: " + isSorted);

5. Reverse an array
int[] arr = {1, 2, 3, 4, 5};
int start = 0, end = [Link] - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
[Link]([Link](arr));

6. Linear search for an element


int[] arr = {10, 20, 30, 40};
int target = 30;
boolean found = false;
for (int num : arr) {
if (num == target) {
found = true;
break;
}
}
[Link]("Element found: " + found);

7. Count even and odd numbers


int[] arr = {1, 2, 3, 4, 5, 6};
int even = 0, odd = 0;
for (int num : arr) {
if (num % 2 == 0) even++;
else odd++;
}
[Link]("Even = " + even + ", Odd = " + odd);

8. Count positive and negative numbers


int[] arr = {-5, 4, -2, 6, 0};
int pos = 0, neg = 0;
for (int num : arr) {
if (num > 0) pos++;
else if (num < 0) neg++;
}
[Link]("Positive = " + pos + ", Negative = " + neg);

9. Copy one array to another


int[] arr1 = {1, 2, 3};
int[] arr2 = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
arr2[i] = arr1[i];
}
[Link]("Copied array: " + [Link](arr2));

10. Find frequency of each element


int[] arr = {1, 2, 2, 3, 1, 4};
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
[Link](num, [Link](num, 0) + 1);
}
[Link]("Frequencies: " + freq);

You might also like