Java interview questions and answers on oops

 Part 2

Java interview questions and answers on oops covered in this post:


    Java interview questions and answers on oops

        Can we declare the main method as a final? if not then why not?

    -          Yes. We can declare a main() method as a final.

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public static final void main(String[] args) {

                System.out.println("Hi");

          }

    }

    O/P : Hi

     

        Can we create a method with the same name as the class name with return type? Will the code compile?

    -          Yes, we can create the method name the same as the class name.  Code will also compile. But remember this is not a good practice. We have a constructor with the same class name.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class Parent { 

          public static void main(String[] args) {

                new Parent().Parent();

          }         

          public void Parent() {

                System.out.println("Hello");

          }

    }

    Output: Hello

     

     

    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 override the overloaded method?

    -          Yes. We can override the overloaded method cause within a class overloading is possible but for overriding, you must have a child class where you defined the overriding method.

    -          For your reference, I have created one simple example.

    package simplifiedjava.crackedInterview; 

    public class OverrideOverloadedMethodDemo { 

          public static void main(String[] args) {       

                Parent p = new Parent();

                Child c = new Child();       

                c.check("Brother");

          }

    } 

    package simplifiedjava.crackedInterview; 

    public class Parent {     

          public void check() {

                System.out.println("Hello");

          }     

          public void check(String relation) {

                System.out.println("Parent Class check() method.");

          }    

    } 

    package simplifiedjava.crackedInterview; 

    public class Child extends Parent{ 

          public void check(String relation) {

                System.out.println("Child Class check() method.");

          }

    } 

    O/P : Child Class check() method. 

     

        Can we execute a program without a main method?

    -          Yes, we can execute a program without a main method. We can simply write code inside static block but after execution completed of a static block, you will get an error java.lang.NoSuchMethodError: main. Java 7 onwards is not possible because JVM checks the presence of the main method before initializing the class.

     

        What is the purpose of static method and static variable?

    -          Static Method:

    1.       Without instantiating the class we can invoke the method with the class name.

    -          Static Variable:

    1.       Static variable can be used to refer common property for all objects.


        What is the difference between instance method and static method?

     

    Instance Method

    Static Method

    1.

    No need to use any special keyword to declare Instance method.

    The Static method must declare with the static keyword.

    2.

    Class must be instantiated to invoke an instance method.

    No need to instantiate the class to invoke the static method.

    3.

    Instance method can be overloaded and overridden.

    The static method can be overloaded but cannot overridden.

    4.

    The Instance method can access the static variables as well as instance variables.

    The static method can access only Static variables but not instance variables.

      

        What is the difference between static block and instance block?

     

    Static Block

    Non Static Block

    1.

    The Static block declared with the static keyword.

    Non-Static block declared with no keyword.

    2.

    The Static block gets executed at the time of class loading.

    Non-Static block gets executed at the time of object instantiation.

    3.

    The Static block executes before Non-static block.

    Non-static block executes after the static block.



        Can we access non-static members inside the static area either method or block?

        -    No. We cannot access non-static members inside static area either block or method.


        What are method overriding and method overloading?

     

    Method Overriding

    Method Overloading

    1.

    Method overriding means creating a method with the same signature in a parent class and child class.

    Method overloading means creating multiple methods with a different signature and the same name in a class.

    2.

    Method overriding is run time polymorphism.

    Method overloading is compiled time polymorphism.

    3.

    We can have a single method in both classes.

    We can have multiple methods in a single class.

    4.

    Method overriding is dynamic binding.

    Method overloading is static binding.

    5.

    Covariant return applicable in method overriding.

    Covariant return cannot applicable in method overloading.

    6.

    A private and static method cannot be overridden.

    A private and static method can be overloaded.

      

        Will the code compile If the parent class method is not throwing any exception but the child class method is throwing a checked exception?

    -          No Class won’t be compiled. We will get a compile-time error.

    -          We can see the below example.

    package simplifiedjava.crackedInterview; 

    public class Parent {           

          public void check(String relation) {

                System.out.println("Parent Class check() method.");

          }    

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.FileNotFoundException; 

    public class Child extends Parent{ 

          public void check(String relation)throws FileNotFoundException {

                System.out.println("Child Class check() method.");

          }    

    }

    Output : Compile time error: Exception FileNotFoundException is not compatible with throws clause in Parent.check(String)


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

    21. Will the code compile If Child class method throwing broder excetion than parent class. 

    22. Will the code compile If parent class method is not throwing any exception but child class method is throwing an unchecked exception.

    23. Can I declare final method inside a interface.

    24. Can I declare static method inside a interface.

    25. What is the role of finalize() method.

    26. Java.lang.Object class has equals() method then why there is a need to override equals() method in user defined class. 

    27. Can you override private method.

    28. Can you overload private method.

    29. Can you overload static method.

    30.Can you override static method.

    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