Core Java - Interview Questions and Answers for 'Array' - 69 question(s) found - Order By Rating Q1. How can we create an arraylist of unique values ? Core Java
Ans. We can put the value in a set to enforce uniqueness and then dum those value into an arraylist. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arraylist  design  collections Q2. Write a program to make an array act as a set. Core Java
Ans. public Set convertArrayToList(T array[]) {
Set set = new HashSet<>(Arrays.asList(array));
return set;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  collections set Asked in 1 Companies Q3. Difference between ArrayList and HashSet ? Core Java
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 Q4. difference between ArrayList and array ? Core Java
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 Q5. Difference between List and ArrayList ? Core Java
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 Q6. Write a Program to Find the maximum sum of the sub array ? Data Structure
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 Q7. 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 Java
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 Q8. Will this give compile time error ?
Object[] strArray = new String[2];
strArray[0] = 5; Core Java
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 Q9. What is the problem with this code
String[] strArray = new String[2];
strArray[0] = 5; Core Java
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 Q10. If arrays cannot be resized , Why is this code valid
String[] strArray = new String[2];
strArray = new String[5]; Core Java
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 Q11. Is this code valid
String[] strArray = new String[2];
strArray.length = 5; Core Java
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 Q12. Is this array initialization correct ? If Yes, What will be the size of array ?
String[] strArray = new String[3]{"Buggy","Bread"}; Core Java
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 Q13. Is this array initialization correct ? If Yes, What will be the size of array ?
String[] strArray = new String[]{"Buggy","Bread"}; Core Java
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 Q14. Is this array declaration correct ? If not , Why ?
String[] strArray = new String[]; Core Java
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 Q15. Can we make array volatile in Java ? Core Java
Ans. Yes, you can make an array (both primitive and reference type array e.g. an int array and String array) volatile in Java but only changes to reference pointing to an array will be visible to all threads, not the whole array Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  volatile Q16. Can we have 3 dimensional arrays in Java ? Core Java
Ans. Yes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays Q17. Can we declare an array without assigning the size of an array? Core Java
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 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 Q19. Difference between Arrays and ArrayList ? Core Java
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 Q20. What is the time and space complexity for different operations for arrays ? Core Java
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  arraysAns. 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 Q22. How can we check equality for Arrays in Java ? Core Java
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 Q23. Are Arrays treated like primitives or like objects in Java ? Core Java
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  arraysAns. an array of different sizes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  jagged arrays Q25. What is an anonymous array in Java ? Core Java
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 arraysAns. 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 Q27. Can we change the size of array once created ? Core Java
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 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  arraysAns. 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 Q30. Find Intersection/union between two arrays. Core Java
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