Java Questions & Answers – Wildcards

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

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
View Answer

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
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

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

Answer: a
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

Answer: b
Explanation: A lower bounded wildcard is expressed using the wildcard character (‘?’), following by the super keyword, followed by its lower bound: .
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac Output.javac
$ java Output
6.0

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

advertisement
  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
View Answer

Answer: b
Explanation: 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
View Answer

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.