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 Newest

next 30
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q1. What is the difference between ArrayList and LinkedList ?Core Java
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   arraylist vs linkedlist     Asked in 61 Companies      Basic        frequent

Try 1 Question(s) Test


Frequently asked.
 Q2. If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ?Core Java
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.

  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     java   collections   list   arraylist   linkedlist   difference between   architecture   data structure   ebay     Asked in 2 Companies      basic        frequent

Try 2 Question(s) Test


 Q3. 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");
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


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


Frequently asked in Indian Service Companies. Tech Mahindra ( Based on 3 inputs ) and Infosys ( 3 inputs )
  Q5. What is the difference between List, Set and Map ?

or

What are the different Java Collections Interfaces ?
Core Java
Ans. List - Members are stored in sequence in memory and can be accessed through index.
Set - There is no relevance of sequence and index. Sets doesn't contain duplicates whereas multiset can have duplicates.
Map - Contains Key , Value pairs.

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

   Like         Discuss         Correct / Improve     java   collections   list   set   map   list vs set vs map     Asked in 8 Companies      basic        frequent

Try 1 Question(s) Test


  Q6. Difference between Vector and ArrayList ?Core Java
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


 Q7. Example of Observer Design Pattern ?
Ans. Listeners.

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

   Like         Discuss         Correct / Improve     java   design pattern   listeners   observer design pattern      expert


 Q8. Difference between ArrayList and LinkedList ?
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


 Q9. Find the third last element in a linked list ?Data Structure
Ans. First Find the number of nodes in the linked list and take it as n.then find the data at n-3 element.

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

   Like         Discuss         Correct / Improve     linkedlist     Asked in 1 Companies


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


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


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


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


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


 Q15. What is a Listener ?Design
Ans. In GUI programming, an object that can be registered to be notified when events of some given type occur. The object is said to listener? for the events.

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

   Like         Discuss         Correct / Improve     java   gui   ui programming   swing   awt   swt   listener   architecture


 Q16. Is it legal to initialize List like this ?


LinkedList l=new LinkedList();
Ans. No, Generic parameters cannot be primitives.

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

   Like         Discuss         Correct / Improve     java   collections   list   linkedlist   generics   yes-no


 Q17. Which of the following syntax are correct ?a. LinkedList<Integer> l=new LinkedList<int>();b. List<Integer> l=new LinkedList<int>();c. LinkedList<Integer> l=new LinkedList<Integer>();d. List<Integer> l = new LinkedList<Integer>();Core Java
Ans. All are correct.

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

   Like         Discuss         Correct / Improve     java   generics   linkedlist   list   collections      basic


 Q18. What is CopyOnWriteArrayList ?
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


 Q19. What are the advantages and disadvantages of CopyOnWriteArrayList ?Core Java
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


 Q20. Name few classes that implement List interface ?
Ans. http://www.buggybread.com/2015/02/java-collections-classes-that-implement_35.html

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

   Like         Discuss         Correct / Improve     java   collections   list


 Q21. 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));
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


 Q22. 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));
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


 Q23. How to determine if the linked list has a cycle in it ?
Ans. http://stackoverflow.com/questions/494830/how-to-determine-if-a-linked-list-has-a-cycle-using-only-two-memory-locations

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

   Like         Discuss         Correct / Improve     linked list   data structure   algorithm   java   ebay


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


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


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


 Q27. What is the difference between Collections.emptyList() and creating new instance of List using new ?Core Java
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.

Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception.

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

   Like         Discuss         Correct / Improve     collections  Collections.emptyList  immutable  immutability  immutability


  Q28. Difference between Array and ArrayList ?Core Java
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


 Q29. Write program to create a linked list and perform different operations on it.Algorithm
Ans. import java.util.*;
class LinkedListSolution{
protected LinkedList list;
public LinkedListSolution(){
list = new LinkedList();
}
public Object pop() throws NoSuchElementException{
if(list.isEmpty())
throw new NoSuchElementException();
else
return list.removeFirst();
}
public void push(Object obj){
list.addFirst(obj);
}
public Object peek() throws NoSuchElementException{
if(list.isEmpty())
throw new NoSuchElementException();
else
return list.getFirst();
}
public boolean isEmpty(){
return list.isEmpty();
}
public String toString(){
return list.toString();
}
}
class TestStack{
public static void main(String args[]){
LinkedListSolution s = new LinkedListSolution();
s.push("First");
s.push("Second");
s.push("Third");
System.out.println("Top: " s.peek());
s.push("Fourth");
while(!(s.isEmpty()))
System.out.println(s.pop());
}
}

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

   Like         Discuss         Correct / Improve     Linkedlist  data structures  algorithm     Asked in 2 Companies      basic


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


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: