250+ TOP MCQs on Wildcards and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Wildcards”.

1. Which of these is wildcard symbol?
a) ?
b) !
c) %
d) &

Answer: a
Clarification: 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
Clarification: 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
Clarification: None.

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
Clarification: None.

5. Which of the following keywords are used for lower bounding a wild card?
a) extends
b) super
c) class
d) lower

Answer: b
Clarification: 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?

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static double sumOfList(List extends Number> list)
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.             List<Integer> li = Arrays.asList(1, 2, 3);
  14.             System.out.println(sumOfList(li));
  15.         }
  16.     }

a) 0
b) 4
c) 5.0
d) 6.0

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
6.0

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static double sumOfList(List extends Number> list)
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[]) 
  12.         {
  13.            List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
  14.            System.out.println(sumOfList(ld));
  15.         }
  16.     }

a) 5.0
b) 7.0
c) 8.0
d) 6.0

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
7.0

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: d
Clarification: 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

Leave a Reply

Your email address will not be published. Required fields are marked *