Array Flashcards

1
Q

What is an Array?

A
  1. Array is a collection of similar data types. It can not have different data type. It can hold both primitive types (int, float, double) and object references.
  2. It is fixed in length i.e static in nature.
  3. Arrays are created on the heap memory not on the stack.
  4. Accessing an invalid index of an Array will cause exception.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare an Array in java?

A

You can declare an Array in java by the following way :

dataType[] arrayVariableName = new dataType[arraySize];

for example for int data type, you can declare an int array as :

int[] temp = new int[256]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you declare an Array without Array size?

A

No, you can not declare Array without Array size. You will get compile time error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where does Array stored in JVM memory ?

A

Array is an object in java. So, Array is stored in heap memory in JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages of Array ?

A

a. We can sort multiple elements of Array at the same time.

b. Using index, we can access the element of the Array in O(1) time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the disadvantages of Array?

A

a. To create an Array, contiguous memory is required. It is possible in JVM that the memory is available to accommodate Array but memory available is not contiguous.
b. The Array is static data structure. It is of fixed size. We can not increase or decrease the size of the Array after creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is legal int[] arr or int arr[] ?

A

Both are legal statements. It is preferred to write int[] arr instead of int arr[].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between Array and ArrayList ?

A

Array is static in size i.e of fixed length. Size can not be changed after declaration. ArrayList is dynamic in nature. If you add elements to an ArrayList, it will automatically increase its size.

Array can contain both primitive and Object data types. ArrayList does not contain primitive data types. It only contains object entries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between Array and LinkedList in java ?

A

Memory required for storing the same number of elements in Array is less as compared to LinkedList. Array only stores the data of the element whereas LinkedList stores data plus the address of the next node.

Array requires contiguous memory allocation where as LinkedList elements are present all over the heap memory. Unlike Array, LinkedList does not have limitation of contiguous memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are jagged arrays in java?

A

Arrays containing arrays of different length is known as jagged arrays. Multidimensional arrays are also known as jagged arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the different ways to copy one Array from another Array?

A

There are four ways by which we can copy an Array.

a. By using for loop
b. By using clone() method
c. By using Arrays.copyOf() method
d. By using System.arraycopy() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will happen if you do not initialize an Array?

A

Array will take default value depending upon the data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to find duplicate elements in a given Array?

A

There are many ways by which you can find the duplicates in an Array. I am sharing two ways

a. using for loop and compare
b. using HashSet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are Array thread-safe ?

A

Reading an Array is a thread-safe operation but modifications are not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Find out smallest and largest number in a given Array?

A

Logic to find the smallest and largest number in a given Array is given below :

a. Create two variables for storing largest and smallest number.
b. Initialize smallest variable with value Integer.MAX_VALUE
c. Initialize largest variable with value Integer.MIN_VALUE
d. In each traversal of for loop, we will compare the current element with the largest and smallest number. We will update the value.
e. If a number is larger than largest, then it can not be smaller than the smallest. So we can skip if first condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to convert HashSet to Array in java ?

A

You can convert HashSet to Array using toArray() method.

17
Q

How do you separate zeros and non-zeros in a given Array in java?

A

Logic to separate zeros and non-zeros is given below :

a. Initialize variable counter to 0.
b. Iterating inputArr from left to right. If inputArr[i] is not zero then assign inputArr[i] to inputArr[counter].
c. Increment the counter by 1.
d. Assign the remaining elements with 0 value.

18
Q

How to convert Array to ArrayList in java ?

A

The easy way to convert Array to ArrayList is using Arrays class asList() method. You need to pass the Array to the asList() method as argument.

There is another way to convert Array to ArrayList using addAll() method. For details check here.

19
Q

How to convert Array to TreeSet in java ?

A

To convert Array to TreeSet in java, first we need to convert Array to List using Arrays class asList() method. After converting Array to List, pass the list object to TreeSet constructor. That’s it , Array has been converted to TreeSet. You can confirm by printing out the values of TreeSet object.

20
Q

How to convert ArrayList to String Array in java ?

A

There are two ways to convert ArrayList to String Array in java. First method is using ArrayList get() method and second is using toArray() method. You can check both of the methods here.

21
Q

What are the different ways of copying an array into another array?

A

There are four methods available in java to copy an array.

1) Using for loop
2) Using Arrays.copyOf() method
3) Using System.arraycopy() method
4) Using clone() method

22
Q

Can we add or delete an element after assigning an array?

A

No it is not possible.

23
Q

Syntax

These methods searches for the specified element in the array with the help of Binary Search algorithm.

A

Arrays.binarySearch(intArr, intKey)

24
Q

Syntax

This method compares two arrays passed as parameters lexicographically.

A

Arrays.compare(intArr, intArr1)

25
Q

Syntax
This method copies the specified array, truncating or padding with the default value (if necessary) so the copy has the specified length.

A

Arrays.copyOf(originalArray, newLength)

26
Q

Syntax

This method copies the specified range of the specified array into a new Arrays.

A

Arrays.copyOfRange(originalArray, fromIndex, endIndex)

27
Q

Syntax

This method checks if both the arrays are equal or not.

A

Arrays.equals(array1, array2)

28
Q

Syntax

This method assigns this fillValue to each index of this Arrays.

A

Arrays.fill(originalArray, fillValue)

29
Q

Syntax

This method finds and returns the index of the first unmatched element between the two specified arrays.

A

Arrays.mismatch(array1, array2)

30
Q

Syntax

This method sorts the specified array using parallel sort.

A

Arrays.parallelSort(originalArray)

31
Q

Syntax

This method sorts the complete array in ascending order.

A

Arrays.sort(originalArray)

32
Q

Syntax
This method returns a String representation of the contents of this Arrays. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”)

A

Arrays.toString(intArr)