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

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.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'List' - 54 question(s) found - Order By Rating

next 30
 Q1. What is the difference between closing and completing the observer / listener ?Angular
 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


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


 Q3. Difference between List and LinkedList ?Core Java
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


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


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


 Q6. What is the default size of list ? How it is growable ?Core Java
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


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


 Q8. Detect a loop in a linked listData Structure
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


 Q9. Difference between Arrays and LinkedList ?Data Structure
 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


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


 Q11. What can we do if we need to retrieve Fibonacci value at a particular index faster ? Can we use Linked List ?Data Structure
 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


 Q12. Write code to see if a Linked List has cycle in it ?

or

Write code using Floyd Algorithm to see if a linked list has cycle in it ? What is it's comlexity ?
Algorithm
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


 Q13. Which of the following - arrays or LinkedList allow elements to be accessed using index and how ? Data Structure
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


 Q14. Can we modify a list while iterating it ?Core Java
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


 Q15. When do you use a Vector?Core Java
 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


 Q16. What are the ways to get the values from list ?Data Structure
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


 Q17. Explain what happens when insertion is performed in case of ArrayList and LinkedList.Data Structure
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


 Q18. How is LinkedList collection class internally implemented ? Is it a Linked List or Doubly Linked List ? Core Java
 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


 Q19. Which of the two - Arrays or LinkedList - is a better data structure for implementing Queue ? and Why ?Data Structure
 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


 Q20. Write method to delete Node from a LinkedList.Data Structure
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


 Q21. In a Linked list with sorted numbers, insert a new numbers while maintaining the sort order.Algorithm
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


 Q22. Write a program for LinkedList, with method to append node and traversing the list ?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


 Q23. What does the following initialization mean ?

ArrayList<LinkedList> traversalPaths = new ArrayList<LinkedList>();

What could be the use of such a collection.
Core Java
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


 Q24. What is the quickest way to find count of duplicate elements in an arraylist, without using iteration or loops ?Core Java
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


 Q25. What problem we could have with ArrayList which aren't possible with Vectors ?Core Java
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


 Q26. What is meant by "Vector is synchronized and ArrayList isn't " ?Core Java
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


 Q27. Why iterators of an array list are fail fast ?Core Java
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. why aren't arrays better than array list ?Core Java
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


 Q29. Write a Program to delete a node from Linked List ?Data Structure
 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


 Q30. What is the trade off between using Collections.synchronizedList() on an ArrayList and a CopyOnWriteArrayList?Core Java
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


next 30

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: