Java Coding Interview Questions – Collections (Part 2)

This post is a continuation of the Java Coding Interview Questions on Collections :

Java Coding Interview Questions – Collections

Java coding interview questions

1. What will be the output of following program on ArrayList ?

package com.topjavatutorial;

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo2 {

  public static void main(String[] args) {

    List<Integer> list = new ArrayList<Integer>();
    
    list.add(10);
    list.add(10);
    
    System.out.print(list.size());
    
    list.remove(10);
    
    System.out.print(list.size());
  }

}


  

Output

Print 2 followed by IndexOutOfBoundsException


ArrayList define the overloaded remove() methods as follows :

- remove(int index)
- remove(Object object)

list.remove(10) will try to remove the element at array position 10 and hence throw IndexOutOfBoundException.

It won't auto-box the int value 10 to Integer and try to remove it.

 

2. Predict the output of following program :

package com.topjavatutorial;

import java.util.HashSet;
import java.util.Set;

public class ShortTest {

  public static void main(String[] args) {
    Set<Short> set = new HashSet<Short>();

    for (Short i = 0; i < 10; i++) {
      set.add(i);
      set.remove(i - 1);
    }

    System.out.println(set.size());
  }

}

  

Output

10


The set contains Shorts and we are trying to remove integers from it. i-1 is integer.

  

3. What will be the output of following program ?

package com.topjavatutorial;

import java.util.ArrayDeque;

public class ArrayDequeDemo {

  public static void main(String[] args) {

    ArrayDeque<String> adq = new ArrayDeque<String>();
    
    adq.add("A");
    adq.push("B");
    adq.addFirst("C");
    adq.offer("D");
        
    System.out.print(adq.peek() + " " + adq.pop() + " " + adq.poll());
    
  }

}


  

Output

C C B


push() and addFirst() add elements at front of queue and add() and offer() add elements at end of queue.

peek() just retrieves the element, does not remove it.

pop() and poll() remove from front of queue.


Refer following post to understand ArrayDeque operations:

Understanding ArrayDeque operations in java
 

4. What will be the output of following program ?

package com.topjavatutorial;

import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    List<String> ll = new LinkedList<String>();

    ll.add("C"); 
    ll.push("B"); 
    ll.addFirst("A");
    ll.offer("D");

    System.out.print(ll.remove() + " ");

    System.out.print(ll.poll());

  }

}


  

Output

Compilation error


Methods offer(), push(), addFirst(), poll(),remove() are available to LinkedList since it implements Deque alongwith List interface. List interface doesn't define these methods.

Without an explicit cast, a reference variable of an interface can only access the variables and methods defined in the interface.

 

5. What will be the output of following program ?

package com.topjavatutorial;

public class Employee {

  private int id;
  
  private String name;
  
  Employee(int id, String name){
    this.id = id;
    this.name = name;
  }
  
}


 
package com.topjavatutorial;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveDemo {

  public static void main(String[] args) {
    
    List<String> countries = new ArrayList<String>();
    
    countries.addAll(Arrays.asList("Australia","Canada","India","USA"));
            
    countries.remove(new String("USA"));
    
    System.out.print(countries.size());

    List<Employee> empList = new ArrayList<Employee>();
    
    empList.add(new Employee(1,"A"));
    empList.add(new Employee(1,"B"));
    empList.add(new Employee(1,"C"));
            
    empList.remove(new Employee(1,"A"));
    
    System.out.print(empList.size());
    
  }

}


  

Output

33


remove() method in ArrayList uses equals() method to find element for removing.

String class overrides equals() method and strings are compared based on values.

But the custom Employee class does not override equals() and default equals() method compares elements are compared by reference.

Hence the string will be removed from countries list, but employee will not be removed from empList.

 

You may also like :

Java Coding Interview Questions – Collections

Java Interview Questions for Experienced Professionals

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags