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. |
|
| ||||
Core Java - Interview Questions and Answers for 'List' - 54 question(s) found - Order By Rating | ||||
| ||||
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  observer   listener | ||||
| ||||
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 | ||||
| ||||
Ans. A List is an child interface of collection interface in java where as Linked list is and implementation class of List interface which has doubly linked as a underlying data structure | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  linkedlist  list vs linkedlist Asked in 1 Companies | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. Default size is 10. Regard how size will increase. Inside add() method there is one method called ensureCapacity() which will check list has enough size available. This ensureCapacity() method if found size of list need to increase then it will call grow() method which will create new list of extra size and copy existing list data to new one and return. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  list  default size of list Asked in 1 Companies | ||||
| ||||
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 | ||||
| ||||
Ans. Work with two pointers on the linked list - a slow pointer (increments by one node) and a fast pointer (increments by two nodes). If both of these pointers meet at the same node, then there is a cycle in the linked list. Otherwise, no cycle. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  linkedlist 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  arrays vs linkedlist | ||||
| ||||
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 | ||||
| ||||
This question was recently asked at 'Canopy Tax'.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  LinkedList Asked in 1 Companies | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); if(checkIfLoop(l1)){ System.out.println("There is a Loop"); } else { System.out.println("No Loop"); } } private boolean checkifLoop(Test l1) { Node slow = start; Node fast = start; Node faster = start; while(slow != null ) { fast = slow.nextNode; faster = fast.nextNode; if(slow == fast || slow == faster) { return true; } slow = slow.nextNode; } return false; } } Complexity of this algorithm is O(n) | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  LinkedList  Cyclic LinkedList | ||||
| ||||
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. No,we cannot.I t will give concurrentModificationExceptin error. It can be resolved by using ConcurrentClasses like ConcurrentHashMap,CopyOnWriteArrayList,BlockingQueue etc which are fail-safe and wont give exception. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  collections  list  iterator Asked in 1 Companies basic | ||||
| ||||
This question was recently asked at 'adobe'.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  vector  collections  list Asked in 1 Companies | ||||
| ||||
Ans. list.get(index); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  list Asked in 1 Companies | ||||
| ||||
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 'Ancestry'.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  linkedlist  internal implementation of linkedlist 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. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } private void addNodeToEnd(Integer value) { if (start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while (head.nextNode != null) { head = head.nextNode; } head.nextNode = new Node(value); } private void deleteNode(Integer value) { head = start; while (head.nextNode != null) { if(head.nextNode.body == value){ head.nextNode = head.nextNode.nextNode; } head = head.nextNode; } } private void traverse() { head = start; while (head != null) { System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); ll.deleteNode(10); ll.traverse(); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  linkedlist  delete node from linkedlist | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node{ Integer body; Node nextNode; Node(Integer value){ body = value; } } private void insertInMiddle(Integer value){ head = start; if(start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while(head.body < value){ if(head.nextNode == null || head.nextNode.body >= value){ Node newNode = new Node(value); newNode.nextNode = head.nextNode; head.nextNode = newNode; break; } head = head.nextNode; } } private void traverse(){ head = start; while(head != null){ System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args){ LinkedList ll = new LinkedList(); ll.insertInMiddle(5); ll.insertInMiddle(10); ll.insertInMiddle(15); ll.insertInMiddle(7); ll.traverse(); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  LinkedList  Data structures  Algorithm | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } private void addNodeToEnd(Integer value) { if (start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while (head.nextNode != null) { head = head.nextNode; } head.nextNode = new Node(value); } private void traverse() { head = start; while (head != null) { System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  LinkedList  Data structures  Algorithm | ||||
| ||||
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. 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 | ||||
| ||||
This question was recently asked at 'Caprus IT'.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  linkedlist  delete a node from linked list Asked in 1 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 | ||||