Welcome Guest | Sign in | Register
Collections - Discussion Page For Q.2568 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Collections > Questions and Answers

Q: What is the output for the below code?

import java.util.LinkedList;
import java.util.Queue;
public class Test {
public static void main(String... args) {
Queue q = new LinkedList();
q.add("newyork");
q.add("ca");
q.add("texas");
show(q);
}
public static void show(Queue q) {
q.add(new Integer(11));
while (!q.isEmpty ( ) )
System.out.print(q.poll() + " ");
}
}
A. Compile error : Integer can't be added B. newyork ca texas 11
C. newyork ca texas D. None of the above

Answer and Explanation

Answer:newyork ca texas 11

Explanation:
q was originally declared as Queue, But in show( ) method it is passed as an
untyped Queue. Nothing in the compiler or JVM prevents us from adding an Integer after
that.
If the show method signature is public static void show(Queue q) than you
can't add Integer, Only String allowed. But public static void show(Queue q) is untyped
Queue so you can add Integer.
Y poll( ) Retrieves and removes the head of this queue, or returns null if this queue is
empty.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.