HCL Java interview questions and answers.

Java Interview Questions and Answers covered in this post:

    Java interview questions and answers
     

        Explain the Term Association, Composition, 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 are the uses of this keyword? Where it is applicable and what are the rules to use this keywords?

    -          this keyword is used to refer to the current object.

    -          this keyword can be used to the invoked current class constructor.

    -          this keyword can be used to the invoked current method.

    -          this can be used to return the current class object.

    -          this can be used to pass as an argument in method as well as constructor.


     

    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 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 SCP(String Constant Pool)?

    -          String constant pool is a separate place in heap memory where all the String objects are stored.

    -          If you create an object using a new keyword then the object gets created on heap memory and keep the same string object into string constant pool.

    -          If you create an object without a new keyword then the object doesn’t create on heap memory that object will be created on string constant pool.

    -          If an object exists then the reference variable pointing to an existing object instead of creating a new one.


        What is a re-throwing exception in java?

    -          Exception throws from try block and catches it in the catch block. A caught exception that can be thrown from the catch block is called re-throwing the exception.

    -          Re-thrown exception must handle somewhere in the program otherwise your program will be terminated abruptly.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class NestedTryCatchDemo { 

          public static void main(String[] args) {

                try {

                            int no = 10 / 0;                 

                }catch(Exception e) {

                      throw new NullPointerException();

                }

          }

    }

    Output:

    Exception in thread "main" java.lang.NullPointerException

    at simplifiedjava.crackedInterview.NestedTryCatchDemo.main(NestedTryCatchDemo.java:10)



        What happen if you don't override run() method on Thread class?

    -          If you don’t override the run() method it will execute the run method of the Thread class that has empty implementation. The compiler won’t throw any exception.

             -         It is recommended to override the run() method and provide your implementation.


        What is blocking queue?

    -          A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

    -          A BlockingQueue does not accept null elements.

    -          Implementations throw NullPointerException on attempts to add, put or offer a null.

    -          A BlockingQueue may be capacity bounded.

    -          At any given time it may have a remaining Capacity beyond which no additional elements can be put without blocking. A BlockingQueue without any intrinsic capacity constraints always reports a remaining capacity of Integer.MAX_VALUE.

    -          BlockingQueue implementations are designed to be used primarily for producer-consumer queues, but additionally support the Collection interface.

    -          BlockingQueue implementations are thread-safe.


        Which collection classes implements RandomAccess interface? What are the benefits of RandomAccess interface?

    -          ArrayList and Vector are implemented Random Access Interface.

    -          The presence of a marker interface is that it indicates ( or expects ) specific behaviour from the implementing class.

    -          So, in our case, ArrayList implements the RandomAccess marker interface.

    -        So, the expectation from the ArrayList class, is that it should produce RandomAccess behaviour to the clients of the ArrayList class when the client wants to access some element at some index. 


        Can you list out some functional interface used in java 6?

    -          FunctionalInterface must have only one abstract method.

    -          We have used 4 functional interfaces.

    1.       Runnable: Runnable interface has run() method.

    2.       Callable: Callable interface has call() method.

    3.       ActionListener: ActionListener has actionPerformed() method.

    4.       Comparable: Comparable has compareTo() method.


        What is Constructor Reference? How will you implements Constructor reference?

    -          Constructor References in Java 8 can be created using the Class Name and the keyword new keyword.

    -          Syntaxes for Constructor reference.

    1.       Integer::new;

    2.       String::new;

    3.       ArrayList::new;

    4.       Employee::new;

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview.java8; 

    @FunctionalInterface

    public interface StudentInterface {

          public Student get(String name);

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class Student { 

          private String name; 

          public Student(String name) {

                super();

                this.name = name;

                System.out.println("Student Name is : "+ name);

          }     

          public static void main(String[] args) {

                StudentInterface stuInt = Student :: new;

                stuInt.get("Shruti");

          }    

    }

    Output: Student Name is : Shruti




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

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

    2. Can you explain types of Inheritance java supports?

    3. What is the difference between static block and instance block.

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

    5. What is ENUM. What is the purpose of ENUM.

    6. What is immutable class. 

    7. What is the role of printStackTrace() method. This method belongs to which class.

    8. Can we have multiple catch blocks.

    9. What is Vector and what are the characteristics of Vector.

    10.What are Priority Queue.



    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