Sunday, 18 August 2013

Core Java Interview Questions & Answers : part 2

This post is the extension of previous post Core Java Interview Questions


For Core Java Objective Questions click here.




Question: What is Exception
Answer : Just like compilation errors occur at compile time Exceptions are errors that occur at runtime, when the program is running.
Example of Exceptions are NullPointerException, NumberFormatException etc.

public class MyClass {

 public static void main(String[] args) {
  System.out.println("In Main");
  int i=5;
  i=i/0;
  System.out.println("Result calculated");
 }
}


Output

In Main
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at MyClass.main(MyClass.java:7)

As seen above the the message Result calculated didnt get printed. This is because a Exception was thrown at i=i/0; ArithmeticException. When a Exception occurs the remaining code does not execute and the program exists.
The Exception prints which Exception occured and also stack trace as to where the exception occured.

Question: How to do Exception Handling and what is the need of doing Exception handling
Answer: When a Exception is thrown the code that is written below the line of code where exception occurred does not get executed and the program terminates abruptly. To avoid this scenario proper Exception handling should be done.
Lets see this in a Example

public class MyClass {

 public static void main(String[] args) {
  System.out.println("In Main");
  int i = 5;
  try {
   i = i / 0;
   System.out.println("Result");
} catch (ArithmeticException e) {
   System.out.println("Exception caught");
  }
  System.out.println("Result calculated");
 }
}


Output

In Main
Exception caught
Result calculated

As seen above we have used try keyword to put the Exception throwing code inside a block and then used catch to catch that Exception thrown. Because of this the "Result" didnt get printed but the code after catch that is "Result calculated" got printed.

Question: What is Runtime and CheckedExceptions.
Answer: Runtime Exceptions are Exceptions for which the compiler will not complain and the developer do not have to provide Exception handling mechanism like try/catch or throws. Checked Exceptions are Exceptions for which the compiler will complain if the developer does not provide Exception handling mechanism.
import java.io.File;
import java.io.FileInputStream;

public class MyClass {

 public static void main(String[] args) {
  System.out.println("In Main");
  int i = 5;
  File f = new File("");
  FileInputStream fileInputStream = new FileInputStream(f);
  
 }
}


Here at line FileInputStream fileInputStream = new FileInputStream(f); the compiler gives a compile time error saying that Unhandeled Exception type FileNotFoundException. So the developer has to provide a try catch block to handle the Exception.

Question: How to create custom Exception
Answer: A custom Exception can be created by extending the Exception class. A custom Exception is a Checked Exception. That means if we throw this Exception than the compiler will complain if this Exception is not called. Lets see a Example.

Question: What is a Thread and how to implement a Thread.
Answer: Thread is a process running simultaneously. A Thread can be created in three different ways.
1. Extend Thread
2. Implement Runnable
3. Implement Callable

Question: What is synchronisation
Answer : Java synchronisation

Question: How do Threads communicate
Answer: Wait and Notify

Question: What is ReentrantLock
Answer:

Question: What is CyclicBarrier, CountDownLatch and Semaphore
Answer :  CountDownLatch, CyclicBarrier, and Semaphore

Question: What are access specifiers
Answer:

Question: What are generics
Answer:

Question: What are different collections explain their differences. Also explain how they work internally?
Answer: Java Collection


Question:What is static and init block?
Answer: Static block and Init block




Share the post