Java Multiple Choice Questions & Answers on Wildcards for Freshers

https://www.computersprofessor.com/2018/01/java-multiple-choice-questions-answers_30.html
1. Which of these is wildcard symbol?
a) ?
b) !
c) %
d) &
Answer: a
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
Answer: a
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
Answer: c
4. Which of these is an correct way making a list that is upper bounded by class Number?
a) List
b) List
c) List(? extends Number)
d) List(? UpperBounds Number)
Answer: a
5. Which of the following is incorrect statement regarding the use of generics and parameterized types in Java?
a) Generics provide type safety by shifting more type checking responsibilities to the compiler.
b) Generics and parameterized types eliminate the need for down casts when using Java Collections.
c) When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes.
d) All of the mentioned
Answer: c
6. Which of the following keywords are used for lower bounding a wild card?
a) extends
b) super
c) class
d) lower
Answer: b
Explanation: A lower bounded wildcard is expressed using the wildcard character (‘?’), following by the super keyword, followed by its lower bound: .
7. What is the output of this 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
b) 4
c) 5.0
d) 6.0
Answer: d
8. What is the output of this 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
b) 7.0
c) 8.0
d) 6.0
Answer: b
9. What is the output of this program?
import java.util.*;
class Output {
public static void addNumbers(List super Integer> list) {
for (int i = 1; i <= 10; i++) {
list.add(i);
}
}
public static void main(String args[]) {
List<Double> ld = Arrays.asList();
addnumbers(10.4);
System.out.println("getList(2)");
}
}
a) 1
b) 2
c) 3
d) 6
b) 2
c) 3
d) 6
Answer: a
10. What is the output of this 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
b) Hello
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.