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


Explain some of the methods on the Queue Interface


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

Explain some of the methods on the Queue Interface.
Answer:
The following are some of the methods on the Queue Interface:
offer(E e): This adds an object to the queue. It does not
throw an Exception if it is not possible to add the object to the
queue.


b.
c.
d.
e.
154.
peek(): This returns the last added object in the queue.
poll(): This returns the last added object in the queue and
removed it from the queue.
remove(Object O): This removes the object from the queue.
add(E e): This adds an object to the queue. This method
throws an Exception if it is not possible to add the object to
the queue.
Is it possible to mix generic and non–generic collections?
If so, give example.
Answer:
Yes, it is possible to mix generic and non–generic collections.The
following code snippet demonstrates this:
public void insertDouble() {
List myDoubleList = new ArrayList();
myDoubleList.add(2.0); // Line 1
myDoubleList.add(4.0); // Line 2
insertSomethingElse(myDoubleList);
}
public void insertSomethingElse(List myDoubleList)
{//Line 3
myDoubleList.add(“Hello World”); //Line 4
}
In the above code, Line 1 and Line 2 add double literals to the list
which is a List with generic Double specified. The code then
invoked the insertSomethingElse() method with this double
List. In the method declaration at Line 3, the generic type is not
specified for the myDoubleList variable. So, any type of value
can be added to this List. Line 4 adds a string literal into the List.


155.
a.
b.
c.
d.
e.

Download 1.52 Mb.

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




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