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

 Part 8

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 will happen If I directly override the run() method instead of the start() method?

    -   First of all, when you invoke the start() method on a thread, New thread will create and start executing parallelly.

    -     Run() method will execute as it is in the same thread.

    -   Run() method will be treated as a normal overridden method of the Thread class.


        What is the difference between Regular Inner Class and Static Inner Class?

     

    Regular Inner Class

    Static Inner Class

    1.

    Outer and Inner regular classes are strongly associated with each other.

    Outer and nested static classes are not strongly associated with each other.

    2.

    Static members are not allowed. i.e. static methods and static fields. Still, if you want to declare a static field inside the inner class then you must declare with the final keyword as well and assign some value to it.

    e.g. static final int  c = 10;

    Static members are allowed.

    3.

    We cannot declare main() inside the inner class so we can’t run directly through the command prompt.

    We can declare the main method inside the inner class so we can call or run directly through the command prompt.



     

    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

        How to remove the duplicates from list object without stream API?

    -          Couple of ways you can remove duplicates from the list.

    1.       By using HashSet.

    2.       By using LinkedHashSet.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.HashSet;

    import java.util.LinkedHashSet;

    import java.util.List;

    import java.util.Set; 

    public class RemoveDuplicatesFromList {

          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("----- Using HashSet-----");

                Set<Integer> listHashSet = new HashSet<Integer>(list);

                for(Integer i : listHashSet) {

                      System.out.println(i);

                }               

                System.out.println("----- Using LinkedHashSet-----");

                Set<Integer> listLinkedHashSet =new LinkedHashSet<Integer>(list);

                for(Integer i : listLinkedHashSet) {

                      System.out.println(i);

                }          

          }

    }

    Output:

    ----- Using HashSet-----

    20

    10

    30

    ----- Using LinkedHashSet-----

    10

    20

    30



        Can we use ConcurrentHashMap in place of Hashtable?

    -          Yes, definitely we can use ConcurrentHashMap in place of Hashtable.

    -          Concurrent hashmap is more flexible than hashtable.

    -          Hashtable maintains the object level lock.

    -          ConcurrentHashMap maintains the bucket level lock.


        What is the difference between SynchronizedHashMap and ConcurrentHashMap?

     

    SynchronizedHashMap

    ConcurrentHashMap

    1.

    The thread will get the whole object locked.

    The thread will get a bucket level or Segment level lock instead of a whole object lock.

    2.

    Only one thread is allowed to perform the thread-safe operation.

    Multiple threads are allowed to perform the thread-safe operation at a time.

    3.

    Every read and write operation require a full object lock.

    Read operation can perform without lock and write operation can perform by getting bucket level or Segment level lock.

    4.

    When one thread is performing a read operation no other thread is allowed to perform any operation otherwise we will get ConcurrentModificationException.

    When one thread is performing a read operation other threads are allowed to perform a write operation on the same object still you won’t get ConcurrentModificationException.

    5.

    Iterator is Fail-Fast.

    Iterator is Fail-Safe.

    6.

    Introduced in Java 1.2

    Introduced in Java 1.5.



        How will you identify lambda expression?

    -          We can identify the the lambda expression with following points.

    1.  This arrow  -> symbol should be there to implement lambda expression.

    2.  If we are passing parameters without parenthesis if there is only one parameter.

    3.   If we are returning something without a return statement.


        What will happen if I declared multiple abstract methods in one interface?

                 -   If you declare multiple abstract methods inside interface and interface is annotated with @FunctionalInterface then it will give a compile-time error.



        How will you invoke static methods from class?

    -    We can invoke the static method using the interface name. No need to create an instance of the class.

    -      Please refer to the below example. 

    package simplifiedjava.crackedInterview.java8; 

    public interface InterfaceForStaticMethod { 

          public static void m1() {

                System.out.println("m1() method called.");

          }

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class StaticMethodDemo { 

          public static void main(String[] args) {

                InterfaceForStaticMethod.m1();

          }

    }

    Output:

    m1() method called.



        What is Bi-Function functional interface?

    -   Bi-Function interface takes one argument and returns any type of object. As we have seen Bi-Predicate returns only Boolean but Function return any type of object.

    -    Please refer below example for syntax.

    @FunctionalInterface

    public interface BiFunction<T, U, R> { 

    }



        Wap to implement Bi-Function interface?

    package simplifiedjava.crackedInterview.java8; 

    import java.util.function.BiFunction; 

    public class BiFunctionInterfaceDemo { 

          public static void main(String[] args) {

           BiFunction<Integer, Integer, Integer> bf = (num1,num2) -> num1 + num2;

                Integer total = bf.apply(100, 200);

                System.out.println("Total = "+ total);

          }

    }

    Output: Total = 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:

    81. What will happen if I am trying to run a thread which is already in running state. 

    82. How will you prevent to be serialized child class when its parent class is serializable.

    83. Can you declare class inside class?

    84. Can you delcare interface inside interface?

    85. Can you declare class inside interface?

    86. Can you declare interface inside class?

    87. Which data structure is implemented for HashSet.

    88. What is TreeMap. Can you explain how the objects will be stored in TreeMap.

    89. What is the difference between Iterator and ListIterator.

    90. What is CopyOnWriteArrayList? How does it works internally?


    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