Basic Java Interview Questions and Answers

 Part 4

Basic Java Interview Questions and Answers covered in this post:


    Basic Java Interview Questions and Answers

        What is static binding and dynamic binding?

    -          Static binding occurs compile time.

    -          Dynamic binding occurs runtime

    -          Best example of static binding is method overloading.

    -      Best example of dynamic binding is method overriding.


        What is instanceOf in java? Where it can be used?

    -          instanceOf is an operator in java.

    -          instanceOf can be used to check the type of object.

    -          syntax for instanceOf operator is, (JavaInterview instanceOf String);

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.List; 

    public class InstanceOfDemo { 

          public static void main(String[] args) {           

                List list = new ArrayList();

                list.add(new String("Java"));

                list.add(new Integer(100));

                list.add(new ITEmployee());                              

                for(Object obj : list) {

                      if(obj instanceof String) {

                            System.out.println((String)obj);

                      }else if(obj instanceof Integer) {

                            System.out.println((Integer)obj);

                      }else if(obj instanceof ITEmployee) {

                            System.out.println((ITEmployee)obj);

                      }                                              

                }                      

          }

    } 

    class ITEmployee{     

    }

     

     

    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 the ternary operator in java?

    -          Ternary operator lets you write if statement in one line.

    -          Ternary operator evaluates either true or false.

    -          There are three parts of the ternary operator. The first part is expression end with a question mark and two options separated with a colon.

    -          Variable = condition ? expression1: expression2. 

    package simplifiedjava.crackedInterview;

     

    public class TernaryOperatorDemo {

     

          public static void main(String[] args) {

                int num=20;

                String typeNum = (num > 0) ? "Positive" : "Negative";

                System.out.println(typeNum);       

          }

    }

    Output : Positive

     

        What is the difference between relative path and absolute path?

    -          Absolute Path: Absolute path specifies the location from the root directory.

    -          E.g. E:/drawings/images/sky.image

    -          Relative Path: Relative path specifies the location from the current directory.

    -          E.g. images/sky.image

     

        What is garbage collections? Who is responsible for garbage collection? How java reclaims a memory?

    -          Garbage collection is a process to delete unused objects and reclaim the memory.

    -          JVM is responsible to reclaim the memory.

    -       JVM finds the unused objects and no longer references objects that reside in memory and then JVM invokes finalize() method. This finalize() methods released the resources acquired by that objects.

    -          Once invoked the finalize() method will mark all targeted objects and simply removed them from memory.

    -      There are many GC algorithms that exist to implement GC. One of them is mark and sweep.

     

        How does an object become eligible for garbage collection?

    -     Object can be made eligible for garbage collection when there is no single reference variable pointing to it.

    -       There are three ways objects can make eligible for garbage collection.

    1.       When an object goes out of scope.

    2.       Objects reference variable explicitly assigned a null value.

    3.       Reinitialize the reference variable.

     

        Can we ask JVM to collect the garbage or destroy the objects?

    -          Yes we can just recommend destroying unused objects but cannot force them to do it.

    -          System.gc() method can be used to recommend the garbage collection.

     

        What is the difference between equals() and ==?

    -          Equals() is the method and can be used for content comparison.

    -          == is an operator and can be used for reference comparison. 

     

        What are Boxing and Unboxing?

    -          The conversion of primitive data type into its corresponding wrapper class is called boxing.

    -     The conversion of wrapper class type into its corresponding primitive type is called unboxing.

    -          Please refer to the below example for boxing and unboxing.

    package simplifiedjava.crackedInterview; 

    public class BoxingUnboxingDemo { 

          public static void main(String[] args) {           

                int num = 100;

                Integer wrapperNum1 = new Integer(num);

                Integer wrapperNum2 = 200;

        System.out.println("Auto Boxing "+ wrapperNum1 + " and " + wrapperNum2);           

                Integer wrapperNum3 = new Integer(300);

                num = wrapperNum3;

                System.out.println("Unboxing "+ num);

          }

    }

    Output :

    Auto Boxing 100 and 200

    Unboxing 300

     

        What are Upcasting and Downcasting?

    -          Upcasting means casting the child object into the parent object.

    -          Implicit upcasting is possible.

    -          UpCasting is also called Widening.

    -          Downcasting means casting the parent object into a child object.

    -          Implicit downcasting is not possible.

    -          Downcasting is also known as Narrowing.

    -          Please refer to the below example for upcasting and downcasting.

    package simplifiedjava.crackedInterview; 

    public class Parent {     

          public void check(){

                System.out.println("Parent Class check() method");         

          }    

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public void check(){

                System.out.println("Child Class check() method");          

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class UpCastingDownCastingDemo { 

          public static void main(String[] args) {           

                Parent p = (Parent)new Child(); // Upcasting

                p.check();           

                Parent p1 = new Child();

                Child c = (Child)p1;    //downcasting

                p1.check();

          }

    }

    Output :

    Child Class check() method.

    Child Class check() method.


    • 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:

    41. What is widening.

    42. What is narrowing.

    43. What is equals() and hashcode() contract.

    44. What is Comparable and Comparator. What is the difference between Comparable and Comparator.

    45. Can you write a code to customize Employee Sorting order by Salary or Designation.

    46. What is ENUM. What is the purpose of ENUM.


    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