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

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Arrays' - 41 question(s) found - Order By Newest

Frequently asked.
 Q1. If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ?Core Java
Admin
info@buggybread.com
Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements. LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements
1. Retrieval - If Elements are to be retrieved sequentially only, Linked List is preferred.
2. Insertion - If new Elements are to be inserted in between other elements , Linked List is preferred.
3. Search - Binary Search and other optimized way of searching is not possible on Linked List.
4. Sorting - Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list.
5. Adding Elements - If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don't need consecutive memory location.

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

   Like      Discuss      Correct / Improve     java   collections   list   arraylist   linkedlist   difference between   architecture   data structure   ebay     ADP      basic        frequent

Try 2 Question(s) Test


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q2. What is the difference between ArrayList and LinkedList ?Core Java
admin
info@buggybread.com
Ans. Underlying data structure for ArrayList is Array whereas LinkedList is the linked list and hence have following differences -

1. ArrayList needs continuous memory locations and hence need to be moved to a bigger space if new elements are to be added to a filled array which is not required for LinkedList.

2. Removal and Insertion at specific place in ArrayList requires moving all elements and hence leads to O(n) insertions and removal whereas its constant O(1) for LinkedList.

3. Random access using index in ArrayList is faster than LinkedList which requires traversing the complete list through references.

4. Though Linear Search takes Similar Time for both, Binary Search using LinkedList requires creating new Model called Binary Search Tree which is slower but offers constant time insertion and deletion.

5. For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster.

  Sample Code for ArrayList

  Sample Code for LinkedList

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

   Like      Discuss      Correct / Improve     collections   java   data structures   arraylist   linkedlist     Deutsche Bank  Overstock.com  HCL Technologies  Infosys  EBay  The Advisory Board Company  Accuity  JC Penney  Cerner  Chase  Amazon  Deloitte  Deegit  Tata Consultancy  Cognizant (CTS)  IBM  Accenture  Mindtree  ESRI  QwikCilver Solutions  TMC Bonds  GumGum  EMC  Yelp  Classteacher Learning System  FactSet  Simply Hired  SalesForce  Royall & Company  Indeed  eClinicalWorks  TravelClick  State Farm  Manhattan Associates  GoEuro  Veeva Systems  Ness Technologies  Marlabs  Rolta  Volante Technologies  Happiest Minds Technologies  Coviam  Velocify  zanox  SimplyHired  IDBI Intech  Wissen Infotech  Nike  OnDot  PexSupply  Pramati Technologies  Credit Agricole  Mastek  Six Dee Telecom  Accuity  Kellton Tech      Basic        frequent

Try 1 Question(s) Test


  Q3. Difference between Vector and ArrayList ?Core Java
Anonymous
Ans. Vectors are synchronized whereas Array lists are not.

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

   Like      Discuss      Correct / Improve     java   basic interview question   vector   arraylist   collections   synchronization     Oracle  Lab49  Infosys  Wipro  Zoho  HCL Technologies  Tata Consultancy (CTS)  BirlaSoft  Emory  Xplana  British Telecom (BT)  Apex Systems  Capgemini  Accenture  3dplm Software Solution  AppKnox  eIntern  Landmark Information Group  ESystems  Orbitz Worldwide  HARMAN  Starmount  iConsultus  KARYA Technologies  Smart Chip  Cuelogic Technologies  Kronos  Trigent Software  Deloitte  ACI Worldwide  Unisys  Tavant Technologies  Referance data      basic        frequent


 Q4. What are the disadvantages of using arrays ?Core Java
Admin
info@buggybread.com
Ans. Arrays are of fixed size and have to reserve memory prior to use. Hence if we don't know size in advance arrays are not recommended to use.

Arrays can store only homogeneous elements.

Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory.

There is no underlying data structure for arrays and no ready made method support for arrays, for every requriment we need to code explicitly

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

   Like      Discuss      Correct / Improve     java   arrays   disadvantages of arrays   arrays memory   arrays vs variables


 Q5. What are the Disadvantages of using Collection Classes over Arrays ?Core Java
Admin
info@buggybread.com
Ans. Collections can only hold objects, It can't hold primitive data types.

Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.

Collections doesn't allow modification while traversal as it may lead to concurrentModificationException.

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

   Like      Discuss      Correct / Improve     java   collections   collections classes   disadvantages of collections over arrays   collections vs arrays


 Q6. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?

List list1 = new ArrayList<>();
list1.add(5);
list1.add("5");
Admin
info@buggybread.com
Ans. If we don't declare the list to be of specific type, it treats it as list of objects.

int 1 is auto boxed to Integer and "1" is String and hence both are objects.

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

   Like      Discuss      Correct / Improve     java   collections   arraylist   list   autoboxing   wrapper classes      expert        rare


 Q7. What is the advantage of using arrays over variables ?
Admin
info@buggybread.com
Ans. Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

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

   Like      Discuss      Correct / Improve     java   arrays   loops   variables   basic interview question


 Q8. Advantage of Collection classes over Arrays ?
Admin
info@buggybread.com
Ans. Collections are re-sizable in nature. We can increase or decrease the size as per recruitment.
Collections can hold both homogeneous and heterogeneous data's.
Every collection follows some standard data structures.
Collection provides many useful built in methods for traversing,sorting and search.

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

   Like      Discuss      Correct / Improve     java   collections   collections classes   advantages of collections over arrays   collections vs arrays   basic interview question      basic        frequent


 Q9. Difference between ArrayList and LinkedList ?
Admin
info@buggybread.com
Ans. LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array.

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

   Like      Discuss      Correct / Improve     java   collections   list   arraylist   linkedlist   difference between      basic        frequent



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. What is the difference between int[] x; and int x[]; ?Core Java
admin
info@buggybread.com
Ans. No Difference. Both are the acceptable ways to declare an array.

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

   Like      Discuss      Correct / Improve     java   array   arrays   array declaration   difference between     ABC Ltd   Infosys  InfoTrellis      basic


 Q11. What is CopyOnWriteArrayList ?
Admin
info@buggybread.com
Ans. Its a type of ArrayList in which all Write operations , i.e add and set are performed by creating a new copy. This array never changes during the lifetime of the iterator, so it never throws ConcurrentModificationException

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

   Like      Discuss      Correct / Improve     java   collections   list   arraylist   copyonwritearraylist   ConcurrentModificationException     Roposo


 Q12. What are the advantages and disadvantages of CopyOnWriteArrayList ?Core Java
Admin
info@buggybread.com
Ans. This collections class has been implemented in such a manner that it can never throw ConcurrentModificationException. As it performs update and write operations by creating a new copy of ArrayList, It's slower compared to ArrayList.

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

   Like      Discuss      Correct / Improve     java   collections   list   arraylist   copyonwritearraylist   advantages-disadvantages   ConcurrentModificationException     Cognizant (CTS)  Engine Singapore  Credit Agricole  Roposo      Expert


 Q13. What is ArrayIndexOutOfBoundException ?
Admin
info@buggybread.com
Ans. Exception thrown by the application is we try to access an element using an index which is not within the range of array i.e lower than 0 or greater than the size of the array.

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

   Like      Discuss      Correct / Improve     java   exceptions   arrayindexoutofboundexception


 Q14. What will the following code result ?

List> intList = new ArrayList>();

intList.add(Optional.empty());
intList.add(Optional.of(2));
intList.add(Optional.of(3));
intList.add(Optional.of(4));

System.out.println(intList.get(null));
admin
info@buggybread.com
Ans. Compile time error at last line as the get method expect argument of type native int.

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

   Like      Discuss      Correct / Improve     arraylist   list   coding   collections


 Q15. What will the following code result ? Will it compile ?

List> intList = new ArrayList>();

intList.add(Optional.empty());
intList.add(Optional.of(2));
intList.add(Optional.of(3));
intList.add(Optional.of(4));

System.out.println(intList.get((Integer)null));
admin
info@buggybread.com
Ans. Yes but the last line will throw NullPointerException upon execution.

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

   Like      Discuss      Correct / Improve     collections   arraylist   list   coding


 Q16. Can we add more elements to an array list that has been marked as final ?

Ans. Yes, the array list can hold more elements. Final only puts the restriction that the array list reference cannot hold any other array list.

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

   Like      Discuss      Correct / Improve     ebay   collections   arraylist   final keyword


 Q17. Does an ArrayList allow elements of different types ? If not, Why the following code works List list1 = new ArrayList<>(); list1.add(1); list1.add("1");Core Java2016-07-29 11:44:07

Ans. With Java 7 or Later. If you don't declare the list to be of specific type , it treats it as list of objects. int 1 is auto boxed to Integer and "1" is String and hence both are objects.

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

   Like      Discuss      Correct / Improve     arraylist  list  collections


 Q18. 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


 Q19. Implement the following method ?

int findMax(int[] items)
Core Java2016-12-01 16:04:18

Ans. int findMax(int[] items){
   int maxNumber = 0;
   for(int x:items){
      if(x > maxNumber){
         maxNumber = x;
      }
   }
      
   return maxNumber;
}

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

   Like      Discuss      Correct / Improve     coding  code  find max number in an array     Nextlabs



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. Difference between Array and ArrayList ?Core Java2016-12-15 14:13:12

Ans. http://javahungry.blogspot.com/2015/03/difference-between-array-and-arraylist-in-java-example.html

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

   Like      Discuss      Correct / Improve     array  arraylist     Accenture


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

 This question was recently asked at 'HCL Technologies'.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     code  coding  find in array     HCL Technologies


 Q22. Write a Program to merge two sorted arrays ?Data Structure2017-03-27 08:46:57

 This question was recently asked at 'Compro Technologies,ECI Telecom'.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  merge sorted arrays     Compro Technologies  ECI Telecom


 Q23. Is array an object in Java ? How can you prove that ?

Core Java2017-03-30 07:19:40

Ans. Yes.

There are 2 ways this can be confirmed

1. Accessing object class methods using array reference.

2. Checking if the array is an instance of Object class

if (arrayRef instanceof Object)


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

   Like      Discuss      Correct / Improve     arrays


 Q24. How can we get an array out of ArrayList ?Core Java2017-05-07 16:42:41

Ans. We can get an array out of ArrayList by using toArray() method of an array list.

String a[] = arrayList.toArray();

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

   Like      Discuss      Correct / Improve     array  arraylist


 Q25. What is the trade off between using Collections.synchronizedList() on an ArrayList and a CopyOnWriteArrayList?Core Java2017-05-07 19:31:54

 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     Collections.synchronizedList()  CopyOnWriteArrayList


 Q26. why aren't arrays better than array list ?Core Java2017-05-31 11:42:53

 This question was recently asked at 'Wells Fargo'.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  arraylist  arrays vs arraylist     Wells Fargo


 Q27. Why iterators of an array list are fail fast ?Core Java2017-06-08 07:30:13

Ans. Because it's access isn't synchronized and hence access / modification by multiple threads may lead to inconsistent state.

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

   Like      Discuss      Correct / Improve     fail fast  arraylist  collections


 Q28. What is meant by "Vector is synchronized and ArrayList isn't " ?Core Java2017-06-08 07:32:02

Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads.

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

   Like      Discuss      Correct / Improve     vectors  arraylist  collections  list  synchronization  synchronized


 Q29. What problem we could have with ArrayList which aren't possible with Vectors ?Core Java2017-06-08 07:34:24

Ans. ArrayLists aren't synchronized and hence doesn't allow synchronized access. As multiple threads can access an arraylist in parallel, it may result in an inconsistent state.

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

   Like      Discuss      Correct / Improve     vector  arraylist  list  collections      Basic



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. What is the quickest way to find count of duplicate elements in an arraylist, without using iteration or loops ?Core Java2017-06-09 08:19:17

Ans. We can copy the elements to a Set and then find the difference of count between ArrayList and Set. As Set don't allow duplicates , they will be removed in the set.

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

   Like      Discuss      Correct / Improve     collections  arraylist  set


 Q31. 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


 Q32. 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


 Q33. 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


 Q34. 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


 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. 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


 Q37. 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


 Q38. 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     Amazon


 Q39. Write a program / method that takes an array of integer and return the difference between the smallest and largest element ?2016-06-30 20:17:28

Ans.
static int diff(int[] x){
   int smallest = x[0];
   int largest = x[0];
   for(int element: x){
      if(element < smallest){
         smallest = element;
      } else if(element > largest){
         largest = element;
      } else {
         continue;
      }
   }
   return largest-smallest;
}

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

   Like      Discuss      Correct / Improve     difference between the smallest and largest element   array



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q40. 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


 Q41. What will be the output of exceuting main method ?

public static void main(String[] args){
      List list = new ArrayList();
      list.add(1);
      list.add(2);
      list.add(3);
      System.out.println(list);
   }
Core Java
a. 1,2,3
b. Order cannot be determined
c. compilation error
d. 3,2,1

Ans.a. 1,2,3



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

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: