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

Java_Arrays_Interview_Questions

The document provides a comprehensive overview of Java arrays, covering basic concepts such as declaration, initialization, and default values. It includes operations like finding length, copying, reversing, sorting, and comparing arrays, as well as searching techniques including binary search. Additionally, it explains how to iterate over arrays using enhanced for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java_Arrays_Interview_Questions

The document provides a comprehensive overview of Java arrays, covering basic concepts such as declaration, initialization, and default values. It includes operations like finding length, copying, reversing, sorting, and comparing arrays, as well as searching techniques including binary search. Additionally, it explains how to iterate over arrays using enhanced for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Arrays – Interview Questions with

Answers
Basic Concepts

What is an array in Java?


Answer: An array is a container object that holds a fixed number of values of a single type.
Example:
int[] arr = {1, 2, 3, 4};

How do you declare an array in Java?


Answer:
int[] arr; // Preferred
int arr[]; // Also valid

How do you initialize an array in Java?


Answer:
int[] arr = new int[5]; // Initialized with default values

What is the default value of array elements in Java?

int: 0, float: 0.0, boolean: false, Object: null

Can you change the size of an array after initialization?


Answer: No. Arrays are fixed in size once initialized.

Array Operations

How to find the length of an array?


Answer: Using .length property.
Example: int len = arr.length;

How to copy an array in Java?


Answer:
int[] copy = Arrays.copyOf(original, original.length);

How to reverse an array in Java?


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

How to sort an array in Java?


Answer: Arrays.sort(arr);

How to compare two arrays?

Arrays.equals(arr1, arr2);

Searching and Iteration

How to search an element in an array?


Answer:
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
// found
}
}

What is binary search?


Definition: Efficient search on a sorted array with O(log n) time.
Example:
Arrays.binarySearch(arr, key);

How to iterate over an array?


Answer:
for (int num : arr) {
System.out.println(num);
}

You might also like