Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers - Order By Newest | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 Asked in 3 Companies basic | ||||
| ||||
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 Asked in 1 Companies | ||||
| ||||
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 Asked in 4 Companies Expert | ||||
| ||||
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 Asked in 1 Companies | ||||
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 | ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. <a href="http://javahungry.blogspot.com/2015/03/difference-between-array-and-arraylist-in-java-example.html" rel="nofollow">http://javahungry.blogspot.com/2015/03/difference-between-array-and-arraylist-in-java-example.html</a> | ||||
Sample Code for ArrayList | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  array  arraylist Asked in 11 Companies basic   frequent | ||||
| ||||
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 | ||||
| ||||
Ans. We can merge two sorted array by using quick sort algorithm | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  arrays  merge sorted arrays Asked in 2 Companies | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. Synchronized List locks the whole list to provide synchronization and thread safety during the read or write operation, while, CopyOnWriteArrayList doesn’t lock the whole list during these operations. The CopyOnWriteArrayList class works according to its name i.e. copy-on-write which performs different actions for reading and write operations. For every write operation (add, set, remove, etc), it makes a new copy of the elements in the list. and for the read operations (get, iterator, listIterator, etc), it works on a different copy. So there is no additional overhead during a read operation and its read operation is faster than Collections.SynchronizedList(). Thus, COWAL is better for reading operation than Synchronized List. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  Collections.synchronizedList()  CopyOnWriteArrayList | ||||
| ||||
Ans. 1. Arrays don't provide any built in support for sorting searching like operation and we need to do it explicitly. 2. It can hold only homogeneous objects, 3. We have to reserve memory as no dynamic expansion allowed. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  arrays  arraylist  arrays vs arraylist Asked in 3 Companies | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||