click below
click below
Normal Size Small Size show me how
Java Collection
Question | Answer |
---|---|
What are collection in Java? | |
What is the difference between a Set and a List? | |
What is the difference between an Array and an ArrayList? | |
What is the difference between ArrayList and Vector? | |
What is the difference between TreeSet and HashSet? | |
What is the difference between HashTable and HashMap? | |
Are Maps in the Collections API? What makes Map different from other interfaces? | |
List several ways to iterate over a Collection. How would you iterate over a Map? | |
Which collection cannot hold null values? | set |
Why does HashTable not take null key? | |
Deletion is faster in LinkedList than ArrayList | true |
What is a Set interface? | Set is a collection of elements which cannot contain duplicate elements |
An array is a type of Collection in Java | false |
A < > comprises of sequence of nodes with each node containing a reference to its successor and can be used to implement Stacks and Queues | LinkedList |
Map implements the Collection interface | false Map does not implement Collection. Is related to Collection |
Because LinkedList in Java implement List, they have an index | true |
ArrayDeque<Integer> ad = new ArrayDeque<>(); ad.add(1); System.out.println(ad.remove()); | 1 |
What is the best case time complexity of add(element) method for ArrayList? | O(1) |
_______ adds elements at the end of the list and _______ adds elements from a specific position. | addAll(collection), addAll(int index, collection) |
_______ method is used to add single element at the end of the list. | add(element) |
________ method is used to add single element at the end of the list. | add(element) |
What is the time complexity of element() method for a PriorityQueue? | O(1) |
What is the time complexity of poll() method for a PriorityQueue? | O(1) |
What is the time complexity of offer(element) method for a PriorityQueue | O(log n) |
What is the time complexity of poll() method for ArrayDeque? | O(1) |
What is the time complexity of offer(element) method for ArrayDeque? | O(1) |
_____ is used to retrive an element from the rear end. | getLast() |
Dora needs a backpack all her items (more than one copy of the item may exist). She wants to get items immediately from her backpack. So, the backpack should be resizable and elements can be accessed in constant time. | ArrayList |
Which of the following is the non-synchronized version of the vector? | ArrayList |
Why is ArrayList preferred over a LinkedList? | ArrayList elements can be accessed at a constant time |
_____ follows the Last in First out principle and extends Vector. | Stack |
_____ is a legacy class so it has some methods which are not part of the collection framework | vector |
Which of the following classes do NOT implement List interface? | PriorityQueue |
Which of the following are NOT an operation performed by a list? | Truncate |
A _______ is an ordered collection of elements. | List |
ArrayDeque<Integer> ad = new ArrayDeque<>(); ad.push(4); System.out.println(ad.pop()); | 4 |
What happens when you try to add a key-value pair to an existing key in HashMap? | It overrides the value |
_____ is the set of key-value pairs from a Map. | entrySet |
Consider that a country stores its citizen's data in a form of Aadhar number and age. The country wants to vaccinate the older people first as they get easily infected by the virus. Which of the following best represents the scenario? | TreeMap with Aadhar number as key and age as value but ordered by value in descending order. |
HashMap allows a _______ null key and _______ null values. | single, multiple |
_____ doesn't store duplicate values and one key can have at most one value. | Map |
_____ is synchronized. It is thread-safe and can be shared with many threads. | HashTable |
______ stores elements before processing and normally queue follows the First in First out principle. | Queue |
_____ is a queue that stores the elements in general order or uses a comparator to sort the elements. | PriorityQueue |
_____ is similar to queue but it supports some operations like waiting for the queue to be non-empty to retrieve an element and waiting till the space is available to insert the elements. | BlockingQueue |
The _______ Interface Inherits the properties of Collection Interface, Queue Interface, and List Interface. | Deque |
There is a movie premiere and people should walk through the red carpet before they are allowed into the movie theatre. people walk on the carpet based on their importance, Which of the following best represents the following scenario? | PriorityQueue |
Consider there are two hash sets a and b with some elements in each, How to get the separate and the unique elements present in a but not b? | a.removeAll(b) |
What is the time complexity of remove(element) method for a HashSet? | O(1) |
What is the time complexity of add() method for a HashSet? | O(1) |
Set Inherits all the methods of Collection Interface like____ | All of the Above. addAll() removeAll() retainAll() |
A class which stores data in the form of node( data+ address of consecutive element) and its a part of List Interface. | LinkedHashSet |
HashSet<Integer> hs= new HashSet<>(); hs.add(1); hs.add(3); hs.add(2); hs.add(-1); TreeSet<Integer> ts = new TreeSet<>(hs); LinkedHashSet<Integer> lhs = new LinkedHashSet<>(ts); | Random order, [-1,1,2,3], [-1,1,2,3] |
_____ method generates the hashCode of two objects, if two objects are equal then the hash Code generated for two objects is the same, while the inverse may or may not be true. | hashCode() |
Set Interface is used to store unique elements of the same type, Classes that implements Set interface are____ | All of the above HashTable LinkedHashSet TreeSet |
A class that has key and values, it converts the keys into hashcode and stores them as the indexes of an array. What is that? | HashTable |
_____ stores, unique elements by retaining the order of elements. | TreeSet |
Features | EnumSet |
______ doesn’t maintain an ordered collection of elements. | HashSet |
Consider there are two hash sets a and b with some elements in each, How to get the intersection of a and b? | a.retainAll(b) |
Consider there are two hash sets a and b with some elements in each, How to check if a is a subset of b? | b.containsAll(a) |
The default implementation of the Object class generates different hash codes for different objects, even if they are equal according to the _______ method. | equals() |
What is a container or object that combines multiple elements into a single unit and is used to store, retrieve and manipulate data? | Collection |
T / F : Collection framework contains implementations for all the data structures? | true |
True or False: A comparable object is capable of comparing itself with another object. | true |
True or False: The concurrent collection APIs of Java provides a range of classes that are specifically designed to deal with concurrent operations. | true |
Which method is used to arrange elements in random order? | shuffle() |
The methods used by an iterator to traverse the collection are_____ | All of the above hasNext() next() remove() |
import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<>(); al.add(1); al.add(13); al.add(2); al.add(0); } } | for(Integer i:al) |
Given 2 HashMaps, what is the syntax to check if a Map is a sub-map of another Map. | System.out.println(Map1.entrySet().containsAll(Map2.entrySet())) |
State True or False. We use Generic to make it type safe. | true |
Which of the following are NOT common type parameters in Generics? | character |
True or False: An invocation of a generic type is generally known as a parameterized type. | true |
The _______ is known as the wildcard in generic programming. | Question mark(?) |
True or False: Best suitable wildcard conditions are based on the type of parameters passed to a method as in variable and out variable. | true |
True or False: Can java generics be applied to primitive types? | false |
What is the purpose of the Iterable interface? What about Iterator? | |
What is the difference between the Comparable and Comparator interfaces? | |
What are generics? | |
What is the diamond operator (<>)? | |
Create and instantiate a generic class. | |
Create and use a generic method. | |
Which collections cannot hold null values? | |
Why does HashTable not take null key? | |
What are Generics? | Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods |
You need a collection to represent secret agents. Every agent has a unique codename that identifies them, and an Agent object that represents them. | HashMap<String, Agent> |
A < > comprises a sequence of nodes with each node containing a reference to its successor and can be used to implement Stacks and Queuess | LinkedList |
Which of the following are benefits of an array over the ArrayList? | It can consume less memory more due to additional internal data structure and pointers, or less when array is only half full accessing an element in array is faster than ArrayList direct array access using an index is faster that calling a method |
T / F - An array is a type of Collection in Java | false |
< > will work on the streams and transform the single input value into the single output | map() |
What are the parts of the method reference? | class/object and method/constructor |
Which line is used to print all the elements in the list using method reference? | list.forEach(System.out::println); Using the forEach method to display the elements one by one in the list |
Can we pass additional parameters in method reference? | false |
Can we refer to the static method by calling its object? | false we can only refer to the static method by calling its name with the class where it resides |
A method of functional Interface is | Method Reference |
interface Sample{ void print(); } public class MethodReference { public static void printHello() { Syso } main() { < > sample.print(); } } | Sample sample = MethodReference::printhello; Here the static method references printhello() refers to its functional method print() in the interface Sample |
List<String> bookList = new ArrayList(); bookList.add("J P"); bookList.add("D S A"); bookList.add("P P"); Collections.sort(bookList, String::compareToIgnoreCase); bookList.forEach(System.out::println); | D S A J P P P This is an example of Reference to an instance method of an arbitrary object of a particular type. First, it will sort the list and apply compareToIgnoreCase to return the result |
Is Circle a functional interface? public interface Circle { public abstract void draw(); } | true there is only one abstract method |
How many default methods can we have in the functional interface? | many functional interface can have any number of default methods with only one abstract method |
Does lambda expression execute on its own? | false it is used to implement a method defined by a functional interface |
What are lambdas? | |
Which of the following are functional interfaces? | Runnable Callable Comparable |
Does the functional interface allows static methods? | true JDK 8 allows static methods in the interface, before this only one abstract method is allowed in functional interface |
@FunctionalInterface interface Arithmetic { void addition(String msg); void substraction(); } | Compile time error It will throw a compile time error that Arithmetic is not a functional interface, since it has 2 abstract methods |
< > is also called functional interfaces, having only one abstract method and multiple default methods | Single Abstract Method |
The Anonymous classes should have < > method so that it can be converted into lambda expression | only one abstract |
Which should be line 7 to print using lambda exp and functional interface? interface Greetings { void greet(); } main() Line 7 g.greet(); | Greetings g = () -> System.out.println("HelloWorld"); |
Is this possible to have default and static methods in the functional interface? | true We can have any number of default and static methods but can contain only one abstract method |
A functional interface can extend the interface only when there are < > in it | no abstract methods if it has an abstract method then it will be an invalid functional interface |
interface Single P void say(String msg); } @FunctionalInterface interface Double extends Single{ void doIt(); Syso; } | compile time error when a functional interface extends another interface it should not contain any abstract methods |
interface Single { void say(String msg); } @FunctionalInterface interface Double extends Single { void doIt(); System.out.println("welcome"); } | Compile time error When a functional interface extends another interface it should not contain any abstract methods |
Is there any limit for static and default methods in the functional interfaces? | false We can add any number of static and default methods in the functional interface in Java 8 |
Is @FunctionalInterface annotation is mandatory for every interface with a single abstract method? | false Not necessarily because the compiler will consider it as a functional interface when it has only one abstract methods |
Select the syntax for the Consumer Functional Interface? | Consumer<Integer> consumer = () -> System.out.println(value); |
< > is a Functional Interface but not in java.util.function package | Consumer |
Select the definition of Supplier Functional Interface in Java 8? | @FunctionalInterface public interface Supplier<T> ( T get(); } which does not take any input or argument and yet returns a single output |
public class AnonymousClassExample { main() { Runnable runnable = new Runnable() { public void run() { Sysout("Conversion"); } }; runnable.run(); } } | public class AnonymousClassExample { main() { Runnable runnable = new () -> { Sysout("Conversion"); }; runnable.run(); } } Functional interface can be instantiated using lambda expression instead of Anonymous class |
Is Functional Interface related to the lambda expression? | true The functional interface has been introduced in Java 8 to support the lambda expression, lambda expression is the instance of the functional interface |
If an interface annotated with @FunctionalInterface, java compiler ensures that interface has < > method | default |
public class Addition { main() { int x, y, sum; sum = sum(x,y); Sysout(" " + sum); } public static int Sum(int a, int b) { return a + b; } } | public class Main { main() { Sum sum = (a, b) -> a + b; Syso(sum.add(2,3)); } interface Sum { int add(int a, int b); } } |
What is a lambda expression? | A lambda expression is a short block of code that takes in parameters and returns a value. They do not need a name (function name) and they can be implemented right in the body of a method |
Which interface does not extend the Collection interface? | Map |
O(1) | constant time ex HashMap, array access and update, pushing and popping elements from a stack |
O(n^2) | quadratic time, often brute force ex nested loops |
O(n) | linear time ex going through an array/linked list |
O(log(n))(log(n)) | logarithmic time ex binary search |