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 Rating | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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 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. <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 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 Asked in 1 Companies | ||||
| ||||
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. 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   vector vs arraylist Asked in 35 Companies basic   frequent | ||||
| ||||
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 | ||||
| ||||
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. 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 | ||||
| ||||
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 Asked in 6 Companies basic   frequent | ||||
| ||||
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 | ||||
| ||||
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 | ||||