Java Generics Interview Questions And Answers

Related Topics:

Java Programming : Java Generics

Here are Java Programming Generics Interview Questions and Answers for freshers asked in various companies. Here you can also download the Java Generics Quiz Questions PDF.
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 run time.

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

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

Answer: C. T

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

Answer: B. N

Explanation:

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. class name { /* … */ }

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 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
[D] All of the mentioned

Answer: C. When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve

Explanation:

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. Anonymous inner class

Explanation:

none
7. What is the output of this program?

1.	    import java.util.*;
2.	    public class genericstack  {
3.	        Stack  stk = new Stack ();
4.		public void push(E obj) {
5.	            stk.push(obj);
6.		}
7.		public E pop() {
8.	            E obj = stk.pop();
9.		    return obj;
10.		}
11.	    }
12.	    class Output {
13.	        public static void main(String args[]) {
14.	            genericstack  gs = new genericstack();
15.	            gs.push("Hello");
16.	            System.out.println(gs.pop());
17.	        }
18.	    }
[A] H
[B] Hello
[C] Runtime Error
[D] Compilation Error

Answer: B. Hello

Explanation:

none
8. Which of these types cannot be used to initiate a generic type?
[A] Integer class
[B] Float class
[C] Primitive Types
[D] Collections

Answer: C. Primitive Types

Explanation:

none
9. Which of these instance cannot be created?
[A] Integer instance.
[B] Generic class instance.
[C] Generic type instance.
[D] Collection instances.

Answer: C. Generic type instance.

Explanation:

It is not possible to create generic type instances. Example – “E obj = new E()” will give a compilation error.
10. Which of these data type cannot be type parameterized?
[A] Array
[B] List
[C] Map
[D] Set

Answer: A. Array

Explanation:

none

Comments: (Your feedback is valuable to us)

alpesh sakhanpara 1 year ago Reply

Efgaf


;