Oracle Java interview questions and answers.

Java Interview Questions and Answers covered in this post:

    Java interview questions and answers.


        What is the difference between Local variable and Instance variable?

     

    Local Variable

    Instance Variable

    1.

    Local variable declares inside the method, block and constructor.

    Instance variable declared outside the method, block and constructor but inside the class.

    2.

    We have to manually initialize the local variable before use.

    When instance variable gets created default value assigned to that variable.

    3.

    Scope of local variable up to method execution.

    Instance variable scope is wider than the local variable.

     

        Can you declare constructor inside interface?

    -          Constructor cannot be declared inside interface.

             -        Constructor is mainly used to initialize the value. We cannot instantiate the interface so we cannot declare the constructor inside an interface.


        Can we declare a class as Abstract without having a single abstract method?

    -  You can declare an abstract class without a single abstract method declared inside an abstract class.

             -     Make a note if you declared the already abstract method then it is mandatory to declare the class as abstract class.


        What is the purpose of the final keyword where it can be applicable?

    -          Final keyword can be applied to the following.

    1.       Variable: Final variable value cannot change once assigned.

    2.      Method: Final method cannot override.

                       3.     Class: Final class cannot be sub-classed.


        What are Boxing and Unboxing?

    -    The conversion of primitive data type into its corresponding wrapper class is called boxing.

    -   The conversion of wrapper class type into its corresponding primitive type is called unboxing.

    -    Please refer to the below example for boxing and unboxing.

    package simplifiedjava.crackedInterview; 

    public class BoxingUnboxingDemo { 

          public static void main(String[] args) {           

                int num = 100;

                Integer wrapperNum1 = new Integer(num);

                Integer wrapperNum2 = 200;

                System.out.println("Auto Boxing "+ wrapperNum1 + " and " + wrapperNum2);           

                Integer wrapperNum3 = new Integer(300);

                num = wrapperNum3;

                System.out.println("Unboxing "+ num);

          }

    }

    Output :

    Auto Boxing 100 and 200

    Unboxing 300



     

    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 create an immutable class or What are the steps to create immutable objects?

    -          Please refer to the below steps to create an immutable class.

    1.       Create a final class so a subclass is not possible.

    2.       Make all instance variables final so no one can change the value.

    3.       Do not provide any setter method just provide a getter method.

    -          For your reference, I have created a new immutable class.

    package simplifiedjava.crackedInterview; 

    public final class ImmutableClassDemo { 

          private final String empCode="E001"; 

          public String getEmpCode() {

                return empCode;

          }

    }


        Which JVM Parameter is used to control stack size of thread?

    -          Parameter: -Xssn represents Stack Size;

             -       Every thread that is spawned during the execution of the program passed to java has n as its C stack size.


        What is the use of ExecutorService interface?

    -   The Java ExecutorService is the interface that allows us to execute tasks on threads asynchronously.

    -  The Java ExecutorService interface is present in the java.util.Concurrent package.

    -   Executors execute() method takes a runnable object and performs the task asynchronously.

    -    After making the call of execute() method we call the shutdown() method.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.concurrent.ExecutorService;

    import java.util.concurrent.Executors; 

    public class ExecutorServiceDemo { 

          public static void main(String[] args) {

                ExecutorService executorService = Executors.newFixedThreadPool(10); 

            executorService.execute(new Runnable() {              

                @Override 

                public void run() { 

                    System.out.println("ExecutorService");                   

                } 

            }); 

            executorService.shutdown(); 

        } 

    }

    Ouput: ExecutorService

     

    -          Executors submit() method takes a runnable object and returns a Future object. Later on, this object can be used to check the status of Runnable whether it has completed execution or not.

    -        Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.concurrent.ExecutorService;

    import java.util.concurrent.Executors;

    import java.util.concurrent.Future; 

    public class ExecutorServiceDemo { 

          public static void main(String[] args) {

                 ExecutorService executorService = Executors.newSingleThreadExecutor(); 

               Future future = executorService.submit(new Runnable() {                    

                      @Override 

                      public void run() { 

                          //System.out.println("ExecutorService");               

                      } 

             });   

               System.out.println(future.isDone());

        } 

    }

    Output: false

     

    -         Executors invokeAny() method takes a collection of callable objects. This method returns the future object of the callable objects which are executed first successfully.

    -       Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.HashSet;

    import java.util.Set;

    import java.util.concurrent.Callable;

    import java.util.concurrent.ExecutionException;

    import java.util.concurrent.ExecutorService;

    import java.util.concurrent.Executors;

    import java.util.concurrent.Future; 

    public class ExecutorServiceDemo { 

          public static void main(String[] args) throws InterruptedException, Exception {          

                // invoke method demo

                ExecutorService executorService = Executors.newSingleThreadExecutor();

                Set<Callable<String>> callableSet = new HashSet<Callable<String>>();       

                callableSet.add(new Callable<String>() {

                      public String call() throws Exception{

                            return "Job 1";

                      }

                });           

                callableSet.add(new Callable<String>() {

                      public String call()throws Exception{

                            return "Job 2";

                      }

                });           

                callableSet.add(new Callable<String>() {

                      public String call()throws Exception{

                            return "Job 3";

                      }

                });           

                String executedJob = executorService.invokeAny(callableSet);

                System.out.println(executedJob);

                executorService.shutdown();

        }      

    }

    Output: Job 2

     

    -    The invokeAll() method takes in a Collection of Callable objects having tasks and returns a list of Future objects containing the result of all the tasks.

    package simplifiedjava.crackedInterview; 

    import java.util.HashSet;

    import java.util.List;

    import java.util.Set;

    import java.util.concurrent.Callable;

    import java.util.concurrent.ExecutionException;

    import java.util.concurrent.ExecutorService;

    import java.util.concurrent.Executors;

    import java.util.concurrent.Future; 

    public class ExecutorServiceDemo { 

          public static void main(String[] args) throws InterruptedException, Exception {           

                // invoke method demo

                ExecutorService executorService = Executors.newSingleThreadExecutor();

                Set<Callable<String>> callableSet = new HashSet<Callable<String>>();       

                callableSet.add(new Callable<String>() {

                      public String call() throws Exception{

                            return "Job 1";

                      }

                });           

                callableSet.add(new Callable<String>() {

                      public String call()throws Exception{

                            return "Job 2";

                      }

                });           

                callableSet.add(new Callable<String>() {

                      public String call()throws Exception{

                            return "Job 3";

                      }

                });           

                List<Future<String>> executedJob = executorService.invokeAll(callableSet);           

                for(Future future: executedJob) {

                      System.out.println(future.get());

                }                      

                executorService.shutdown();                       

        }      

    }

    Output:

    Job 2

    Job 3

    Job 1

     


        What is the role of Future interface in Multithreading?

    -      A Future represents the result of an asynchronous computation.

    -     Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

    -   The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready.

    -    Cancellation is performed by the cancel method.

    -   Additional methods are provided to determine if the task was completed normally or was cancelled.

    -  Once a computation has been completed, the computation cannot be cancelled.

    -   If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.


        What is the fill ratio in collection framework and what is the default fill ratio?

    -  Fill ratio is a measure that decides when to increase the size of the collection.

    -    Default fill ratio is 75%. It means when your collection object gets full by 75% then collection size will increase the space internally.

    -   Every collection object has its own way or different formula to increase the size internally.


    • 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

    Questions covered in upcoming post:

    1. How would you implement Multiple Inheritance in java?

    2. In your program you have static block, non-static block, constructor and main(). What will be the sequence of execution.

    3. What is the purpose to make a constructor private.

    4. What is instanceOf in java. Where it can be used.

    5. What is the difference between equals() and ==.

    6. What is Comparable and Comparator. What is the difference between Comparable and Comparator. 

    7. What is the difference between checked exception and unchecked exceptions.

    8. Can you explain why NullPointerException occurred and how to handle it? 

    9. What is Thread and what is process. What is the difference between these two.

    10.What are the advantages and disadvantages of Serialization.


    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