Java Coding Interview Questions – Collections

Interview questions topjavatutorial

This is first part of the coding interview questions. You may refer the complete list here :

Java Coding Interview Questions – Part 1

Java Coding Interview Questions – Part 2

Java Coding Interview Questions – Part 3

Java Coding Interview Questions – Part 4

Java Coding Interview Questions – Part 5

Java Coding Interview Questions – Part 6

 

1. What happens when you compile and run the below program?

 

package com.javatutorial.quiz;

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

public class Quiz38 {

  public static void main(String[] args) {

    List<Integer> list = new ArrayList<Integer>();
    
    Integer[] arr = {2,10,3};
    
    list = Arrays.asList(arr);
    
    list.set(0, 3);
    
    System.out.println(list);
    
    list.add(1);
    
    System.out.println(list);
  }

}

  

Output :

[3,10,3], followed by exception

 Arrays.asList() returns a fixed-size list backed by the specified array. Therefore, the arraylist can't grow.
So, when add() is called, an exception is thrown.

 

2. What will be output of following program ?

 

package com.topjavatutorial;

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

public class ArrayListDemo {

  public static void main(String[] args) {

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

}


  

Output:

21


ArralyList can contain duplicate elements. 
ArrayList remove() method only removes the first occurrence of a matching element.

 

3. What will be the output of following Java quiz on PriorityQueue ?

 

package com.topjavatutorial;

import java.util.PriorityQueue;

public class PriorityQueueExample {

  public static void main(String[] args) {
    PriorityQueue<String> pQueue = new PriorityQueue<String>();
    pQueue.add("Apple");
    pQueue.add("Nokia");
    pQueue.add("Samsung");
    pQueue.add("Apple");
    
    System.out.print(pQueue.poll() + " " + pQueue.poll());
    System.out.print(" " + pQueue.peek() + " " + pQueue.poll());
  }

}


 

Output:

“Apple Apple Nokia Nokia”

PriorityQueue keeps elements sorted and it can have duplicates.

add() and offer() methods both offer same functionality.

poll() method removes the first element in queue and returns it, while peek() method returns the first element without removing it.

 

4. What will be the result for below program?

 

package com.topjavatutorial;

public class Student {

  int rollNumber;
  
  Student(int n){
    rollNumber = n;
  }
}

package com.topjavatutorial;

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

public class HashSetDemo {

  public static void main(String[] args) {

    Set<Student> students = new HashSet<Student>();
    
    students.add(new Student(1));
    students.add(new Student(3));
    students.add(new Student(4));
    students.add(new Student(1));
    students.add(new Student(3));
    
    System.out.println(students.size());
  }

}

  

Output

5

Since Student doesn't override equals(), there are 5 objects in the HashSet.

 

5. Predict output of following program :

 

package com.topjavatutorial;

public class Employee implements Comparable<Employee>{

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

  @Override
  public int compareTo(Employee emp) {
    return this.name.compareTo(emp.name);
  }
  
}

package com.topjavatutorial;

import java.util.Comparator;

public class EmployeeComparator implements Comparator<Employee>{

  @Override
  public int compare(Employee emp1, Employee emp2) {
    
    return emp2.id - emp1.id;
  }

}


package com.topjavatutorial;

import java.util.TreeSet;

public class TreeSetDemo{

  public static void main(String[] args) {

    TreeSet<Employee> empTreeSet = new TreeSet<Employee>(new EmployeeComparator());
    
    Employee emp1 = new Employee(20, "Clark");
    Employee emp2 = new Employee(24, "Bernie");
    Employee emp3 = new Employee(3, "Alex");
    
    empTreeSet.add(emp1);
    empTreeSet.add(emp2);
    empTreeSet.add(emp3);
    
    
    for(Employee emp : empTreeSet)
          System.out.print(emp.name + " ");
    

  }

}

 

Output :

Bernie Clark Alex


Comparator takes precedence over Comparable, when both are implemented.

 

Next

Java Coding Interview Questions – Collections (Part 2)

You may also like :

 

Test your Java knowledge : 10 Popular and Tricky Java Puzzles
 
Best 25 Java articles on the web in 2015 (Worth Reading !!)
 
March 2016 Magazine : TopJavaTutorial articles
 
Java 8 Interview Questions
 
Top 10 Java Collection articles

 

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

4 comments

  1. […] Java Coding Interview Questions – Part 1 […]

  2. […] Java Coding Interview Questions – Part 1 […]

  3. Michael Ungermann

    The output for question is not “Apple Apple Nokia Nokia”. The correct answer is “Apple Apple Nokia Samsung”

    1. The correct answer is “Apple Apple Nokia Nokia”. Please run the program to verify.

      The third operation is peek(). It does not remove “Nokia”, but just returns it.

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

%d bloggers like this: