Saturday, June 14, 2014

Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers



1. Which two method you need to implement for key Object in HashMap ?
In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java.
13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.

14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap.
3. What is the difference between creating String as new() and literal?
When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.
String s = new String("Test");
does not  put the object in String pool , we need to call String.intern() method which is used to put  them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.
8. What is difference between Executor.submit() and Executer.execute() method ?
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err).

If you submitted the task with submit any thrown exception, checked exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.
ExecuterService.submit() can return result of computation because it has a return type of Future, but execute() method cannot return anything because it's return type is void.

On the other hand, submit() method is defined in the ExecutorService interface which is a sub-interface of Executor and adds the functionality of terminating the thread pool, along with adding submit() method which can accept a Callable task and return a result of computation. 

1) The submit() can accept both Runnable and Callable task but execute() can only accept the Runnable task.

2) The submit() method is declared in ExecutorService interface while execute() method is declared in the Executor interface.

3) The return type of submit() method is a Future object but return type of execute() method is void.
The difference is that execute doesn't return a Future, so you can't wait for the completion of the Runnable and get any exception it throws using that.
The difference is that execute doesn't return a Future, so you can't wait for the completion of the Runnable and get any exception it throws using that.
The submit(...) method is an executor framework extension introduced in ExecutorServiceinterface.
Its main difference from execute(Runnable) is that submit(...) can accept a Callable<V>(whereas execute() accepts only Runnable) and returns an instance of Future<V>, which you can use later in the caller to retrieve the result asynchronously (potentially blocking until the computation performed by the Callable is completed).
As you see from the JavaDoc execute(Runnable) does not return anything.
However, submit(Callable<T>) returns a Future object which allows a way for you to programatically cancel the running thread later as well as get the T that is returned when the Callable completes. See JavaDoc of Future for more details
Future<?> future = executor.submit(longRunningJob);
...
//long running job is taking too long
future.cancel(true);
Moreover, if future.get() == null and doesn't throw any exception then Runnable executed successfully
The difference is that execute simply starts the task without any further ado, whereas submitreturns a Future object to manage the task. You can do the following things with the Futureobject:
  • Cancel the task prematurely, with the cancel method.
  • Wait for the task to finish executing, with get.
The Future interface is more useful if you submit a Callable to the pool. The return value of the call method will be returned when you call Future.get. If you don't maintain a reference to the Future, there is no difference.
execute: Use it for fire and forget calls
submit: Use it to inspect the result of method call and take appropriate action on Future objected returned by the call
Major difference: Exception handling
submit() hides un-handled Exception in framework itself.
execute() throws un-handled Exception.
Solution for handling Exceptions with submit()
  1. Wrap your Callable or Runnable code in try{} catch{} block
    OR
  2. Keep future.get() call in try{} catch{} block
    OR
  3. implement your own ThreadPoolExecutor and override afterExecute method
A task queued with execute() that generates some Throwable will cause the UncaughtExceptionHandler for the Thread running the task to be invoked. The default UncaughtExceptionHandler, which typically prints the Throwable stack trace to System.err, will be invoked if no custom handler has been installed.
On the other hand, a Throwable generated by a task queued with submit() will bind the Throwable to the Future that was produced from the call to submit(). Calling get() on that Future will throw an ExecutionException with the original Throwable as its cause (accessible by calling getCause() on the ExecutionException).
submit method has 3 forms >
<T> Future<T> submit(Callable<T> task)
Submits a callable task for execution. 
Method returns a Future which represents pending results of the task. 
Once task is completed Future's get method will return the task's result.

<T> Future<T> submit(Runnable task, T result)
Submits a Runnable task for execution.
Method returns a Future which represents that task. Once task is completed Future's get method will return result.

Future<?> submit(Runnable task)
Submits a Runnable task for execution.
Method returns a Future which represents that task. Once task is completed Future's get method will return null.

24. What is difference between CyclicBarrier and CountDownLatch in Java
Main difference between both of them is that 
you can reuse CyclicBarrier even if Barrier is broken but you can not reuse CountDownLatch in Java. 

See CyclicBarrier vs CountDownLatch in Java for more differences.
Read full article from Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts