Basic Java Interview Questions and Answers

 Part 5

Basic Java Interview Questions and Answers covered in this post:


    Basic Java Interview Questions and Answers

        What is Widening?

    -          When we assign a value of a smaller data type to a bigger data type.

    -          It is possible when two data types are compatible.

    -          Widening Scope: byte -> short -> int -> long -> float -> double.

    package simplifiedjava.crackedInterview; 

    public class WideningDemo { 

          public static void main(String[] args) {

                int i=100;       

                long j = i;      

                float k = j;           

                float k1 = i;

                System.out.println("Value of i = "+ i + " \nValue of j = "+ j + "\nValue of k = "+ k + "\nValue of k1 = "+ k1);

          }

    }

     

        What is Narrowing?

    -          When we assign a value of a bigger data type to a smaller data type.

    -          It is possible when two data types are compatible.

    -          Narrowing Scope: byte <- short <- int <- long <- float <- double.

    package simplifiedjava.crackedInterview; 

    public class WideningDemo { 

          public static void main(String[] args) {

                float i=100;           

                long j = (long) i;           

                int k = (int) j;       

                int k1 = (int) i;

                System.out.println("Value of i = "+ i + " \nValue of j = "+ j + "\nValue of k = "+ k + "\nValue of k1 = "+ k1);

          }

    }

    3.   

     

    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 equals() and hashcode() contract?

    -          If the two objects are equal then they should have the same hashcode and if two objects are not equal then they may or may not have the same hashcode.

    -          Objet1.equals(Object2) if true then hashcode must be same of both objects.

    -          Object1.equals(Object2) if false then hashcode may be or may not be same.

    -          As per the oracle documentation, both methods should be overridden to get the complete equality mechanism you must override equals() and hashcode() methods.

     

        What is Comparable and Comparator? What is the difference between Comparable and Comparator?

    -          Comparable and Comparator are interfaces that can be used for sorting purposes.

    -          Comparable interface can be used for natural sorting order.

    -          Comparator interface can be used to customize sorting orders.

    -          Comparable has compareTo() method.

    -          Comparator has compare() method.

     

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

    package simplifiedjava.crackedInterview; 

    import java.util.Comparator; 

    public class Employee { 

          private String empName;

          private String dept;

          private Double salary;

          private String designation;                 

          public Employee(String empName, String dept, Double salary, String designation) {

                super();

                this.empName = empName;

                this.dept = dept;

                this.salary = salary;

                this.designation = designation;

          }

          public Employee(String empName) {

                this.empName = empName;

          }    

          public String getEmpName() {

                return empName;

          }

          public String getDept() {

                return dept;

          }

          public void setDept(String dept) {

                this.dept = dept;

          }

          public Double getSalary() {

                return salary;

          }

          public void setSalary(Double salary) {

                this.salary = salary;

          }

          public String getDesignation() {

                return designation;

          }

          public void setDesignation(String designation) {

                this.designation = designation;

          }

          public void setEmpName(String empName) {

                this.empName = empName;

          }

          @Override

          public String toString() {

                return "Employee [empName=" + empName + ", dept=" + dept + ", salary=" + salary + ", designation=" + designation

                            + "]";

          }    

    } 

    package simplifiedjava.crackedInterview; 

    import java.util.Comparator; 

    public class SortEmployeeBySalary implements Comparator<Employee>{ 

          public int compare(Employee e1, Employee e2) {

                return e1.getSalary().compareTo(e2.getSalary());

          }    

    } 

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.List; 

    public class ComparableAndComparatorDemo { 

          public static void main(String[] args) {           

        List<Employee> empList = new ArrayList<Employee>();

        empList.add(new Employee("Yogesh","IT",100000.00,"System Analyst"));

        empList.add(new Employee("Arpita","Mangement",1200000.00,"Trustee"));

        empList.add(new Employee("Shweta","DevOps",45000.00,"Jenkin Engineer"));

        empList.add(new Employee("Shruti","IT",65000.00,"DB Admin"));

        empList.add(new Employee("Priyanka","IT",35000.00,"Test Engineer"));

     

        System.out.println("=========== Employee Before Sorting ==========");

                System.out.println(empList);           

                System.out.println("======== Employee After Sorting =======");

                empList.sort(new SortEmployeeBySalary());

                System.out.println(empList);

          }

    } 

    Output:

    =================== Employee Before Sorting =====================

    [Employee [empName=Yogesh, dept=IT, salary=100000.0, designation=System Analyst], Employee [empName=Arpita, dept=Mangement, salary=1200000.0, designation=Trustee], Employee [empName=Shweta, dept=DevOps, salary=45000.0, designation=Jenkin Engineer], Employee [empName=Shruti, dept=IT, salary=65000.0, designation=DB Admin], Employee [empName=Priyanka, dept=IT, salary=35000.0, designation=Test Engineer]]

    =================== Employee After Sorting =====================

    [Employee [empName=Priyanka, dept=IT, salary=35000.0, designation=Test Engineer], Employee [empName=Shweta, dept=DevOps, salary=45000.0, designation=Jenkin Engineer], Employee [empName=Shruti, dept=IT, salary=65000.0, designation=DB Admin], Employee [empName=Yogesh, dept=IT, salary=100000.0, designation=System Analyst], Employee [empName=Arpita, dept=Mangement, salary=1200000.0, designation=Trustee]]

     

     

        What is ENUM? What is the purpose of ENUM?

    -          Enum stands for Enumeration.

    -          Enum is a data type that is a set of constants.

    -          Enum can be declared inside or outside the class.

    -          Please refer to the below example for enum implementation.

    package simplifiedjava.crackedInterview; 

    public class EnumDemo { 

          public enum months {JANURAY,FEBRUARY,MAR,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEBER,OCTOBER,NOVEMBER,DECEMER};     

          public static void main(String[] args) {       

                for(MONTHS month : months.values()) {

                      System.out.print(" "+ month);

                }

          }

    }

    Output: JANURAY FEBRUARY MAR APRIL MAY JUNE JULY AUGUST SEPTEBER OCTOBER NOVEMBER DECEMER




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

    47. Can we declare constructor,methods and fields inside ENUM definition. 

    48. When the constructor will be executed once you declare the constructor inside enum.

    49. What is the difference between enum, Enum and Enumeration.

    50. What is Assertion. What is the role of Assertion in java development.

    51. Can we declare class inside interface.

    52. Can we declare interface inside a class.

    53. Can we declare class inside a class.

    54. Can we declare interface inside a interface.


    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