Core Java - Interview Questions and Answers for 'Array' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Interview Questions


 3359 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Array' - 70 question(s) found - Order By Rating

next 40
 Q1. Difference between ArrayList and HashSet ?Core Java2019-02-06 11:24:19

Ans. ArrayList is a list , i.e an implementation of List interface whereas HashSet is a Set and an implementation of Set interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arraylist  hashset


 Q2. difference between ArrayList and array ?Core Java2019-02-06 11:23:55

Ans. ArrayList is a variable length collection class whereas arrays are fixed length primitive structure.

We can use generics with arraylist but not with arrays.

We can store primitive data types within arrays but can't with ArrayList. In ArrayList that needs to be converted to Wrapper objects.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arraylist  arrays  collection classes  collections      basic        frequent


 Q3. Difference between List and ArrayList ?Core Java2019-01-17 20:28:50

Ans. List is an interface whereas ArrayList is an implementation of List.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     list  arraylist  list vs arraylist  difference between  collections     Asked in 2 Companies      basic


 Q4. Write a Program to Find the maximum sum of the sub array ?Data Structure2018-09-16 16:26:46

 This question was recently asked at 'Reddit'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Maximum subarray variant  arrays  sub array  subarray     Asked in 1 Companies


 Q5. Why doesn't this code give compile time error when we are clearly trying to add an integer element to an array of Strings

Object[] strArray = new String[2];
strArray[0] = 5;
Core Java2018-07-25 08:18:37

Ans. Because the reference is of Object Array and the resolution of what it holds is identified at the runtime.

String[] strArray = new String[2];
strArray[0] = 5;

would have given the compile time array as the reference is of String array.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q6. Will this give compile time error ?

Object[] strArray = new String[2];
strArray[0] = 5;
Core Java2018-07-25 08:17:03

Ans. No. But It will throw ArrayStoreException at runtime.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ArrayStoreException  arrays


 Q7. What is the problem with this code

String[] strArray = new String[2];
strArray[0] = 5;
Core Java2018-07-25 08:16:18

Ans. We are trying to add an integer element to an array of String. Will give compile time error.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q8. If arrays cannot be resized , Why is this code valid

String[] strArray = new String[2];
strArray = new String[5];
Core Java2018-07-25 08:01:42

Ans. We are not resizing the first array here but assigning the reference strArray to a new Array with size 5.

So after line 2, We have 2 arrays in memory, one with size 2 and other with size 5 with strArray referring to second array with size 5.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q9. Is this code valid

String[] strArray = new String[2];
strArray.length = 5;
Core Java2018-07-25 08:00:02

Ans. It will give compile time error saying "The final field array.length cannot be assigned"

Arrays once initialized cannot be resized.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q10. Is this array initialization correct ? If Yes, What will be the size of array ?

String[] strArray = new String[3]{"Buggy","Bread"};
Core Java2018-07-25 07:55:04

Ans. No. It will result in error saying "Cannot define dimension expressions when an array initializer is provided"

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q11. Is this array initialization correct ? If Yes, What will be the size of array ?

String[] strArray = new String[]{"Buggy","Bread"};
Core Java2018-07-25 07:54:15

Ans. Yes, size of the array will be 2.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q12. Is this array declaration correct ? If not , Why ?

String[] strArray = new String[];
Core Java2018-07-25 07:52:50

Ans. No, We haven't specified the size of array to be initialized.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q13. Can we make array volatile in Java ?Core Java2018-07-22 07:38:11

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  volatile


 Q14. Can we have 3 dimensional arrays in Java ?Core Java2018-07-22 07:37:54

Ans. Yes

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q15. Can we declare an array without assigning the size of an array? Core Java2018-07-22 07:36:26

Ans. No, It will throw compile time error saying "must provide either dimension expressions or an array initializer"

Alternatively we can provide array initializer like

String[] strArray = new String[]{"Buggy","Bread"};

which will initialize it to size 2 with values as "Buggy" and "Bread"


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q16. Difference between Arrays and LinkedList ?Data Structure2018-07-22 07:24:14

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays vs linkedlist


  Q17. Difference between Arrays and ArrayList ?Core Java2018-07-22 07:23:59

Ans. Both Arrays and ArrayLists are used to store elements. Elements can be either primitives or objects in case of Arrays, but only objects can be stored in Arraylist. Array is a fixed length data structure while arraylist is variable length collection class. Once created, you cannot change the size of the arrays, but arraylists can dynamically resize itself when needed.Another notable difference between Arrays and Arrayslist is that arary is part of core java programming and array list is part of collection classes

  Sample Code for arrays arraylist

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     array  arraylist  array vs arraylist     Asked in 7 Companies      basic        frequent


 Q18. What is the time and space complexity for different operations for arrays ?Core Java2018-07-22 07:22:09

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q19. Are arrays Thread Safe ?Core Java2018-07-22 07:21:29

Ans. Yes

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  concurrency     Asked in 1 Companies


 Q20. How can we check equality for Arrays in Java ?Core Java2018-07-22 07:21:05

Ans. You call the method java.util.Arrays.equals(Object[] a, Object[] a2)?

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q21. Are Arrays treated like primitives or like objects in Java ?Core Java2018-07-22 07:20:34

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q22. What are jagged arrays ?Core Java2018-07-22 07:19:45

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  jagged arrays


 Q23. What is an anonymous array in Java ?Core Java2018-07-22 07:18:37

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     anonymous arrays


 Q24. What is ArrayStoreException ?Core Java2018-07-22 07:18:05

Ans. It's an exception that is thrown when we attempt to add value of an incompatible type to an array.

For example -

Object[] strArray = new String[2];
strArray[0] = 5;

In this code, strArray reference of type Object has currently been assigned the String array but at line 2 we are trying to add an integer value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays   ArrayStoreException


 Q25. Can we change the size of array once created ?Core Java2018-07-22 07:16:31

Ans. No. Arrays cannot resize dynamically.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     array  array size      Basic


 Q26. How arrays work internally ?Core Java2018-07-22 07:15:43

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


  Q27. Write code to sort an array.Algorithm2018-07-04 16:15:16

Ans. https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     array  sorting     Asked in 16 Companies      basic        frequent


  Q28. Find Intersection/union between two arrays.Core Java2018-05-13 19:40:34

 This question was recently asked at 'Bristlecone,Amazon Lab126,Amazon,Microsoft,Facebook,NCR,Google'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  code  coding     Asked in 7 Companies        frequent


 Q29. Which of the following - arrays or LinkedList allow elements to be accessed using index and how ? Data Structure2018-04-29 09:03:26

Ans. Arrays allows elements to be accessed directly using the index.

As Array elements are stored in continuous memory locations it's very easy to find the memory address of any element using the formula as following

Memory Address of Array start or index 0 + ( Size of array element * Index )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  linkedlist


 Q30. Explain what happens when insertion is performed in case of ArrayList and LinkedList.Data Structure2017-10-26 15:52:53

Ans. Array List works on Array and when we add an element in middle of the list, Array List need to update the index of all subsequent elements. I the capacity is full, it even may need to move the whole list to a new memory location . Linked List works on Double linked list algorithm and all it has to do is to adjust the address of the previous and next elements.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arraylist vs linkedlist  collections  list     Asked in 2 Companies


 Q31. Write an efficient program for printing k largest elements in an array. Elements in array can be in any order.Data Structure2017-10-21 08:25:55

 This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays  coding  code     Asked in 1 Companies


 Q32. Which of the two - Arrays or LinkedList - is a better data structure for implementing Queue ? and Why ?Data Structure2017-08-28 19:34:12

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Arraylist  linkedlist  queue  collections


 Q33. Write a Program to find two maximum numbers in an array2017-08-20 14:50:48

Ans. public class FindTwoMax {
public static void main(String[] args) {
int myArray[] = { 1, 3, 5, 8, 6, 3 };
int max1 = myArray[0];
int max2 = 0;
for (int count = 0; count < myArray.length; count++) {
if (max2 < myArray[count]) {
max2 = max1;
max1 = myArray[count];
} else if (max2 < myArray[count]) {
max2 = myArray[count];
}
}
System.out.println(max1);
System.out.println(max2);
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     find 2 max in array


 Q34. How can we convert a character or a character array into a String ?Core Java2017-07-30 15:57:10

Ans. String has an argument constructor that take char array as argument and creates a string.

There is no constructor available with String that takes in a character and creates a String. We can use StringBuilder which has a char argument constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     character  char  char array  String  char to String  char array to String


 Q35. How can we convert String into Char array and vice versa ?Core Java2017-07-30 15:56:01

Ans. There is a method toCharArray() within String class that can be used to convert string to char array.

string.toCharArray();

String class has an argument constructor that takes a char array and create a string

new String(charArray);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  char array


 Q36. What will the following code print

int x[] = new int[5];
x[0] = 1;
x[1] = 2;
      
for(int count=0;count<=x.length;count++){
System.out.println(x[count]);
}
Core Java2017-07-25 19:06:12

Ans. 1
2
0
0
0
ArrayIndexOutOfBoundException: 5

As array index starts with 0 and ends with the index of (size - 1), the index 5 is inaccessible for the array and hence will throw the exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q37. What will the following code print ?

int x[] = new int[5];
x[0] = 1;
x[1] = 2;
      
for(int count=0;count<x.length;count++){
System.out.println(x[count]);
}
Core Java2017-07-25 19:03:36

Ans. 1
2
0
0
0

As arrays are not dynamically expanded , we have declared the array for size 10. As we have only initialized only 2 values , it will print rest as their default values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays


 Q38. What does the following initialization mean ?

ArrayList<LinkedList> traversalPaths = new ArrayList<LinkedList>();

What could be the use of such a collection.
Core Java2017-07-23 08:33:28

Ans. Initialize an ArrayList that will hold LinkedLists i.e every element of the arraylist will be a linked list.

Such collection could be used in algorithms that require first random access and then sequential traversal. For example - Storing traversal paths for a graph wherein we can start from any vertex. Implementing dictionary with each arraylist element holding staring with character and then linked list holding duplicate words.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections  linkedlist  arraylist


 Q39. Write code to find second largest number in an array of integers.Core Java2017-07-10 14:31:32

Ans. int arr[]={1,3,5,6,4,8,9,2,10};
Arrays.sort();
System.out.println(arr[arr.length-1]);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     code  coding  find in array     Asked in 1 Companies


 Q40. Why do we pass an array of strings to main method ?Core Java2017-06-23 12:58:05

Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   main method string array argument


next 40


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this