Free Java Quiz Questions With Answers – Prepare Yourself For Interviews

1. Online Java Quiz Questions

Today, we will move towards Java quiz questions. These Java quiz questions will brush up your concepts of java and prepare you to face Java interview. This Java quiz contains multiple choice questions for java. Answers to all these Java quiz questions and follow the relevant link to get the deeper knowledge of Java.

So, let’s start exploring Java Quiz Questions.

Java Quiz Questions

Free Java Quiz Questions With Answers

Q.1 Which Set class should be most popular in a multi-threading environment, considering performance constraint?

Correct! Wrong!

Q.2 Which Map class should be most popular in a multi-threading environment, considering performance constraint?

Correct! Wrong!

Q.3 Which allows the removal of elements from a collection?

Correct! Wrong!

Q.4 What will be the output of given code
import java. util.Arrays; 
import java. util.Comparator;
class Sorting implements Comparator
{ 
    public int compare(Integer o1, Integer o2)
    {
        return o2. compareTo(o1);
    }
}
public class abc {
     public static void main (String...a)
     {
         Integer intArray[]= {2,3,1};
         Arrays.sort(intArray, new Sorting()); 
         for(int i: intArray)
             {
             System. out. print(i+"");
             }
     }
}

Correct! Wrong!

Q.5 What will be the output of the following code
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class ConcurrentSkipListMapTest {
	 public static void main (String args)
	 {
		 Set set = new TreeSet();
		 set.add(1);
		 set.add("2");
		 set.add(3);
		 
		 Iterator iterator = set.iterator();
		 while (iterator. hasNext())
		 {
			 System. out. print(iterator. next()+"");
		 }
	 }

}

Correct! Wrong!

Q.6 What will be the output of given code
import java.util.ArrayList;
import java.util. Iterator;
import java.util. List;

public class ArrayListTest {
	public static void main (String args[])
	{
		List arrayList = new ArrayList();
		
		arrayList.add("a"); 
		arrayList.add("b");
		
		Iterator iterator = arrayList.iterator(); 
		
		while (iterator.hasNext()) 
		{
			System.out.println(iterator.next());
			arrayList.add("c");
		}
	}
}

Correct! Wrong!

Q.7 What will be the output of given code
import java.util.ArrayList;
import java.util.Collections; 
import java.util.Comparator; 

class Employee { 
	String name; 
	public Employee() {}
	public Employee (String name) { 
		this.name = name;
	}
	public String toString()
	{
		return "name=" + name;
	}
	static class ComparatorName implements Comparator
	{
		public int compare (Employee obj1, Employee obj2) 
	{
		return obj1.name.compareTo(obj2.name);
	}
	
	}
}
public class ComparatorUsageDemo {
	  public static void main (String[] args)
	  {
		  Employee emp1 =new Employee("ankit"); 
		  Employee emp2= new Employee("brad");
		  
		  ArrayList list=new ArrayList();
		  {
			  list.add(emp1);
			  list.add(emp2);
			  
			  Collections.sort(list,new Employee.ComparatorName());
			  System. out. println (list) ;  
		  }
		  		
	  }
}

Correct! Wrong!

Q.8  What will be the output of the following code
import java.util.HashSet;
import java.util.Set;
public class HashSetTest 
{ 
	public static void main (String args[]) 
	{
		Set hashSet = new HashSet();
		hashSet.add("1");
		hashSet.add(1);
		hashSet.add(null);
		hashSet.add("null");
		System.out.println(hashSet);
}


}

Correct! Wrong!

Q.9 What will be the output of the following code
import java.util.IdentityHashMap;
import java.util.Map;

	  public class MyClass 
	  { 
		  public static void main(String args[]) 
		  { 
			  Map identityHashMap = new IdentityHashMap();
			  identityHashMap.put(new String("a"),"audi");
			  identityHashMap.put(new String("a"),"ferrari");
			  System.out.println(identityHashMap);
	  }
}

Correct! Wrong!

Q.10 What will be the output of the following code
import java.util.HashMap;
import java.util.Map;

	  public class MyClass 
	  { 
		  public static void main(String args[]) 
		  { 
			  Map hashMap = new HashMap();
			  hashMap.put(new String("a"),"audi");
			  hashMap.put(new String("a"),"ferrari");
			  System.out.println(hashMap);
	  }
}

Correct! Wrong!

Q.11 Which permits the removal of elements from a collection?

Correct! Wrong!

Q.12 The Comparator interface contains the method?

Correct! Wrong!

Q.13 Which of those is synchronized?

Correct! Wrong!

Q.14 Which offers the best performance?

Correct! Wrong!

Q.15 Which of these is the most popularly used class as a key in a HashMap?

Correct! Wrong!

16 What should we use when add and remove operations are more frequent than get operations

Correct! Wrong!

Q.17 Iterator returned by ArrayList is?

Correct! Wrong!

Q.18 Iterator returned by LinkedList is?

Correct! Wrong!

Q.19 Iterator returned by CopyOnWriteArrayList is?

Correct! Wrong!

Q.20 Iterator returned by HashMap on key, value and entry are?

Correct! Wrong!

See also – 

Java Quiz Part 4

Java Quiz Part 5

2 Responses

  1. Saravanan K says:

    For question number 10 the answer and options are not valid.

    • DataFlair Team says:

      Hey Saravanan,
      Thanks for connecting to DataFlair, the que.10 and its answer is verified by our experts, which is absolutely correct. You can run the code in Java and get the answer {a=ferrari}.
      Hope, it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.