Basic Java Interview Questions and Answers

Part 3

Basic Java Interview Questions and Answers covered in this post:

    Basic Java Interview Questions and Answers


        What is static import?

    -    Static import means we are importing static members from another class.

    -   Once we import static members then we can use those members without the class name.

               Generally, we use static members with the class name but once you import then there is no need to use the class name. You can directly access those members.


        Where the static variables are stored in java?

    -   The static variables were stored in the Perm-Gen space (also called the method area).

    -      The static variables are stored in the Heap itself.

    -    Java 8 onwards the Perm-Gen Space has been removed and a new space named Meta-Space is introduced which is not part of Heap memory anymore unlike the previous Perm-Gen Space.


     

    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 diamond problem in Java?  Why java doesn't support multiple inheritance?

    -       Diamond problem comes into the picture in multiple inheritance.

    -    When one class is inheriting the same property from multiple classes. This is called the diamond problem.

    -   E.g. There are 3 classes. Class A, Class B and Class C. Class A and Class B have add() method and when class C extends class A and class B. Class C is trying to override add() method then which method compiler will refer for overriding. There is ambiguity. This is a diamond problem. That’s why java doesn’t support multiple inheritance.


        What is a marker interface? Can you tell me the example of a marker interface?

    -          An empty interface is called a marker interface.

    -          Marker interface doesn’t have any method or variable.

    -          List of marker interfaces.

    1.       Serializable interface.

    2.       Cloneable interface.

    3.       Remote Interface.

     

        What are the wrapper classes in java? Why do we require wrapper classes?

    -   Wrapper class is a class whose objects are wraps or contains primitive data types.

    -     Wrapper class hold primitive values and is represented as an object.

    -     All primitives has its own wrapper class.

    -     E.g. int has Integer, float has Float, double has Double etc.

    -   In the collection framework if we wanted to represent primitive as an object for manipulation then wrapper class can be used.


        What is variable shadowing?

    -   Shadowing means a variable declared within a certain scope (Declared inside method or block) has the same name as a variable declared in an outer scope.

    -     In short, we have the same variable name in block or method and class level variable.

    -      For a demonstration please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class VariavleShadowingDemo { 

          int num = 10;     

          public void printNum() {

                int num = 20;

                System.out.println("Num Value "+ num);

          }         

          public static void main(String[] args) {

                new VariavleShadowingDemo().printNum();

          }

    }

    Output : 20. Because local variable has highest priority inside method.

     

        What is static method shadowing?

    -   The static method in the superclass will be hidden by the one that is declared in the subclass. This mechanism is known as method shadowing.

    -     Method shadowing is applicable only for a static method.

    package simplifiedjava.crackedInterview; 

    public class Parent {     

          public static void printGreetings() {

                System.out.println("Parent Class Static Method");

          }    

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public static void printGreetings() {

                System.out.println("Child Class Static Method");

          }     

          public static void main(String[] args) {

                Parent.printGreetings();

                Child.printGreetings();

          }

    }

    Output:

    Parent Class Static Method

    Child Class Static Method

     

        What is object cloning?

    -          Object cloning means creating a duplicate copy of an object.

    -          clone() can be used to creating the object clone.

     

        What is a co-variant return type?

    -   Covariant return means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method's return type.

    -    In the below example, There are two classes Human and Men. Men class extends the Human class.

    -    Both class has dressingStyle() method. This method is overriding in the Men class. Both methods have the same return type but if you observe then the parent class method returns the actual Human class object and the child class method returns the Child class(Men Class) object when the return type is Human. This is called covariant return type.

    package simplifiedjava.crackedInterview; 

    public class Human { 

          public Human dressingStyle() {

                return new Human();

          }    

    }

    package simplifiedjava.crackedInterview; 

    public class Men extends Human { 

          public Men dressingStyle() {

                return new Men();

          }    

    }

     

        What is the purpose of the final keyword where it can be applicable?

    -          Final keyword can be applied to the following.

    1.       Variable: Final variable value cannot change once assigned.

    2.       Method: Final method cannot override.

            3.       Class: Final class cannot be sub-classed. 


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

    31. What is static binding and dynamic binding.

    32. What is instanceOf in java. Where it can be used.

    33. What is ternary operator in java.

    34. What is the difference between relative path and absolute path.

    35. How java reclaims a memory. what is garbage collections. Who is responsible for garbage collection.

    36. How does object become an eligible for garbage collection.

    37. Can we ask JVM to collect the garbage or destroy the objects.

    38. What is the difference between equals() and ==

    39. What is autoboxing and unboxing.

    40.What is upcasting and downcasting.


    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