Generic Interview Preparation

FREQUENTLY ASKED GENERICS INTERVIEW QUESTION IN JAVA

What is generics ?

As the name explains ,Generic is a way by which we can make a class or method generics.Using generics same class or method can be used by many types.Generic provies string compile time checking and by using generics developer don't require casting

Does Array in java type safe ?

Yes always, In Array of String we can not add Integer.

When generics is introduced in java ?

Java 1.5.

Does non generics collection can hold any object ?

yes for example list can hold any object

List list = new ArrayList()

list.add("test");

list.add(5)

What is the return type when method gets object out from collection ?

java.lang.Object

How to convert object into Other Object for example String when method gets object out from collection ?

By casting For example (provided list only contains String)

String result = (String)list.get(0);

What will happen if method tries to get object out from collection other than cast type ?

ClassCastException will be thrown

How can we make Collection type safe ?

Collection can be made type safe by Generic for example

If List should only contain String then declare it as List<String> list= new ArrayList<String>();

If we try to insert any object in list except String then compile error will be displayed.

Type is angle bracket can be referred as either Parameterized type or type parameter

How getting object from generics collection is different from non generics collection ?

In generics we get rid of casting for example

String result = (String)list.get(0); : Non Generic

String result = list.get(0); : Generic

How to declare type parameter for method argument ?

public void test(List<String> input)

Can we declare type parameter for method return ?

yes for example public List<String> test()

Can we assign/pass generics collection to non generics collection? For example will below code snippet work ?

public void caller(){
List<String> list= new ArrayList<String>();
receiver(list);}
public void receiver(List list){}

Yes generics collection can be assigned to non generics .Its is allowed so that backward compatibility can be supported

Will below generics code snippet work ?

public void caller(){
List<String> list= new ArrayList<String>();
receiver(list);}
public void receiver(List list){
list.add(new Integer(42));}

Yes non generics collection can put any object.

Does java gives any error or warning when generics collection is assigned to non generics collection and non generics collection tries to add any object ?

It does not give any error but warning is given

javac classname.java

Note: classname.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

Why at runtime JVM does not throw any exception when non generics collection add different type of object ?

Truth is Generic type information does not exist at runtime.All generics code is for compiler.Thst's why this process is called type erasure

Does polymorphism work for generics type for example will below line of generics code compile ?

class Parent {}
class Child extends Parent { }
List<Parent> parent = new ArrayList<Child>

No , generics does not support polymorphism so above code will not compile.The generics type of the variable declaration must match the generics type passed to the actual object type, so correct way is

either

List<Parent> parent = new ArrayList<Parent>

or

List<Child> parent = new ArrayList<Child>

Will below generics code snippet work ?

class Parent {}
class Child extends Parent { }
public void caller(){
List<Child> child = new ArrayList<Child>
receiver(list);}
public void receiver(List<Parent> list){}

It will not work.Generic does not support polymorphism.

Can we add Child object into a collection declared with supertype. Will below generics code snippet work ?

class Parent {}
class Child extends Parent { }
public void caller(){
List<Parent> parent = new ArrayList<Parent>
parent.add(new Child();}

It will work because we can add an instance of a subtype into a collection declared with a supertype.

How can i pass subtype of generics to supertype of generics ?

By using <? extends Parent>, it means, it can be assigned a typed of <Parent> or anything that extends Parent. For example

List<? extends Parent> list = new ArrayList<Child>();

Note : Parent can be class or interface

Will below generics code snippet work ?

List<? extends Parent> list = new ArrayList<Child>();
list.add(new Child());

No, JVM allows to assign subtype of generics collection to supertype of generics collection but there is one constraint nothing can be added in that collection, so code will not work because we are adding parent

What is <? super Child> ?

it means, it can be assigned a typed of <Child> or anything that is supertype of Child. For example

List<? super Child> list = new ArrayList<Parent>();

What is <?> ?

it means, it can be assigned any type.There is one constraint nothing can be added in that collection

What is the difference between <?> and <Object> ?

<?> can take any type for example Parent, Child, String, Integer.

<Object> can take only Object for example.

List<Object> list = new ArrayList<Object>

To make a class generics, where should we define generics type ?

After class name for example Class Test<T>.

To make a method generics, where should we define generics type ?

Before return type for example public <T> void test<T t >.

Does array support generics ?

No because generics does not exist at runtime, if we write

T[] t = new T[]; then during runtime generics will be erased adn array synatx will not exist.

What is bounded wildcard generics ?

It puts bound on Type for example as we discussed above <? extends Parent> and <? super Child> are bounded wilcard generics because earlier one imposes upper limit while later one imposes limit on lower side.

What is unbounded wildcard generics ?

It puts no bound on Type for example as we discussed above <?> are unbounded wilcard generics because it imposes no limit, any object can be passed to it.

What does it mean by type inference ?

It's a way by this compiler finds the type of generics by looking argument type.For example if a method takes paramert like receiver(T t) and caller calls this method by receiver("String"), it means type of generics type is String.




Visit Others Links

Interview Questions Top Elasticsearch Interview Question
Top Kubernetes Interview Question
Top HTTP Interview Question
Top PouchDB Interview Question
Top Pivotal Cloud Foundry (pcf) Interview Question
Top Apache Camel Interview Question
Top Docker Interview Question
Top Gradle Interview Question
Top Generics Interview Questions In Java
Java 9 Interview Questions
Top Maven Interview Question
Top RabbitMQ Interview Question
Top Spring Batch Interview Question
Top Spring Boot Interview Question
Top Tika Interview Question
Top Kotlin Interview Question
Top OpenShift Interview Question
Top Polymer Interview Question
Top WebGL Interview Question
Top Continuous Integration Interview Question
Top Spring Cloud Interview Question
Top JOGL Interview Question
Top JFreeChart Interview Question
Java MultiThreading Interview Question
Top Spring ROO Interview Question
Top Seaborn Interview Question
Top TestRail Interview Question
Top LolCode Interview Question
Top Spring Transaction Management Interview Question
Top DynamoDB Interview Question
Top PySpark Interview Question
Top SaltStack Interview Question
Top Spring AOP Interview Question
© 2019 PreparationForInterview.com