




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















4) What is the hashCode()?
5) Distinguish between ArrayList and Vector in the Java collection framework. ArrayList Vector ArrayList is cannot be synchronized. Vector can be is synchronized. It is not a legacy class. It is a legacy class. It can increase its size by 50% of the size of the array. It can increase its size by doubling the size of the array. ArrayList is not thread-safe. Vector is a thread-safe. 6) What is ArrayList in Java?
public interface BlockingQueue
11) What is the difference between Comparable and Comparator?
Comparable Comparator Comparable provides compareTo() method to sort elements in Java. Comparator provides compare() method to sort elements in Java. Comparable interface is present in java.lang package. Comparator interface is present in java. util package. The logic of sorting must be in the same class whose object you are going to sort. The logic of sorting should be in a separate class to write different sorting based on different attributes of objects. The class whose objects you want to sort must implement the comparable interface. Class, whose objects you want to sort, do not need to implement a comparator interface. It provides single sorting sequences. It provides multiple sorting sequences. This method can sort the data according to the natural sorting order. This method sorts the data according to the customized sorting order. It affects the original class. i.e., the actual class is altered. It doesn’t affect the original class, i.e., the actual class is not altered. Implemented frequently in the API by Calendar, Wrapper classes, Date, and String. It is implemented to sort instances of third-party classes. All wrapper classes and String class implement the comparable interface. The only implemented classes of Comparator are Collator and RuleBasedColator.
12) Explain equals() with example
public boolean equals(Object o)
import java.lang.Integer; public class Test { public static void main(String args[]) { Integer p = 5; Integer q = 20; Integer r =5; Short s = 5; System.out.println(p.equals(q)); System.out.println(p.equals(r)); System.out.println(p.equals(s)); } } 13) List out benefits of generic collection
//Remove element by index arlTest.remove(2); System.out.println("See contents after removing element by index: " + arlTest); //Check size after removing elements System.out.println("Size of arrayList after removing elements: " + arlTest.size()); System.out.println("List of all elements after removing elements: " + arlTest); //Check if the list contains "K" System.out.println(arlTest.contains("K")); } } 16) Give example to sort an array in dscending order
package com.guru99; public class SelectionSortAlgo { public static void main(String a[]) { int[] myArray = {860,8,200,9}; System.out.println("------Before Sort-----"); printArray(myArray); selection(myArray);//sorting array using selection sort System.out.println("-----After Sort-----"); printArray(myArray); } public static void selection(int[] array) { for (int i = 0; i < array.length - 1; i++) { System.out.println("Sort Pass Number "+(i+1)); int index = i; for (int j = i + 1; j < array.length; j++) {
System.out.println("Comparing "+ array[index] + " and " + array[j]); if (array[j] < array[index]){ System.out.println(array[index] + " is greater than " + array[j] ); index = j; } } int smallerNumber = array[index]; array[index] = array[i]; array[i] = smallerNumber; System.out.println("Swapping Elements: New Array After Swap"); printArray(array); } } static void printArray(int[] array){ for(int i=0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); } } 17) Explain the basic interfaces of the Java collections framework
20) What is linked list?
21) Give example of ArrayList
import java.util.ArrayList; class Test_ArrayList { public static void main(String[] args) { //Creating a generic ArrayList ArrayList
arlTest.remove("D"); System.out.println("See contents after removing one element: " + arlTest); //Remove element by index arlTest.remove(2); System.out.println("See contents after removing element by index: " + arlTest); //Check size after removing elements System.out.println("Size of arrayList after removing elements: " + arlTest.size()); System.out.println("List of all elements after removing elements: " + arlTest); //Check if the list contains "K" System.out.println(arlTest.contains("K")); } } 22) Explain linked list supported by Java
23) Explain the methods provided by the Queue interface?
Method Description boolean add(object) Inserts specified element into the Queue. It returns true in case it a success.
Collection Collections The collection is an interface. Collections is a class. It represents a group of objects as a single entity. It defines various utility methods for collection objects. The collection is the root interface of the Java Collection framework. Collections is a general utility class. This interface is used to derive the collection data structures. This class contains static methods to manipulate data structure. 27) Define LinkedHashSet in the Java Collection framework?
28) What is the difference between failfast and failsafe? Failfast Failsafe It does not allow collection modification while iterating. It allows collection modification while iterating. It can throw ConcurrentModificationException It can’t throw any exception. It uses the original collection to traverse the elements. It uses an original collection copy to traverse the elements. There is no requirement of extra memory. There is a requirement of extra memory. 29) List collection views of a map interface
30) What are the benefits of the Collection Framework in Java?
31) What is a good way to sort the Collection objects in Java?
32) Explain Vector in Java
33) What is the difference between Set and Map? Set Map Set belongs to package-java.util. The map belongs package- java.util. It can extend the collection interface. It does not extend the collection interface. It does not allow duplicate values. It allows duplicate values. Set can sort only one null value. The map can sort multiple null values.
39) What are the methods to make collection thread-safe?
40) Explain UnsupportedOperationException
41) Name the collection classes that gives random element access to its elements
42) Explain the difference between Queue and Deque. Queue Deque
It is called a single-ended Queue It is called a double-ended Queue Elements in the Queue are added or removed from one end Elements in the Queue are added from either end can be added and removed from the both end It is less versatile. It is more versatile. 43) Mention the implementing List and Set interface
44) Explain the design pattern followed by Iterator
45) What is the peek() of the Queue interface?
46) What is CopyOnWriteArrayList?
47) Differentiate between ArrayList and LinkedList
iterator iterator() It returns an iterator over the elements in this set. int size(): It returns the number of elements available in the set. 50) What are the methods of Java TreeSet class?
Methods Descriptions boolean addAll(Collection c) Add all the elements in the specified collection to this set. boolean contains(Object o) Returns true if the set contains the mention element. boolean isEmpty() This Java method returns true if this set contains no elements. boolean remove(Object o) Remove the specified element from the set. void add(Object o) It adds the specified element to the set. void clear() This Java method removes all the elements from the set. 51) Explain Linked HashSet
52) What are the important methods used in a linked list?
Method Description boolean add( Object o) It is used to append the specified element to the end of the vector. boolean contains(Object o) It a method that returns true if this list contains the specified element. void add (int index, Object element) Inserts the element at the specified element in the vector.
void addFirst(Object o) It is used to insert the given element at the beginning. void addLast(Object o) It is used to append the given element to the end. Int size() This method can be used to return the total number of elements in a list. boolean remove(Object o) It can remove the first occurrence of the specified element from this list. int indexOf(Object element) This Java method returns the index with the first occurrence of the mention element in this list, or -1. int lastIndexOf(Object element) It is a Java method that returns the index with the last occurrence of the specified element in this list, or -1. 53) List various classes available in sets
54) List methods available in Java Queue interface
55) Differentiate between List and Set. List Set An ordered collection of elements An unordered collection of elements Preserves the insertion order Doesn’t preserves the insertion order Duplicate values are allowed Duplicate values are not allowed Any number of null values can be stored Only one null values can be stored