250+ TOP MCQs on Type Interface and Answers

Java MCQs 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

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

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

Answer: b
Clarification: N is used for Number.

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

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

Answer: c
Clarification: None.

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

Answer: a
Clarification: None.

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

  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]

Answer: d
Clarification: None.
Output:

$ 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]

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

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

Answer: d
Clarification: None.
Output:

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

Leave a Reply

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