Basic Java Interview Questions and Answers

 Part 1

Basic Java Interview Questions and Answers covered in this post:


    Basic Java Interview Questions and Answers

        What is object-oriented language? Is java 100% object-oriented language? Why?

    -          Object-oriented programming is about creating an object which can have its data and methods.

    -          Java supports primitive data types like int, float, double etc. which is not an object, so Java is not a purely object-oriented language.

     

        What is platform-independent? Why java is platform-independent?

    -          Platform independent means “write once run anywhere”. In short, once you write a program on any platform can run it on any platform.  e.g. If you wrote a program on Unix can run it on windows or any other platform or operating system.

    Once the Java program compiles that program converts into byte code. Byte code is not platform dependent so Java is platform-independent.


     

    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 a Class?

    -          Class is a blueprint or template, from object can be created.

    -          E.g. I have a class with the name Person. I have created '10' objects of the Person class. Each object has its copy of each member variable.

     

    package simplifiedjava.crackedInterview;

     

    public class Person {

     

          private int age;

          private int height;

          private int weight;

          private String name;

          private String education;

         

          // Getter and setter methods.

    }

     


        What is an Object?

    -          Generally, an Object is a real-time entity. Objects are the behaviour of the class.

    -          Technically an Object is the combination of data and functions.

     

        What is JVM? What is the difference between JDK, JVM and JRE?

    -          JVM stands for Java Virtual Machine.

                   -       JVM provides a runtime environment in which byte code can be executed.


    JDK

    JVM

    JRE

    JDK Stands for Java Development Kit.

    JVM Stands for Java Virtual Machine.

    JRE Stands for Java Runtime Environment.

    JDK Physically exist in System

    JVM exist virtually but not physically.

    JRE exists physically.

    JDK = JRE + JVM

    JVM is a run time environment in which bytecode can execute.

    JRE is an Implementation of JVM.



        What is the class loader and what are the types of class loaders?

    -          ClassLoader loads the classes dynamically into memory whenever required by Java Runtime Environment.

    -          ClassLoader is an abstract class in Java Runtime Environment.

    -          There are three types of class loaders available in java.

    a.       BootStrap Class Loader (Primordial ClassLoader):

    This is a parent loader of all existing loaders. Bootstrap loaders load all JDK files from jre/lib/rt.jar.

     

    b.      Extension Class Loader :

    Extension class loader loads the extensions of java core classes from jre/lib/ext directory. Extension class loader is a child loader of Bootstrap class loader.

     

    c.       System Class Loader :

    System class loader loads the application level classes. System class loader is a child loader of Extension class loader.

     

        What is Heap memory and Stack Memory? What is the difference between Heap Memory Stack Memory?

    -          Heap Memory :

    o   Heap memory is used to store the Objects. 

    -          Stack Memory:

    o   Stack memory is used to store the local variables and function list.


     

    Heap Memory

    Stack Memory

    1.

    Heap memory stores the objects.

    Stack Memory stores local variables, reference variables and methods.

    2.

    Objects stores in a hierarchical manner.  

    Variables stores in a sequential manner.

    3.

    Objects can reside in heap memory until it points to reference variable.

    Variables will be removed once the function is executed.

    4.

    Access speed is slow.

    Access speed is high.

    5.

    If Heap memory gets full JVM immediately throw OutOfMemoryError.

    If Stack gets full JVM immediately throws StackOverflowError.

    6.

    The Garbage collections mechanism is used to reclaim the memory.

    There is no role of garbage collector because variables are automatically removed once the function is executed.

    7.

    Heap is a flexible memory.

    Stack is not flexible memory.

    8.

    -Xms and –Xmx JVM options can be used to increase or decrease the size of the Heap.

    -Xss option can be used to increase the size of the stack.



        Explain the Term Association, Composition, and Aggregation? What is HAS-A and IS-A Relationship?

    -          Association: Creating the relationship between two objects is referred to as Association.

    Ø  Association can be One-to-One, One-to-Many, Many-to-One, Many-to-Many.

    Ø  Aggregation and Composition are two forms of Association.

    Ø  One-to-One = One Student can have one Class.

    Ø  One-to-Many = One Student can have multiple Subjects.

    Ø  Many-to-One = Many developers can have one Project Manager.

    Ø  Many-to-Many = Multiple Employees can have Multiple Tasks.

    Ø  Example of Association. We have created an association between Company and Employees. 

     

    package simplifiedjava.crackedInterview;

     

    public class Company {

     

          private String companyName;

         

          public Company(String companyName) {

                this.companyName = companyName;

          }    

          public String getCompanyName() {

                return companyName;

          }    

    }

     

    package simplifiedjava.crackedInterview;

     

    public class Employee {

     

          private String empName;

         

          public Employee(String empName) {

                this.empName = empName;

          }    

          public String getEmpName() {

                return empName;

          }    

    }

     

    package simplifiedjava.crackedInterview;

     

    public class AssociationExample {

     

          public static void main(String[] args) {

                Employee employee1 = new Employee("Rusty");

                Company company = new Company("Company");      

                System.out.println(employee1.getEmpName() + " is working for "company.getCompanyName());

          }

    }

    OUTPUT : Rusty is working for Company.

                

    -          Composition:

    ü  The composition can be called the HAS-A relationship.

    ü  When two objects are tightly coupled with each other or we can say when one object is dependent on another object is called Composition.

    ü  E.g. Engine cannot survive without a car.  If a car exists then an engine exists. If a car is destroyed automatically engine gets destroyed.

    package simplifiedjava.crackedInterview; 

    public class Engine { 

          private String engineName;

          private String power;

          private String modelNo;     

          // getter and Setter methods.

    } 

    package simplifiedjava.crackedInterview; 

    public class Car { 

          private String carName;

          private String carModel;

          private String manufacturer;           

          private Engine engine; // Car HAS-A reference of Engine. 

          public Car(String carName, String carModel, String manufacturer, Engine engine) {

                super();

                this.carName = carName;

                this.carModel = carModel;

                this.manufacturer = manufacturer;

                engine = new Engine();

          }

          // getter and Setter

    }

     

    -          Aggregation:

    o   Aggregation can be called the HAS-A relationship.

    o   When two objects are loosely coupled or we can say when one object is not dependent on another object is called Aggregation.

    o   E.g. We have two objects. The first one is College and the second one is Student. If college gets destroyed still Student objects can survive independently.

    package simplifiedjava.crackedInterview; 

    public class Student { 

          private int id;

          private String name;

          private String standard;

          // getters and Setters method.     

    } 

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.List; 

    public class College {

          List<Student> studentList = new ArrayList<Student>();

          // add and remove Students.  

    }

     

        What is the difference between Composition and Aggregation?

     

    Composition

    Aggregation

    1.

    Tightly couple relationship.

    Loosely coupled relationship.


        Is Java Pass by value or Pass by reference. How will you prove java is call by value?

    -          Java is Call by Value not a Pass by reference.

    -          We will illustrate with the examples below.

    package simplifiedjava.crackedInterview; 

    public class Addition { 

          int num1 = 100;

          int num2 = 0;     

          public static void main(String[] args) {

                Addition add = new Addition();

                System.out.println("Before Adding Numbers : "add.num1);

                add.addNumber(add.num1);

                System.out.println("After Adding Numbers : "add.num1);   

          }     

          public void addNumber(int num2) {

                num2 = num2 + 100;

          }          

    } 

    OUTPUT:

    Before Adding Numbers : 100

    After Adding Numbers : 100 

    Explaination : In the above program, we have passed integer numbers and trying to add 100. But the change remained in addNumber() function not in main(). 

    package simplifiedjava.crackedInterview; 

    public class Addition { 

          int num1 = 100; 

          public static void main(String[] args) {

                Addition add = new Addition();           

                System.out.println("Before Addition " + add.num1);

                add.addNumberWithObject(add);

                System.out.println("After Addition " + add.num1);

          }           

          public void addNumberWithObject(Addition addCopy) {

                addCopy.num1 = 200;

          }

    } 

    OUTPUT : 

    Before Addition 100

    After Addition 200 

    Explanation: We have passed the reference variable as a value so the value of num1 got changed.


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

    11. Which is the top most Parent class in Java? What the methods of Object Class? 

    12. What is the role of break and continue keywords in java.

    13. Is NULL keyword in java.

    14. What is Actual Parameter and what is formal paramter? 

    15. What is the difference between Local variable and Instance variable.

    16. What is the difference between Instance variable and Class Variable.

    17. What are the uses of this keywords. Where it is applicable and what are the rules to use these two keywords. 

    18. What are the uses of super keywords. Where it is applicable and what are the rules to use these two keywords. 

    19. What are the access specifiers and its scope?

    20. Can we use static member inside non-static area.


    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