Interview questions on Generics in java for experienced

 Part 1

Interview questions on Generics in java for experienced covered in this post:

    Interview questions on Generics in java for experienced


        What is Generics? Can you list out some advantages of Generics?

    -          Generic provides type safety and resolve type casting problems.

    -          Advantages of Generics.

    1.       No explicit type casting is required.

    2.       Reduce chances to throw ClassCastException.

    3.       With the help of generics, we can identify which type of list is there.

    4.       If you are trying to add a heterogeneous object then you will get a compile-time error.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.List; 

    public class GenericsDemoForStringList { 

          public static void main(String[] args) {

                List<String> list = new ArrayList<String>();

                list.add("Shweta");

                list.add("Shruti");

                list.add("Arpita");

                System.out.println(list);

          }

    }

    Output: [Shweta, Shruti, Arpita] 

    If you are trying to add other thanString you will get compile time error. 

     

        What is Generic Type Parameter?

    -          Type parameter is an identifier that specifies a generic type name.

    -          Type parameter is also known as the Type variable.

    -          Type parameter can be used to declare the return type.

    -          Please refer to the syntax.

    class ArrayList<T>{

          add(T t);

          T get(int index);

    } 

    class ArrayList<String>{

          add(String s);

          String get(int index);

    }

     

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview;

    public class GenericTemplate<T> { 

          T obj;     

          public GenericTemplate(T obj) {

                this.obj = obj;

          }    

          public T getobj() {

                return obj;

          }

          public void show() {

                System.out.println("Type of Object "+ obj.getClass().getName());

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class GenericDemo { 

          public static void main(String[] args) {

            GenericTemplate<String> str = new GenericTemplate<String>("Yogesh");

                str.show();

                System.out.println(str.getobj());

               

            GenericTemplate<Integer> numbers = new GenericTemplate<Integer>(100);

                numbers.show();

                System.out.println(numbers.getobj());

               

            GenericTemplate<Double> dblNum = new GenericTemplate<Double>(25.50);

                dblNum.show();

                System.out.println(dblNum.getobj());

          }

    } 

    Output:

    Type of Object java.lang.String

    Yogesh

    Type of Object java.lang.Integer

    100

    Type of Object java.lang.Double

    25.5 

     

     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

        What is bounded type parameter?

    -          We can bound type parameters to the particular range by using extends keyword such types are called bounded types.

    -          Please refer to the below example.

    class Test <T extends Number>{

         

    }

     

        What is type interface?

    -          Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable. 

        What is type Erasure?

    -          Generic provides compile-time java files all generics syntax will be removed.

    -          At runtime generics syntax that won’t be available is called Type Erasure.




    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here

    Upcoming questions in next post:


    Previous Post                                                                    Next Post



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     


    Post a Comment

    0 Comments