Java MCQs on Type interface in Java Programming Language.
1. Why are generics used? Answer: c 2. Which of these type parameters is used for a generic class to return and accept any type of object? Answer: c 3. Which of these type parameters is used for a generic class to return and accept a number? Answer: b 4. Which of these is an correct way of defining generic class? Answer: b 5. Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java? Answer: c 6. Which of the following reference types cannot be generic? Answer: a 7. What will be the output of the following Java program? a) 10 8. What will be the output of the following Java program? a) 0 9. What will be the output of the following Java program? a) Error 10. What will be the output of the following Java program? a) 10
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
Clarification: Generics add stability to your code by making more of your bugs detectable at compile time.
a) K
b) N
c) T
d) V
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.
a) K
b) N
c) T
d) V
Clarification: N is used for Number.
a) class name(T1, T2, …, Tn) { /* … */ }
b) class name
c) class name[T1, T2, …, Tn] { /* … */ }
d) class name{T1, T2, …, Tn} { /* … */ }
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.
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
Clarification: None.
a) Anonymous inner class
b) Interface
c) Inner class
d) All of the mentioned
Clarification: None.
public class BoxDemo
{
public static <U> void addBox(U u, java.util.List<Box<U>> boxes)
{
Box<U> box = new Box<>();
box.set(u);
boxes.add(box);
}
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
{
int counter = 0;
for (Box<U> box: boxes)
{
U boxContents = box.get();
System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
counter++;
}
}
public static void main(String[] args)
{
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
BoxDemo.outputBoxes(listOfIntegerBoxes);
}
}
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
Clarification: None.
Output:
$ javac Output.javac
$ java Output
Box #0 contains [10].
public class BoxDemo
{
public static <U> void addBox(U u,
java.util.List<Box<U>> boxes)
{
Box<U> box = new Box<>();
box.set(u);
boxes.add(box);
}
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
{
int counter = 0;
for (Box<U> box: boxes)
{
U boxContents = box.get();
System.out.println("[" + boxContents.toString() + "]");
counter++;
}
}
public static void main(String[] args)
{
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
BoxDemo.outputBoxes(listOfIntegerBoxes);
}
}
b) 1
c) [1]
d) [0]
Clarification: None.
Output:
$ javac Output.javac
$ java Output
[0]
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 <String> gs = new genericstack<String>();
gs.push("Hello");
System.out.print(gs.pop() + " ");
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}
}
b) Hello
c) 36
d) Hello 36
Clarification: None.
Output:
$ javac Output.javac
$ java Output
Hello 36
public class BoxDemo
{
public static <U> void addBox(U u,
java.util.List<Box<U>> boxes)
{
Box<U> box = new Box<>();
box.set(u);
boxes.add(box);
}
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
{
int counter = 0;
for (Box<U> box: boxes)
{
U boxContents = box.get();
System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
counter++;
}
}
public static void main(String[] args)
{
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
BoxDemo.outputBoxes(listOfIntegerBoxes);
}
}
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]
Clarification: None.
Output:
$ javac Output.javac
$ java Output
Box #0 contains [10].