Java Questions & Answers – Type Interface

This section of our 1000+ Java MCQs focuses on Type interface in Java Programming Language.

1. Why are generics used?
a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs detectable at compile time
d) Generics add stability to your code by making more of your bugs detectable at runtime
View Answer

Answer: c
Explanation: Generics add stability to your code by making more of your bugs detectable at compile time.

2. Which of these type parameters is used for a generic class to return and accept any type of object?
a) K
b) N
c) T
d) V
View Answer

Answer: c
Explanation: T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

3. Which of these type parameters is used for a generic class to return and accept a number?
a) K
b) N
c) T
d) V
View Answer

Answer: b
Explanation: N is used for Number.
advertisement
advertisement

4. Which of these is an correct way of defining generic class?
a) class name(T1, T2, …, Tn) { /* … */ }
b) class name<T1, T2, …, Tn> { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) class name{T1, T2, …, Tn} { /* … */ }
View Answer

Answer: b
Explanation: The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, …, and Tn.

5. Which of the following is an 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
View Answer

Answer: c
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Which of the following reference types cannot be generic?
a) Anonymous inner class
b) Interface
c) Inner class
d) All of the mentioned
View Answer

Answer: a
Explanation: None.

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

advertisement
  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, java.util.List<Box<U>> boxes)
  4.         {
  5.            Box<U> box = new Box<>();
  6.            box.set(u);
  7.            boxes.add(box);
  8.         }
  9.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  10.         {
  11.             int counter = 0;
  12.             for (Box<U> box: boxes)
  13.             {
  14.                 U boxContents = box.get();
  15.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  16.                 counter++;
  17.             }
  18.         }
  19.         public static void main(String[] args)
  20.         {
  21.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  22.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  23.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  24.         }
  25.     }

a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac Output.javac
$ java Output
Box #0 contains [10].

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

  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, 
  4.         java.util.List<Box<U>> boxes)
  5.         {
  6.            Box<U> box = new Box<>();
  7.            box.set(u);
  8.            boxes.add(box);
  9.         }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes) 
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes) 
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("[" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 0
b) 1
c) [1]
d) [0]
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
$ java Output
[0]

9. 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 <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
$ java Output
Hello 36

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

  1.     public class BoxDemo 
  2.     {
  3.         public static <U> void addBox(U u, 
  4.         java.util.List<Box<U>> boxes) 
  5.         {
  6.            Box<U> box = new Box<>();
  7.            box.set(u);
  8.            boxes.add(box);
  9.         }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes) 
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }        
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
$ java Output
Box #0 contains [10].

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.