{"id":115,"date":"2020-12-11T15:36:26","date_gmt":"2020-12-11T15:36:26","guid":{"rendered":"https:\/\/www.canosielabs.com\/blog\/?p=115"},"modified":"2020-12-13T17:34:33","modified_gmt":"2020-12-13T17:34:33","slug":"java-generics-interview-questions","status":"publish","type":"post","link":"https:\/\/www.canosielabs.com\/blog\/java-generics-interview-questions\/","title":{"rendered":"Java: Generics Interview Questions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Over the years, I&#8217;ve created, been asked, and acquired a number of interview questions.  So I&#8217;ve decided to publish them because I think others will find them useful.  Though the questions are a combination of my own creations and what I&#8217;ve seen, all the answers are of my own(unless noted).  Please let me know if there are typos are mistakes.<\/p>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Write a generic method to return the last non null element in a array.<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"06e31d45\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"06e31d45\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"cb4c49c5\" id=\"cb4c49c5-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"cb4c49c5\">\n<pre><code class=\"language-java\">public static &lt;T&gt; T returnLast(T[] array) {\n    T lastItemFound = null;\n    for (int i = 0; i &lt; array.length; i++) { \n        if (array[i] != null) {\n            lastItemFound = array[i];\n        } \n     } \n    return lastItemFound;\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can you talk about type erasure?  <\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"ba32cd7d\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"ba32cd7d\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"ba3716eb\" id=\"ba3716eb-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"ba3716eb\">\nGenerics were introduced for strong compile time checking and generic programing.  Type erasure removes many of the references to generics in the Java byteocde.\nIdeal answer describe the following concepts:\n<ul>\n<li>Replace generic types with bounds or Object.<\/li>\n<li>Bytecode only contains regular classes and methods.<\/li>\n<li>Concept of bridge method generation.  This is a more advanced answer.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<p class=\"question-section\">Can you change this code to use a parametrized version of List?<\/p>\n<pre><code class=\"language-java\">List aList = new ArrayList();\naList.add(\"string1\");\n<\/code><\/pre>\n\n\n\n<p class=\"write-answer-button\" data-id=\"96ddf1a4\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"96ddf1a4\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"e4dcaa16\" id=\"e4dcaa16-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"e4dcaa16\">\n<pre><code class=\"language-java\">List&lt;String&gt; aList = new ArrayList&lt;&gt;();\naList.add(\"string1\");\n<\/code><\/pre>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">How can you restrict a type argument to be a subclass of two or more classes?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"7d88373b\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"7d88373b\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"1d4b2021\" id=\"1d4b2021-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"1d4b2021\">\n<p>Generics allows for types to be bounded by multiple types.<\/p> \n<pre><code class=\"language-java\">public class Box&lt;T extends ClassA &amp; ClassB&gt; {<\/code><\/pre>\n<p>This restricts the types provided by the user to be a subclass of ClassA and ClassB.  Remember, taken straight from the official documentation: If one of the bounds is a class, it must be specified first.<\/p> \n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What is the difference between a raw type and a non-generic?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"cfed0964\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"cfed0964\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"2486c2a2\" id=\"2486c2a2-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"2486c2a2\">\n<p>A raw type is the name of a generic class or interface without any type arguments were a non-generic type is a regular class.  More specifically, generics add compile time type safety with the use of type parameter provided by the user.  If these are omitted, you loose the intended type safety. Non-generic classes do make this contract.<\/p> \n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Explain why &lt;Object&gt; is different than &lt;?&gt;.<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"a092f1a6\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"a092f1a6\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"5c6315f6\" id=\"5c6315f6-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"5c6315f6\">\n<p>The unbounded wildcard &lt;?&gt; represents any Object.  &lt;Object&gt; represents specifically the Object type.<\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Why is the following method not possible?  In particular, why can we not create a generic array in this way?<\/p>\n\n\n\n<pre><code class=\"language-java\">public &lt;T&gt; T[] createArray(size) {\n    return new T[size]; \n}<\/code><\/pre><br>\n\n\n\n<p class=\"write-answer-button\" data-id=\"3c274470\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"3c274470\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"15890e16\" id=\"15890e16-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"15890e16\">\n<p>Though this may seem ok at face value, lets consider what happens after type erasure.  At runtime, the createArray() is return Object[].  So while this code may look valid at compile time:<\/p> \n<pre><code class=\"language-java\">Integer[] array = createArray(10);\n}<\/code><\/pre>\n<p>At runtime, we&#8217;ve are assigning an Object[] to a variable declared to be a Integer[] which violates type safety.<\/p> \n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What is a type parameter in generics?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"9a44bcb1\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"9a44bcb1\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"5f382718\" id=\"5f382718-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"5f382718\">\n<p>Used in Java generics, a type parameter is used when a type can be used as a parameter in a class, method, or interface.<\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Why use Generic Types, name some advantages?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"169318d4\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"169318d4\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"7429e864\" id=\"7429e864-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"7429e864\">\n<ol>\n<li>Provides better type safety, especially when working with Lists (or Collections).  We can declare a List to contain objects of just one type.<\/li>\n<li>Avoiding unnecessary casts that clutter up code.<\/li>\n<li>Allows for the creation of methods and classes that can be used on with different types.  No need to sublcass or create duplicates.<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What happens if you instantiate a generic class without provide a type argument?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"cd2f4b9e\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"cd2f4b9e\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"1eac0c34\" id=\"7429e864-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"1eac0c34\">\nIn order to maintain backwards combability, you can are allowed to create instances of generic classes without providing a type argument.  It is not considered practice to omit the type and the compiler will generate a warning.\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What is Type Inference?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"cdf6ae08\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"cdf6ae08\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"861aae3b\" id=\"861aae3b-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"861aae3b\">\nType inference describes Java&#8217;s ability to look at context and for a method invocation or class instantiation and determine the type arguments to make the generic type invocation.\n<ol>\n<li>With respect to method invocations, type inference allows you to invoke generic methods as you would a ordinary method (omitting the type arguments)<\/li>\n<li>With class instantiation, you can invoke the constructor of a generic class using an empty set of type parameters ().<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p class=\"question-section\">Can you write classes\/interfaces ClassA, ClassB, and ClassC such that ClassC  can be provided as a type argument when we perform a generic type on a generic class with the following definition:<\/p>\n<pre><code class=\"language-java\">public class Box&lt;T extends ClassA &amp; ClassB &amp; Comparator&gt; {<\/code><\/pre>\n\n\n\n<p class=\"write-answer-button\" data-id=\"5c418dbf\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"5c418dbf\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"8102d4d0\" id=\"8102d4d0-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"8102d4d0\">\n<pre><code class=\"language-java\">public class ClassA {}\npublic interface ClassB {}\npublic class ClassC extends B implements Comparable{}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What is the difference between List&lt;? extends AClass&gt; and  List&lt;T extends AClass&gt;?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"e972020f\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"e972020f\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"ff551e7b\" id=\"ff551e7b-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"ff551e7b\">\n<p>List&lt;? extends A&gt; &#8211; Defines a wildcard bounded by the class A.  Type arguments provide can include any class that extends or implements (if A is a interface) A.  This syntax is typically used for example, in a method parameter definition.  <\/p>\n<p>List&lt;T extends A&gt; &#8211; This definition also accepts type arguments that are extends\/implements A.  However, this syntax is applicable for generic class, interface or method definitions.<\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What is difference between List&lt;? extends A&gt; and List &lt;? super A&gt;?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"a5063ea5\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"a5063ea5\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"ef973e43\" id=\"ef973e43-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"ef973e43\">\n<p>Both are an example of bounded wildcards.  List&lt;? extends A&gt; will accept any parameterized typed List with a type argument that is a subclass of A or if A is an interface, implements A.  This is an example of a upper bounded wildcard.  List &lt;? super A&gt;? accepts any List whos type that is a super class of A.  This is an example of a lower bounded wildcard.<\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can you pass List&lt;String&gt; to a method which accepts List&lt;Object&gt;?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"0840ec1e\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"0840ec1e\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"cf383367\" id=\"cf383367-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"cf383367\">\n<p>No you cannot.  Even though String is a subclass of Object, this relationship does not exist between List&lt;String&gt; and List&lt;Object&gt;?<\/p>\n<\/div>\n\n\n\n<p class=\"question-section\">Let&#8217;s take at the following class definitions<\/p>\n<pre><code class=\"language-java\">public class ClassA&lt;T&gt; {}\npublic class ClassB extends ClassA&lt;String&gt; {}\npublic class ClassC&lt;T&gt; extends ClassD&lt;T&gt; {\n<\/code><\/pre>\n<p>Which of the assignments fail and why?<\/p>\n<pre><code class=\"language-java\">ClassA&lt;String&gt; a = new ClassA&lt;&gt;();\nClassB&lt;Integer&gt; b = new ClassB&lt;&gt;();\nClassA&lt;Integer&gt; a2 = new ClassA&lt;&gt;();\nClassC&lt;String&gt; c1 = new ClassC&lt;&gt;();\nClassC&lt;Integer&gt; c2 = new ClassC&lt;&gt;();\n\na = b;  \/\/ 1\na = a2;  \/\/ 2\na2 = b;  \/\/ 3\na= c1;  \/\/ 4\nc1 = a;  \/\/ 5\nc1= c2;   \/\/ 6\na2 = c2;  \/\/ 7\n<\/code><\/pre>\n\n\n\n<p class=\"write-answer-button\" data-id=\"ef28978e\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"ef28978e\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"bb3ab18d\" id=\"bb3ab18d-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"bb3ab18d\">\n<ol>\n<li><b>Valid<\/b>.  b (ClassB) is a subclass of ClassA, thus the b can be assigned to to &#8220;a&#8221; since A is of type Class A.<\/li>\n<li><b>Invalid<\/b>.  &#8220;a&#8221; and &#8220;a2&#8221; are of different types.   &#8220;a&#8221; is a ClassA&lt;String&gt; and a2 is a ClassA&lt;Integer&gt;<\/li>\n<li><b>Invalid<\/b>.  Because &#8220;b&#8221; is actually a subclass of ClassA&lt;String&gt;.  <\/li>\n<li><b>Valid<\/b>.  &#8220;a&#8221; is a super class of  &#8220;c1&#8221;, thus &#8220;a&#8221; can be assigned c1.<\/li>\n<li><b>Invalid<\/b>.  Since &#8220;a&#8221; is a super class of &#8220;c1&#8221; so while &#8220;c1&#8221; is a ClassA, the opposite is not true.<\/li>\n<li><b>Invalid<\/b>.  &#8220;c1&#8221; is the parameterized type ClassC&lt;String&gt; and &#8220;c2&#8221; is the parametrized type ClassC&lt;Integer&gt;.  There is no relationship between these two parametrized types even if the generic class is the same.<\/li>\n<li><b>Valid<\/b>.  &#8220;a2&#8221; is actually super class (ClassA) of a ClassC.  When &#8220;c2&#8221; is created, it is of a parameter type ClassC&lt;Integer&gt; which extends ClassA&lt;Integer&gt;.  ClassA&lt;Integer&gt; is the type of &#8220;a2&#8221;. <\/li>\n<\/ol>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What are the differences between List&lt;?&gt; and List&lt;Object&gt;?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"ed4183c1\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"ed4183c1\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"f40eac34\" id=\"f40eac34-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"f40eac34\">\n<p>It&#8217;s important to know that the wildcard character (?) represent unknown types and Object represents specifically the object type. \n<ul><li> A variable declared as List can be assigned List,List or even List<\/li>\n<li>List can only be assigned List<\/li>\n<li>It cannot be assigned List&lt;String&gt; even though Strings are a subclass of Object and a String can be added to a List declared as List&lt;Object&gt;. <\/li> \n<\/ul>\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section\">Take a look at the Box class. Right now it can only hold Strings.  Can you modify it so that it can take other types?  <\/p>\n<pre><code class=\"language-java\">public class Box {\n    private String object;\n\n    public void set(String object) { this.object = object; }\n    public String get() { return object; }\n}<\/code><\/pre>\n\n\n\n<p class=\"write-answer-button\" data-id=\"beaa1092\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"beaa1092\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"9f357c7f\" id=\"9f357c7f-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"9f357c7f\">\n<p>We don&#8217;t want to duplicate the Box class for every single type of object we want to store.  Instead, we&#8217;ll convert Box into a generic class using a type parameter T.<\/p>\n<pre><code class=\"language-java\">public class Box&lt;T&gt; {\n    private T object;\n\n    public void set(T object) { this.object = object; }\n    public T get() { return object; }\n}<\/pre><\/code>\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can you declare static fields whose types are generic type parameters?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"b3c0de99\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"b3c0de99\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"8c21fd9c\" id=\"8c21fd9c-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"8c21fd9c\">\n<p>No.  Think of how we pass type arguments during the generic type invocation process.  Since static fields are shared by all classes of an instance, it does not make sense to have static fields who are type parameters since different instances of the Class can be parameterized by different types.\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section\">Why won't the following Util class compile? <\/p>\n<pre><code class=\"language-java\">public class AnotherUtil&lt;T&gt; {\n    private static T instance = null;\n    public static T getInstance() {\n        if (instance == null)\n\t    instance = new Singleton&lt;T&gt;();\n            return instance;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"write-answer-button\" data-id=\"d008a57d\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"d008a57d\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"94ca3b49\" id=\"94ca3b49-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"94ca3b49\">\n<p>No, it will not compile.  The type parameter T is non-static but is referenced in a static context.  More generally, you cannot use type parameters in statically defined fields.\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section\">Given the following classes:<\/p>\n<pre><code class=\"language-java\">class Shape { \/* ... *\/ }\nclass Circle extends Shape { \/* ... *\/ }\nclass Rectangle extends Shape { \/* ... *\/ }\n\nclass Node&lt;T&gt; { \/* ... *\/ }<\/pre><\/code>\n<p>Will the following compile?<\/p>\n<pre><code class=\"language-java\">Node&lt;Circle&gt; nc = new Node&lt;&gt;();\nNode&lt;Shape&gt;  ns = nc;<\/pre><\/code>\n<p>This question is from the official generics documentation.&lt;\/p\n\n\n\n<p class=\"write-answer-button\" data-id=\"7573ffe3\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"7573ffe3\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"6b8f3983\" id=\"6b8f3983-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"6b8f3983\">\n<p>No, this is not a valid assignment.  Node&lt;Shape&gt; is not a super class of Node&lt;Circle&gt; even if Shape is a super class of Circle.\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can Java generics be applied to primitive types?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"4111a9a5\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"4111a9a5\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"bd58815f\" id=\"bd58815f-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"bd58815f\">\n<p>No Java generics cannot be applied to primitive types. <\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can you create instances of generic type parameters?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"8df596f5\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"8df596f5\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"7b817a93\" id=\"7b817a93-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"7b817a93\">\n<p>No, you cannot create an instance like the following: <\/p>\n<pre><code class=\"language-java\">public class Test &lt;T&gt; {\t\n    public T getSomething() {\n        T t = new T(); \/\/ error, will not compile\n        return t;\n    }\n}<\/code><\/pre>\n<p>However, there are workarounds to use parameterized factory types to create instances:<\/p>\n<pre><code class=\"language-java\">public class Test&lt;T&gt; {\n    private Supplier&lt;T&gt; supplier;\n    \n    Test(Supplier&lt;T&gt; supplier) {\n        this.supplier = supplier;\n    }\n\n    public T getSomething() {\n        return supplier.get();\n    }\n}\n\/ ** **\/\nSupplier&lt;String&gt; stringSupplier = () -&gt; \"abc\";\nSupplier&lt;Integer&gt; integerSupplier = () -&gt; Integer.valueOf(1);\n\nTest&lt;String&gt; s = new Test&lt;&gt;(stringSupplier);\nString str = s.getSomething();\n\t\nTest&lt;Integer&gt; n = new Test&lt;&gt;(integerSupplier);\nInteger num = n.getSomething();<\/code><\/pre>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">When do you implement Unbounded wildcards?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"c2059afe\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"c2059afe\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"d3e87af4\" id=\"d3e87af4-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"d3e87af4\">\n<p>Unbounded wildcards can be useful when are writing  a method that does not need to know (or deal with) actual type.  A simplistic example is a method to return the first non-null value of a List.  We don't care about actual type of the contents of the List.\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">What are the types of bounded wildcards?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"79b73aa0\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"79b73aa0\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"d3e87af4\" id=\"d3e87af4-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"d3e87af4\">\n<ul>\n<li>&lt;? extends T&gt;<\/li>\n<li>&lt;? super T&gt;<\/li>\n<\/ul>\n <\/p>\n<\/div>\n\n\n\n<p class=\"question-section wp-block-paragraph\">Can you summarize the main objections for the introduction of generics?<\/p>\n\n\n\n<p class=\"write-answer-button\" data-id=\"2db243a4\">Write Answer<\/p>\n<textarea class=\"answer-textarea interview-write-answer-hidden\" id=\"2db243a4\"><\/textarea>\n\n\n\n<p class=\"show-answer-button\" data-id=\"05535e0b\" id=\"05535e0b-show-button\">Show Answer<\/p>\n<div class=\"interview-answer-hidden\" id=\"05535e0b\">\n<ul>\n<li>Stronger compile time type checking.\/li&gt;\n<li>Remove unnecessary type casting.<\/li>\n<li>Enabling programmers to implement generic algorithms.<\/li>\n<\/ul>\n <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Over the years, I&#8217;ve created, been asked, and acquired a number of interview questions. So I&#8217;ve decided to publish them because I think others will&hellip;<\/p>\n","protected":false},"author":3,"featured_media":248,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[1],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":10,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/media\/248"}],"wp:attachment":[{"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.canosielabs.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}