Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Which collection implementation allows growing or


Download 1.52 Mb.
Pdf ko'rish
bet58/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   54   55   56   57   58   59   60   61   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Which collection implementation allows growing or
shrinking its size and provides indexed access to its
elements?
Answer:
ArrayList is an implementation of the List interface. It allows
growing or shrinking its size. It has add/remove methods that
increase or decrease the size of the List. It also provides indexed
access to its elements. It has a get method to achieve this. The
following code demonstrates an ArrayList:
List myList = new ArrayList();
myList.add(2);
myList.add(4);
myList.add(6);
myList.remove(1);
Integer num = myList.get(0);
Explain the Queue interface.


152.
153.
a.
Answer:
The Queue interface is part of the Collection API and extends the
java.util.Collection interface. In addition to Collection operations,
Queue provide features of a queue data structure like FIFO (First
in First Out). The Queue interface has several methods that allow
queue operations:
peek(): allows inspecting the element at the top of the queue
poll(): removes the element at the top of the queue
offer(): insert an element into the queue
Explain the Comparator interface.
Answer:
Comparator is an interface that can be used for sorting. It has
been designated as a functional interface since Java 8. It has a
single abstract method called compare. The compare method
accepts two objects and returns a boolean value that is the result
of comparison. Comparator can also be used to sort custom
object. For this, the corresponding class needs to implement the
Comparator interface and provide an implementation for the
compare method. If you are using Java
8 or a higher version, you can also implement a Comparator
using a lambda expression.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   54   55   56   57   58   59   60   61   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling