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

Part 5 

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

        What is the difference between volatile and atomic variable?

    -  Volatile Variable: Volatile keyword is used in java synchronization technique to achieve thread-safety, basically to overcome the visibility problem. The variables marked as volatile gets stored in main memory rather than CPU cache.

    -  Atomic Variable: Atomic variables are used for performing atomic operations on a single variable instead of a compounded operation, So it solves the synchronization problem ( race condition etc.)


     

    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

        Can we serialize the class if class has some collection objects like List, Map and Set?

    -          Yes, we can serialize the collection objects like List, Map and Set.

    -          All collections object implements a Serializable interface.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.Serializable;

    import java.util.List;

    import java.util.Map;

    import java.util.Set; 

    public class MyCollection implements Serializable{ 

          List<String> myList;

          Set<String> mySet;

          Map<Integer,String> myMap;     

          public MyCollection(List<String> myList, Set<String> mySet,Map<Integer,String> myMap) {

                this.myList = myList;

                this.mySet = mySet;

                this.myMap = myMap;

          } 

          @Override

          public String toString() {

                return "MyCollection [myList=" + myList + ", mySet=" + mySet + ", myMap=" + myMap + "]";

          }    

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.HashSet;

    import java.util.List;

    import java.util.Map;

    import java.util.Set; 

    public class SerializeCollectionObjectsDemo { 

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

                List<String> myList = new ArrayList<String>();

                Set<String> mySet = new HashSet<String>();

                Map<Integer,String> myMap = new HashMap<Integer,String>();

               

                myList.add("One");

                myList.add("Two");

                myList.add("Three");

               

                mySet.add("Four");

                mySet.add("Five");

                mySet.add("Six");

               

                myMap.put(1, "One");

                myMap.put(2, "Two");

                myMap.put(3, "Three"); 

                MyCollection collection = new MyCollection(myList, mySet, myMap);

               

                FileOutputStream fos = new FileOutputStream("coll.ser");

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(collection);

               

                FileInputStream fis = new FileInputStream("coll.ser");

                ObjectInputStream ois = new ObjectInputStream(fis);

                MyCollection updatedCollection = (MyCollection)ois.readObject();

               

                System.out.println(updatedCollection);           

          }

    }

    Output: MyCollection [myList=[One, Two, Three], mySet=[Five, Six, Four], myMap={1=One, 2=Two, 3=Three}]


        How will you instantiate the Inner class?

    -          There are three ways to instantiate an inner class.

    package simplifiedjava.crackedInterview; 

    public class Outer { 

          static int i = 0;     

          class Inner{

              public void m1() {

                  System.out.println("Inner class m1() called. Counter = "+ ++i);

                }

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class InstantiatingInnerClassDemo { 

          public static void main(String[] args) {           

                // First way to create inner class instance.         

                Outer o = new Outer();

                Outer.Inner i = o.new Inner();

                i.m1();           

                // Second way to create inner class instance.

                Outer.Inner i2 = new Outer().new Inner();

                i2.m1();           

                // Third way to create inner class instance.

                new Outer().new Inner().m1();

          }

    } 

    Output:

    Inner class m1() called. Counter = 1

    Inner class m1() called. Counter = 2

    Inner class m1() called. Counter = 3

     


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

    -  We can make collection read-only by unmodifiableList() method which exists in the Collections utility class.

    -    If you apply the unmodifiableList() method on any list object then you are not allowed to modify the list object. Still, you try to modify it then you will get UnSupportedOperationException.

    -    Make sure once you invoked to unmodifiableList() method on the list then you have to assign the same list to the old reference variable.

    -   If you don’t assign then you won’t get UnSupportedOperationException and you will be able to modify the list.

    -     Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.List; 

    public class UnmodifiableListDemo { 

          public static void main(String[] args) {           

                List<Integer> list = new ArrayList<Integer>();

                list.add(10);

                list.add(20);

                list.add(30);

                list.add(30);

                list.add(10);

     

                System.out.println("Before Unmodified "+ list);

                list = Collections.unmodifiableList(list);

                list.remove(2);        

                System.out.println("After Unmodified "+ list);

          }

    }

    Output:

    Exception in thread "main" Before Unmodified [10, 20, 30, 30, 10]

    java.lang.UnsupportedOperationException

           at java.util.Collections$UnmodifiableList.remove(Unknown Source)

                   at simplifiedjava.crackedInterview.UnmodifiableListDemo.main(UnmodifiableListDemo.java:21)


        What is the difference between HashMap and HashTable?

     

    HashMap

    HashTable

    1.

    All methods of HashMap are not synchronized.

    All methods of HashTable are synchronized.

    2.

    HashMap is not thread-safe.

    Hashtable is thread-safe.

    3.

    The performance of HashMap is relatively high as compared to Hashtable.

    The performance of Hashtable is relatively low as compared to HashMap.

    4.

    Only one NULL key is allowed and multiple NULL values are allowed.

    Null is not allowed either key or value otherwise it will throw NullPointerException.

    5.

    HashMap is not a legacy class.

    Hashtable is a legacy class.

    6.

    HashMap introduced in Java 1.2 version.

    Hashtable was introduced in Java 1.0 version.


        What is Concurrency? What is the need of Concurrency?

    -          Concurrency is the ability to run multiple programs at a time.

    -          For concurrency threads are responsible.

    -         Concurrency package provides more flexible classes than synchronized methods or blocks.

    -          Concurrency belongs to java.util.Concurrent package.

    -          Needs of Concurrency are as follows.

    1. Traditional collection object ArrayList, LinkedList, HashSet and Hashmap access by multiple threads so data inconsistency problem may occur hence, these are not thread-safe. But, Concurrency classes are thread-safe.

    2. Already existing thread-safe collection Vector, Stack, SynchronizedMap, SynchronizedSet are not good performance-wise. But, Concurrent package classes are very good performance-wise.

    3.       Only one thread can execute thread-safe objects so other threads must wait until the first thread completes its task so performance goes down. Concurrent classes overcome these problems.

    4. Another big problem is, when one thread is iterating collection objects, the other thread can not modify the same collection object. When the first tread is iterating the collection object at the same time another thread is trying to modify the collection object then we will get ConcurrentModificationException.


        What is type Erasure?

    -  Generic provides compile-time java files all generics syntax will be removed.

              -    At runtime generics syntax that won’t be available is called Type Erasure.


        What is JIT compiler?

    -  The Just-In-Time (JIT) compiler is a component of the runtime environment that improves the performance of Java applications by compiling bytecodes to native machine code at run time.


        What is the difference between JAR, WAR and EAR?

     

    JAR

    WAR

    EAR

    1.

    JAR stands for Java Archive.

    WAR stands for Web Application Archive or Web  Application Resources.

    EAR stands for Enterprise Application Archive.

    2.

    Extension of the JAR file is .jar

    Extension of WAR file is .war

    Extension of EAR file is .ear

    3.

    The JAR file contains java classes, resources such as text, images aggregated into one file.

    WAR file contains JSP, servlets, XML, static web pages.

    EAR file represents the modules of the application and meta-data directory called META-INT which has one or more deployment descriptors.


        How lambda expression and functional interface are related?

    -          Functional interface must have only one abstract method.

    -          Lambda expression can call an abstract method of functional interface.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview.java8; 

    @FunctionalInterface

    public interface FunctionalInterfaceDemo {

          public void add(int no1,int no2);

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class FunctionalInterfaceImplDemo { 

          public static void main(String[] args) {

                FunctionalInterfaceDemo f = (no1,no2) -> System.out.println("Addition of Two nos = "+ (no1+no2));

                f.add(100, 200);

          }

    }

    Output:

    Addition of Two nos = 300 


    • 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. Can we create a try block inside a try block.

    2. Can I apply transient keyword to premitive,static and reference variables. 

    3. Can we declare constructor inside inner class.

    4. Which Collection objects implements Queue interface.

    5. What is the difference between SortedSet and TreeSet.

    6. What is the difference between Fail-Fast and Fail-Safe.

    7. What is the difference between HashTable and ConcurrentHashMap.

    8. List out some web servers and application servers?

    9. Can you compare lambda expression and Inner classes.

    10. What is Supplier functional 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