Core java interview questions and answers for 3-5 years experience

 Part 4

Core java interview questions and answers for 3-5 years experience covered in this post:



    Core java interview questions and answers for 3-5 years experience

        Can you identify which try, catch and finally block combination is valid?

    -          Following are the combinations.

    1.

    try {

    }catch() {       

    }

    try {      

    }catch() { 

    }

     

    2.

    try {           

    }catch() {           

    }

    try { 

    }finally {     

    }

     

    3.

    try {           

    } 

     

    4.

    try {           

    }finally {           

    }catch() {           

    }

     

    1 and 2 are valid.

    3 and 4 are invalid.


        How can you prevent particular variable to be serialized?

    -  Transient keyword can be used to prevent a particular variable to be serialized.

    -  You can declare the variable as static. Static variables are not considered for serialization.

    -    Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.Serializable; 

    public class Student implements Serializable { 

          private static final long serialVersionUID = 1L;

          private int id;

          private transient String name;

          private String dept;

         

          public Student(int id, String name, String dept) {

                super();

                this.id = id;

                this.name = name;

                this.dept = dept;

          }

          public int getId() {

                return id;

          }

          public void setId(int id) {

                this.id = id;

          }

          public String getName() {

                return name;

          }

          public void setName(String name) {

                this.name = name;

          }

          public String getdept() {

                return dept;

          }

          public void setdept(String dept) {

                this.dept = dept;

          }

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class SerializationDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {           

                Student s1 = new Student(100,"Yogesh","IT");

               

                File file1 = new File("stud1.ser");

                FileOutputStream fos = new FileOutputStream(file1);

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(s1);

               

                File file2 = new File("stud1.ser");

                FileInputStream fis = new FileInputStream(file2);

                ObjectInputStream ois = new ObjectInputStream(fis);

                Student s2 = (Student) ois.readObject();

               

                System.out.println("Student ID :" + s2.getId());

                System.out.println("Student Name :"+ s2.getName());

                System.out.println("Student Dept :"+ s2.getdept());

          }

    }

    Output:

    Student ID :100

    Student Name :null – This is not considered for serialization.

    Student Dept :IT

     


     

    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 static nested class?

    -    We can declare an inner class with a static modifier such types of inner classes are called a static nested class.

    -     In the case of regular inner class without the existing outer class object there is no chance of existing inner class object i.e. inner class object is strongly associated with an outer class object.

    -    In the case of static nested classes without the existing outer class object there may be a chance of existing inner class object. Hence, Static nested class object is not strongly associated with an outer class object.

    -     Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class StaticNestedClassDemo { 

          public static void main(String[] args) {

                Nested n = new Nested();

                n.m1();

          }     

          static class Nested{

                public void m1() {

                      System.out.println("Nested Class.");

                }

          }    

    }


        What is the purpose of Collections Utility class? What are the utility methods are available in Collections Class?

    -          Collections is a utility class for the collection framework.

    -          Collections utility class has some methods which are as follows.

    1.       addAll()

    2.       binarySearch()

    3.       checkedCollection​()

    4.       copy()

    5.       fill​()

    6.       min()

    7.       max()

    8.       replaceAll()

    9.       reverse()

    10.   shuffle()

    11.   synchronizedCollection()

    12.   unmodifiableCollection()


        What is the difference between TreeMap and SortedMap?

     

    TreeMap

    SortedMap

    1.

    TreeMap is a concrete class.

    SortedMap is an interface.


        What is ConcurrentModificationException?

    -   When one thread is performing a read operation other threads are not allowed to perform any operation on the same object still you are attempting the same then it will throw ConcurrentModificationException.

    -     Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.HashMap;

    import java.util.Iterator;

    import java.util.Map; 

    public class ConcurrentModificationExceptionDemo { 

          public static void main(String[] args) {

                HashMap<Integer,String> m = new HashMap<Integer,String>();

                m.put(101,"Shweta");

                m.put(102,"Shruti");

               

                Iterator itr = m.entrySet().iterator();

                while(itr.hasNext()) {

                      m.put(103, "Anjali");

                      Map.Entry<Integer, String> pair = (Map.Entry<Integer, String>)itr.next();                      

                      System.out.println("Key = "+ pair.getKey()+ "\t Value = "+ pair.getValue());

                }          

          }

    }

    Output:

    Exception in thread "main" java.util.ConcurrentModificationException

           at java.util.HashMap$HashIterator.nextNode(Unknown Source)

           at java.util.HashMap$EntryIterator.next(Unknown Source)

           at java.util.HashMap$EntryIterator.next(Unknown Source)

           at simplifiedjava.crackedInterview.ConcurrentModificationExceptionDemo.main(ConcurrentModificationExceptionDemo.java:17)

     


        What is type interface?

    -  Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.


        Please identify valid lambda expression?

    -          () -> System.out.println("Hello");

    -          (s) -> System.out.println("Java");

    -          (a,10) -> {System.out.println(a); System.out.println(10);}

    -          (a,b) -> System.out.println(a+b);

    -          s -> {System.out.println(s);}

    -          (int a, int b) -> {System.out.println(a+b);}

    -          s -> s.length();

    -          (s) -> {s.length();}

    Expression

    Valid / Invalid

    () -> System.out.println("Hello");

    Valid

    (s)-> System.out.println("Java");

    Valid

    (a,10) -> {System.out.println(a); System.out.println(10);}

    Valid

    (a,b) -> System.out.println(a+b);

    Valid

    s -> {System.out.println(s);}

    Valid

    (int a, int b) -> {System.out.println(a+b);}

    Valid

    s -> s.length();

    Valid

    (s) -> {s.length();}

    Valid


        How will you identify particular method is default method?

    -       All default methods must be declared inside the interface. You cannot declare the default method inside the class.

    -          All default methods are declared with the default keyword.


        What is the difference between Function and Bi-Function?

     

    Function

    Bi-Function

    1.

    The Function accepts two parameters.

    Bi-Function accepts three parameters.

    2.

    @FunctionalInterface

    public interface Function<T, U> {

     

    }

    @FunctionalInterface

    public interface BiFunction<T,U,R>{

     

    }




    • 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 the difference between volatile and atomic variable. 

    42. Can we serialize the class if class has some collection objects like arraylist, map etc.

    43. How will you instantiate the Inner class.

    44. Can you make collection object ready only. If yes then how will you make sure your collection object is read only.

    45. What is the difference between HashMap and HashTable.

    46. What is concurrency? What is the needs of Concurrency.

    47. What is type erasure.

    48. What is JIT compiler.

    49. What is the difference between JAR, WAR and EAR?

    50. How lambda expression and functional interface are related.

    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

    1 Comments