Question 1 : What is difference between List<?> and List<Object> in Java?
Answer :
List<?> is List of unknown type while List<Object> is essentially List of any Type.
You can assign List<String>, List<Integer> to List<?> but you can not assign List<String> to List<Object>.
This is because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings.
Question 2 : Can you pass List<String> to a method which accepts List<Object> ?
Answer :
In first glance it looks like : String is Object so List<String> can be used where List<Object> is required but this is not true.
It will result in compilation error.
This is because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings !!.
Question 3 : What is Bounded and Unbounded wildcards in Generics ?
Bounded wildcards are :
<? extends T> which impose an upper bound by ensuring that type must be sub class of T.
<? super T> where its imposing lower bound by ensuring Type must be super class of T.
<?> represent and unbounded type because <?> can be replace with any Type.
Questions 4 : What is difference between List<? extends T> and List <? super T> ?
This question is related to bounded and unbounded wildcards in Generics.
Bounded wildcard : List<? extends T> will accept any List with Type extending T.
And Unbounded wildcard : List<? super T> will accept any List with type super class of T.
Example List<? extends Number> can accept List<Integer> or List<Float>.
Answer :
List<?> is List of unknown type while List<Object> is essentially List of any Type.
You can assign List<String>, List<Integer> to List<?> but you can not assign List<String> to List<Object>.
This is because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings.
Question 2 : Can you pass List<String> to a method which accepts List<Object> ?
Answer :
In first glance it looks like : String is Object so List<String> can be used where List<Object> is required but this is not true.
It will result in compilation error.
This is because List<Object> can store any any thing including String, Integer etc but List<String> can only store Strings !!.
Question 3 : What is Bounded and Unbounded wildcards in Generics ?
Bounded wildcards are :
<? extends T> which impose an upper bound by ensuring that type must be sub class of T.
<? super T> where its imposing lower bound by ensuring Type must be super class of T.
<?> represent and unbounded type because <?> can be replace with any Type.
Questions 4 : What is difference between List<? extends T> and List <? super T> ?
This question is related to bounded and unbounded wildcards in Generics.
Bounded wildcard : List<? extends T> will accept any List with Type extending T.
And Unbounded wildcard : List<? super T> will accept any List with type super class of T.
Example List<? extends Number> can accept List<Integer> or List<Float>.
Question 6 : What are advantages of using Generics ?
Answer : Generics provides compile time type-safety and ensures that you only insert correct Type in collection and avoids ClassCastException during runtime
Read full article from Java Collections Tricky Interview Questions And Answers
No comments:
Post a Comment