0% found this document useful (0 votes)
11 views6 pages

Java Array Questions Separate Solutions

dyguydgdy ydgiudid tdiudiugd iddggd
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)
11 views6 pages

Java Array Questions Separate Solutions

dyguydgdy ydgiudid tdiudiugd iddggd
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
You are on page 1/ 6

Java Array and 2D Array Questions

Array Questions
1. Declare an array of 10 integers in Java.

2. Initialize an array with the first 5 prime numbers.

3. Find the sum of all elements in an array.

4. Find the maximum element in an array.

5. Reverse the elements of an array.

6. Check if an array contains a specific value.

7. Count the number of even and odd numbers in an array.

8. Sort an array in ascending order.

9. Merge two arrays into one.

10. Find the duplicate elements in an array.

11. Remove duplicates from an array.

12. Rotate an array to the left by one position.

13. Find the second largest element in an array.

14. Check if two arrays are equal.

15. Move all zeros to the end of the array.

2D Array Questions
1. Declare a 2D array of integers with 3 rows and 3 columns.

2. Initialize a 3x3 matrix with values from 1 to 9.

3. Find the sum of all elements in a 2D array.

4. Print the elements of a 2D array row-wise.

5. Print the elements of a 2D array column-wise.

6. Transpose a 2D array.

7. Add two 2D arrays.

8. Multiply two 2D arrays.

9. Find the maximum element in each row of a 2D array.


10. Check if a 2D array is a square matrix.

11. Check if a 2D array is symmetric.

12. Calculate the sum of diagonal elements.

13. Find the row with the maximum sum.

14. Rotate a 2D array 90 degrees clockwise.

15. Check if a 2D array is an identity matrix.


Solutions: Array Questions
1. 1. Declare an array of 10 integers in Java.

Solution:

int[] arr = new int[10];

2. 2. Initialize an array with the first 5 prime numbers.

Solution:

int[] primes = {2, 3, 5, 7, 11};

3. 3. Find the sum of all elements in an array.

Solution:

int sum = 0; for (int num : arr) sum += num;

4. 4. Find the maximum element in an array.

Solution:

int max = arr[0]; for (int num : arr) if (num > max) max = num;

5. 5. Reverse the elements of an array.

Solution:

for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i]

= temp; }

6. 6. Check if an array contains a specific value.

Solution:

boolean found = false; for (int num : arr) if (num == value) found = true;

7. 7. Count the number of even and odd numbers in an array.

Solution:

int even = 0, odd = 0; for (int num : arr) { if (num % 2 == 0) even++; else odd++; }

8. 8. Sort an array in ascending order.

Solution:

Arrays.sort(arr);

9. 9. Merge two arrays into one.


Solution:

int[] merged = new int[arr1.length + arr2.length]; System.arraycopy(arr1, 0, merged, 0, arr1.length);

System.arraycopy(arr2, 0, merged, arr1.length, arr2.length);

10. 10. Find the duplicate elements in an array.

Solution:

for (int i = 0; i < arr.length; i++) for (int j = i + 1; j < arr.length; j++) if (arr[i] == arr[j])

System.out.println("Duplicate: " + arr[i]);

11. 11. Remove duplicates from an array.

Solution:

Set<Integer> set = new HashSet<>(); for (int num : arr) set.add(num);

12. 12. Rotate an array to the left by one position.

Solution:

int first = arr[0]; for (int i = 1; i < arr.length; i++) arr[i - 1] = arr[i]; arr[arr.length - 1] = first;

13. 13. Find the second largest element in an array.

Solution:

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; }

14. 14. Check if two arrays are equal.

Solution:

boolean equal = Arrays.equals(arr1, arr2);

15. 15. Move all zeros to the end of the array.

Solution:

int[] result = new int[arr.length]; int index = 0; for (int num : arr) if (num != 0) result[index++] = num;
Solutions: 2D Array Questions
1. 1. Declare a 2D array of integers with 3 rows and 3 columns.

Solution:

int[][] matrix = new int[3][3];

2. 2. Initialize a 3x3 matrix with values from 1 to 9.

Solution:

int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};

3. 3. Find the sum of all elements in a 2D array.

Solution:

int sum = 0; for (int[] row : matrix) for (int val : row) sum += val;

4. 4. Print the elements of a 2D array row-wise.

Solution:

for (int[] row : matrix) System.out.println(Arrays.toString(row));

5. 5. Print the elements of a 2D array column-wise.

Solution:

for (int j = 0; j < matrix[0].length; j++) { for (int i = 0; i < matrix.length; i++) System.out.print(matrix[i][j]

+ " "); System.out.println(); }

6. 6. Transpose a 2D array.

Solution:

int[][] transpose = new int[matrix[0].length][matrix.length]; for (int i = 0; i < matrix.length; i++) for (int j

= 0; j < matrix[0].length; j++) transpose[j][i] = matrix[i][j];

7. 7. Add two 2D arrays.

Solution:

int[][] sum = new int[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) sum[i][j] = a[i][j] + b[i][j];

8. 8. Multiply two 2D arrays.

Solution:

int[][] product = new int[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++)
product[i][j] += a[i][k] * b[k][j];

9. 9. Find the maximum element in each row of a 2D array.

Solution:

for (int[] row : matrix) { int max = row[0]; for (int val : row) if (val > max) max = val;

System.out.println("Max: " + max); }

10. 10. Check if a 2D array is a square matrix.

Solution:

boolean isSquare = matrix.length == matrix[0].length;

11. 11. Check if a 2D array is symmetric.

Solution:

boolean symmetric = true; for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[0].length; j++) if

(matrix[i][j] != matrix[j][i]) symmetric = false;

12. 12. Calculate the sum of diagonal elements.

Solution:

int diagSum = 0; for (int i = 0; i < matrix.length; i++) diagSum += matrix[i][i];

13. 13. Find the row with the maximum sum.

Solution:

int maxSum = Integer.MIN_VALUE, rowIndex = -1; for (int i = 0; i < matrix.length; i++) { int rowSum =

0; for (int val : matrix[i]) rowSum += val; if (rowSum > maxSum) { maxSum = rowSum; rowIndex = i; }

14. 14. Rotate a 2D array 90 degrees clockwise.

Solution:

int[][] rotated = new int[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) rotated[j][2-i] = matrix[i][j];

15. 15. Check if a 2D array is an identity matrix.

Solution:

boolean isIdentity = true; for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[0].length; j++) if

((i == j && matrix[i][j] != 1) || (i != j && matrix[i][j] != 0)) isIdentity = false;

You might also like