This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Wildcards”.
1. Which of these is wildcard symbol?
a) ?
b) !
c) %
d) &
View Answer
Explanation: In generic code, the question mark (?), called the wildcard, represents an unknown type.
2. What is use of wildcards?
a) It is used in cases when type being operated upon is not known
b) It is used to make code more readable
c) It is used to access members of super class
d) It is used for type argument of generic method
View Answer
Explanation: The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
3. Which of these keywords is used to upper bound a wildcard?
a) stop
b) bound
c) extends
d) implements
View Answer
Explanation: None.
4. Which of these is an correct way making a list that is upper bounded by class Number?
a) List<? extends Number>
b) List<extends ? Number>
c) List(? extends Number)
d) List(? UpperBounds Number)
View Answer
Explanation: None.
5. Which of the following keywords are used for lower bounding a wild card?
a) extends
b) super
c) class
d) lower
View Answer
Explanation: A lower bounded wildcard is expressed using the wildcard character (‘?’), following by the super keyword, followed by its lower bound: super A>.
6. What will be the output of the following Java program?
import java.util.*;
class Output
{
public static double sumOfList(List<? extends Number> list)
{
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
public static void main(String args[])
{
List<Integer> li = Arrays.asList(1, 2, 3);
System.out.println(sumOfList(li));
}
}
a) 0
b) 4
c) 5.0
d) 6.0
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output 6.0
7. What will be the output of the following Java program?
import java.util.*;
class Output
{
public static double sumOfList(List<? extends Number> list)
{
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
public static void main(String args[])
{
List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
System.out.println(sumOfList(ld));
}
}
a) 5.0
b) 7.0
c) 8.0
d) 6.0
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output 7.0
8. What will be the output of the following Java program?
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
View Answer
Explanation: generic stack object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.javac
$ java Output
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Java Books
- Practice Programming MCQs
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Apply for Java Internship