[Go to site: main page, start]

0% found this document useful (0 votes)
13 views4 pages

Java Array Questions

The document contains a series of Java programming exercises focused on array manipulation, including finding the largest and smallest elements, calculating sum and average, reversing an array, searching for elements, counting occurrences, sorting, finding the second largest element, merging arrays, identifying duplicates, and calculating the maximum difference between elements. Each question is accompanied by a complete Java code solution. These exercises are designed to enhance understanding of array operations in Java.
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)
13 views4 pages

Java Array Questions

The document contains a series of Java programming exercises focused on array manipulation, including finding the largest and smallest elements, calculating sum and average, reversing an array, searching for elements, counting occurrences, sorting, finding the second largest element, merging arrays, identifying duplicates, and calculating the maximum difference between elements. Each question is accompanied by a complete Java code solution. These exercises are designed to enhance understanding of array operations in Java.
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

Java Array Practice Questions with Solutions

Q1. Write a Java program to find the largest and smallest elements in an ar
public class MinMaxArray {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 7};
int min = arr[0], max = arr[0];

for (int num : arr) {


if (num < min) min = num;
if (num > max) max = num;
}
[Link]("Smallest: " + min + ", Largest: " + max);
}
}

Q2. Write a Java program to find the sum and average of elements in an ar
public class SumAverageArray {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20};
int sum = 0;

for (int num : arr) {


sum += num;
}
double avg = (double) sum / [Link];
[Link]("Sum: " + sum + ", Average: " + avg);
}
}

Q3. Write a Java program to reverse an array.


import [Link];
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = [Link] - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
[Link]("Reversed Array: " + [Link](arr));
}
}
Q4. Write a Java program to check if an element exists in an array.
public class SearchElement {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int target = 5;
boolean found = false;

for (int num : arr) {


if (num == target) {
found = true;
break;
}
}
[Link](target + (found ? " is found." : " is not found."));
}
}

Q5. Write a Java program to count occurrences of each element in an array


import [Link];
public class CountOccurrences {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 3, 3};
HashMap<Integer, Integer> countMap = new HashMap<>();

for (int num : arr) {


[Link](num, [Link](num, 0) + 1);
}
[Link](countMap);
}
}

Q6. Write a Java program to sort an array in ascending order.


import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {7, 2, 5, 3, 9};
[Link](arr);
[Link]("Sorted Array: " + [Link](arr));
}
}

Q7. Write a Java program to find the second largest element in an array.
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 5, 8, 30};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;

for (int num : arr) {


if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
[Link]("Second Largest: " + second);
}
}

Q8. Write a Java program to merge two arrays into a third array.
import [Link];
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] merged = new int[[Link] + [Link]];

[Link](arr1, 0, merged, 0, [Link]);


[Link](arr2, 0, merged, [Link], [Link]);

[Link]("Merged Array: " + [Link](merged));


}
}

Q9. Write a Java program to find the duplicate elements in an array.


import [Link];
public class FindDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 5, 1};
HashSet<Integer> set = new HashSet<>(), duplicates = new HashSet<>

for (int num : arr) {


if (![Link](num)) {
[Link](num);
}
}
[Link]("Duplicate Elements: " + duplicates);
}
}
Q10. Write a Java program to find the maximum difference between any tw
public class MaxDifference {
public static void main(String[] args) {
int[] arr = {3, 10, 6, 8, 2};
int min = arr[0], max = arr[0];

for (int num : arr) {


if (num < min) min = num;
if (num > max) max = num;
}
[Link]("Max Difference: " + (max - min));
}
}

Common questions

Powered by AI

The Java program calculates the sum by iterating through each element of the array, accumulating the values into a sum variable. The average is computed by dividing the sum by the number of elements, using type casting to ensure a floating-point result. With larger datasets, consider numerical overflow and computation time as factors that might require optimizing data structures or using parallel processing techniques .

One can improve efficiency using a binary search algorithm, which requires the array to be sorted first, reducing time complexity to O(log n). This is practically applied in search engines or databases where fast retrieval of information is crucial .

The merge operation copies elements from both arrays into a new pre-allocated array, using System.arraycopy for efficiency. Compared to dynamic data structures like lists, this approach is faster due to contiguous memory allocation but lacks flexibility, as arrays are fixed-size, whereas lists can dynamically resize .

The program computes the difference between the maximum and minimum elements to find the max difference. This is significant in financial applications for volatility measurement, ensuring accuracy by scanning the list once to capture extremities accurately despite the data distribution .

The program uses a HashSet to track seen elements, adding duplicates to another set. This approach efficiently identifies duplicates in O(n) time complexity due to the constant average time for insertion operations in hash tables, suitable for datasets prone to duplication issues .

The Java program iterates through the array, initializing the min and max variables to the first element of the array and then compares each element to update min or max accordingly. This is important in applications like data analysis where identifying extremities can influence decision-making processes .

Counting occurrences using a hash map provides an efficient means to tally elements, crucial in data analysis tasks like frequency analysis or histograms. This technique is employed extensively in statistics and machine learning for characterizing and understanding data distributions .

The program keeps track of the largest and second largest elements as it iterates through the array. Appropriate initialization (using Integer.MIN_VALUE) ensures that any number in the array will replace initial values, mitigating errors from uninitialized variables, especially important in dynamic datasets .

Arrays.sort() utilizes a dual-pivot quicksort, offering average time complexity of O(n log n). This is essential in data processing as it optimizes operations that require ordered data, such as binary search and data deduplication .

The algorithm swaps elements from both ends of the array moving towards the center, which runs in O(n) time complexity, where n is the length of the array. This process is fundamental in programming when in-place modifications of sequences are necessary, preserving memory space and efficiency .

You might also like