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

 Part 12

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 experience1.      

        Will the child interface functional interface?

    1. @FunctionalInterface

    public interface Parent {

          public void add();

    }                                            

    @FunctionalInterface

    public interface Child extends Parent {  

    }

     

    2.@FunctionalInterface

    public interface Parent {

          public void add();

    }

    @FunctionalInterface

    public interface Child extends Parent {

          public void add();

    }

     

    3.@FunctionalInterface

    public interface Parent {

          public void add();

    }

    @FunctionalInterface

    public interface Child extends Parent {

          public void substract();

    }

     

    4.@FunctionalInterface

    public interface Parent {

          public void add();

    }

    public interface Child extends Parent {

          public void substract();

    }

     

    -      Case 1: If an interface extends functional interface and the child interface doesn’t contain any abstract method, then the child interface is always a functional interface.

    -     Case 2: In the child interface we can define exactly the same parent interface abstract method.

    -        Case 3: In the child interface we can’t define any new abstract method then we will get a compile-time error.

    -          Case 4: Child interface doesn’t declare FunctionalInterface then code will compile. 


     

    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 the difference between Annonymous Inner class and Lambda Expression?

     

    Annonymous Inner class

    Lambda Expression

    1.

    It is a class without a name.

    It is a function without a name.

    2.

    An anonymous inner class can extend an abstract class or concrete class.

    Lambda expression cannot extend an abstract class or concrete class.

    3.

    An anonymous inner class can implement an interface that has multiple abstract methods as well as a functional interface.

    Lambda expression can implement only a functional interface.

    4.

    Inside the inner class, we can declare an instance variable.

    Inside lambda expression, we can declare only local variables.

    5.

    An anonymous inner class can be instantiated.

    Lambda expression cannot be instantiated.

    6.

    An anonymous interface is the best choice if we want to handle multiple methods.

    Lambda expression is the best choice if we want to handle the interface with a single abstract method.

    7.

    Inside the Anonymous inner class, ‘this’ always refers current anonymous inner class object but not the outer class object.

    Inside lambda expression, this always refers current outer class object.

    8.

    For an anonymous inner class at the time of compilation, a separated .class file will be generated.

    For the lambda expression, separate .class file won’t be generated.



        What are default methods? What is the basic purpose of default methods?

    -   Methods defined inside interface with default keyword is called default method.

    -     Since, Java 8 you can have method implementation inside the interface.

    -     Default methods can be used for backward compatibility.

    -    In the regular case, if you declare or add a new method in the interface then million of code may break so avoiding this situation, we can add a default method inside the interface so no code will be affected.

    -     Please refer to the below example.

    package simplifiedjava.crackedInterview.java8; 

    public interface InterfaceForDefaultMethod { 

          default void m1() {

          System.out.println("m1() from DefaultMethodDemo interface is called");

          }

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class DefaultMethodDemo implements InterfaceForDefaultMethod{ 

          public static void main(String[] args) {

                InterfaceForDefaultMethod demo = new DefaultMethodDemo();

                demo.m1();       

                DefaultMethodDemo demo1 = new DefaultMethodDemo();

                demo1.m1();

          }

    }

    Output:

    m1() from DefaultMethodDemo interface is called

    m1() from DefaultMethodDemo interface is called


        How will you uniquely call same methods declared in multiple interfaces? If Inteface A has add()  and interface B also have add() then how will you differentiate both methods?

    -          Please refer below example.

    package simplifiedjava.crackedInterview.java8; 

    public interface Left { 

          default void m1() {

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

          }

    } 

    package simplifiedjava.crackedInterview.java8; 

    public interface Right {

          default void m1() {

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

          }

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class LeftRightInterfaceDemo implements Left, Right{ 

          public static void main(String[] args) {

                LeftRightInterfaceDemo demo = new LeftRightInterfaceDemo();

                demo.m1();

          } 

          @Override

          public void m1() {

                Left.super.m1();

                Right.super.m1();

          }

    }

    Output:

    Left m1() called.

    Right m1() called.


        How will you invoke default methods from class?

    -      First, you need to declare the default method in the interface.

    -     Second, you have to create one class and implement that interface that has the default method.

    -     Then, simply instantiate the class and with the class reference variable, you can call default methods.

    -       Please refer to the below example.

    package simplifiedjava.crackedInterview.java8; 

    public interface InterfaceForDefaultMethod { 

          default void m1() {

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

          }

    } 

    package simplifiedjava.crackedInterview.java8; 

    public class DefaultMethodDemo implements InterfaceForDefaultMethod{ 

          public void m1() {

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

          }         

          public static void main(String[] args) {

                InterfaceForDefaultMethod demo = new DefaultMethodDemo();

                demo.m1();       

                DefaultMethodDemo demo1 = new DefaultMethodDemo();

                demo1.m1();

          }

    }

    Output:

    Class m1() method called.

    Class m1() method called.

     


        What are the static methods in functional interface?

    -          Static method declared inside the interface is called the static method.

    -          We can have the implementation of static class inside the interface.

             -        Static methods can invoke with the Interface name. No need to instantiate the class.


        What is the purpose of static methods in functional interface?

    -    Just to define utility methods in the interface then we can go for static methods.

    -   The main purpose of the static method is, we can invoke static methods like the utility class method. No need to create an instance of that class.


        What are the static methods defined in Predicate functional interface?

    -          Predicate has defined isEqual () static method inside it.

    -          Please refer to the below example for syntax.

    package simplifiedjava.crackedInterview.java8; 

    import java.util.function.Predicate; 

    public class PredicateStaticMethodDemo { 

          public static void main(String[] args) {

                Predicate<String> p = Predicate.isEqual("Java");

                System.out.println(p.test("java"));

                System.out.println(p.test("Java"));

                System.out.println(p.test("Databse"));

          }

    }

    Output:

    false

    true

    false 


        Wap to implement Predicate interface?

    package simplifiedjava.crackedInterview.java8; 

    import java.util.ArrayList;

    import java.util.List;

    import java.util.function.Predicate; 

    public class FindPositiveOrNegativeNoUsingPredicate { 

          public static void main(String[] args) {           

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

                list.add(-10);

                list.add(20);

                list.add(30);                     

                Predicate<Integer> p = no -> no > 0;           

                for(Integer i : list) {

                      if(p.test(i)) {

                            System.out.println("Positive Number");

                      }else {

                            System.out.println("Negative Number");

                      }

                }

          }

    }

    Output:

    Negative Number

    Positive Number

    Positive Number


        WAP to implement Function interface?

    package simplifiedjava.crackedInterview.java8; 

    import java.util.function.Function; 

    public class FunctionInterfaceDemo { 

          public static void main(String[] args) {

                Function<String, Integer> f = s -> s.length();

                System.out.println(f.apply("Simplify Java"));

          }

    }

    Output: 13


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

    121. What are the static method defined in Function functional interface.

    122. What is the difference between Predicate and Function interface.

    123. What is Consumer functional interface?

    124. What is the abstract method defined in Consumer functional interface.

    125. What are the default methods defined in Consumer functional interface.

    126. What is Bi-Consumer functional interface.

    127. What is the abstract method defined in Supplier functional interface.

    128. What are the default methods defined in Supplier functional interface.

    129. What are the static method defined in Supplier functional interface.

    130. What is the difference between Predicate and Bi-Predicate.

    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