IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application.

Say the Excel Jar files with utility methods used the application uses the methods in the Utility Class and the flow is controlled by the way the method gets called in order the get the things done.

whereas

In framework like spring the implementation is defined in XML files and by using annotation and the framework calls the methods as per defined in xml.

Without Ioc

  Application -> Methods -> Framework      

With Ioc

  Framework -> XML File ->  Method Call (or) Implementation      

Inversion of Control(IoC) Container:
Common characteristic of frameworks IOC manages java objects

  1. From instantiation to destruction through its BeanFactory.
  2. Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean’s scope, lifecycle events, and any AOP features for which it has been configured and coded.

Flow of control is “inverted” by dependency injection because you have effectively delegated dependencies to some external system

The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.

For example, say your application has a text editor component and you want to provide spell checking. Your standard code would look something like this:

public class TextEditor
{
    private SpellChecker checker;
    public TextEditor()
    {
        this.checker = new SpellChecker();
    }
}

What we’ve done here is create a dependency between the TextEditor and the SpellChecker. In an IoC scenario we would instead do something like this:

public class TextEditor
{
    private ISpellChecker checker;
    public TextEditor(ISpellChecker checker)
    {
        this.checker = checker;
    }
}

Now, the client creating the TextEditor class has the control over which SpellChecker implementation to use. We’re injecting the TextEditor with the dependency.

Without IoC: you ask for “apple”, and you are always served apple when you ask more.

With IoC:
You can ask for “fruit”. You can get different fruits each time you get served. for example, apple, orange, or water melon.

Inversion of Control, (or IoC), is about getting
freedom (You get married, you lost freedom and you are being controlled. You divorced, you have just implemented Inversion of Control. That’s what we called, “decoupled”. Good computer system discourages some very close relationship.)

flexibility (The kitchen in your office only serves clean tap water, that is your only choice when you want to drink. Your boss implemented Inversion of Control by setting up a new coffee machine. Now you get the flexibility of choosing either tap water or coffee.)

less dependency (Your partner has a job, you don’t have a job, you financially depend on your partner, so you are controlled. You find a job, you have implemented Inversion of Control. Good computer system encourages in-dependency.)

Dependency Injection

What is Dependency

Quick Example:EMPLOYEE OBJECT WHEN CREATED,
              IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT
   (if address is defines as dependency by Employee object)

Now in the above EMPLOYEE Class is dependent on ADDRESS Class.

You can not create the Address Object unless you create Employee Object

EMPLOYEE Class is dependent and ADDRESS Class is dependency

What is the purpose of DI?
With dependency injection, objects don’t define their dependencies themselves, the dependencies are injected to them as needed.The purpose of Dependency Injection is to reduce coupling in your application to make it more flexible and easier to test.

How does it benefit ? The objects don’t need to know where and how to get their dependencies, which results in loose coupling between objects, which makes them a lot easier to test.

When to use Dependency Injection
One of the most compelling reasons for DI is to allow easier unit testing without having to hit a database and worry about setting up ‘test’ data.Dependency Injection gives you the ability to test specific units of code in isolation.

Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself. It’s a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.

Dependencies can be injected into objects by many means (such as constructor injection or setter injection). One can even use specialized dependency injection frameworks (e.g Spring) to do that, but they certainly aren’t required.

Example
A Car depends on wheels, engine, fuel, battery, etc. to run. Traditionally we define the brand of such dependent objects along with the definition of the Car object

Without Dependency Injection (DI):

class Car
{
  private Wheel wh   = new ApolloWheel();
  private Battery bt = new ExideBattery();

  //The rest
}

Here, the Car object is responsible for creating the dependent objects.

What if we want to change the type of its dependent object – say Wheel – after the initial ApolloWheel() punctures? We need to recreate the Car object with its new dependency say SpareWheel(), but only the Car manufacturer can do that.

Then what does the Dependency Injection do us for…?

When using dependency injection, objects are given their dependencies at run time rather than compile time (car manufacturing time). So that we can now change the Wheel whenever we want. Here, the dependency (wheel) can be injected into Car at run time.

After using dependency injection:

class Car
{
  private Wheel wh   = [Inject an Instance of Wheel at runtime];
  private Battery bt = [Inject an Instance of Battery at runtime];

  Car(Wheel wh,Battery bt) 
  {
      this.wh = wh;
      this.bt = bt;
  }

  //Or we can have setters
  void setWheel(Wheel wh) 
  {
      this.wh = wh;
  }
}

Lets take the below example

public class Foo
{
    private Bar _bar;

    public Foo(Bar bar)
    {
        _bar = bar;
    }

    public bool IsPropertyOfBarValid()
    {
        return _bar.SomeProperty == PropertyEnum.ValidProperty;
    }
}

without dependency injection the Bar object is dependent and tightly coupled with Foo class like below

public class Foo
{
    private Bar _bar = new Bar();
    .
    . 
}

But by using dependency injection like before code you can mock the Bar Object at runtime and call the IsPropertyOfBarValid() method over it.

String Questions

  1. How to find the Longest Substring in String
  2. Swapping Letters in 2 Words
  3. Print String in Reverse Order
  4. Print Left Right Angle Triangle
  5. Inverted Right Triangle
  6. Print Right Angle Triangle
  7. Print Pyramid
  8. Sum of Pairs in Array of Numbers
  9. Check 2nd List has all elements in 1st List
  10. How to find String is anagram
  11. Check Palindrome
  12. Reverse of String without for loop and array
  13. Check Palindrome with recursion

1.How to find the Longest Substring in String
Input

String strSample = "ABCAAABCD"

Output

ABCD
public static void main(String[] args) 
{
   String Test = "ABCAAABCD";
   String dummy = ""; 
		
   String[] arrTest = Test.split("");
		
   List<String> seenChars = new ArrayList<String>();
   List<String> subStrings = new ArrayList<String>();
		
   int j = 0;
		
   for (int i = 1; i < arrTest.length; i++) 
   {
      if(seenChars.contains(arrTest[i]))
      {
	 subStrings.add(dummy);
	 dummy = "";
	 seenChars.clear();
      }
			
      seenChars.add(arrTest[i]);
      dummy += arrTest[i];
			
       j = i + 1;
			
       if(j == arrTest.length)
	  subStrings.add(dummy);
   }
		
   int highestNo = 0;
   int lastHighest = 0;
   int currHighestNo = 0;
   int highestPOS = 0;
	
   for(int k = 0; k < subStrings.size() ; k++)
   {
	currHighestNo = subStrings.get(k).length();
		
	if(currHighestNo  > lastHighest)
	{
  	  highestNo = currHighestNo;
	  highestPOS = k;
	}		
	  lastHighest = currHighestNo;
	}
		
	System.out.println(subStrings.get(highestPOS));		
}

Method 2

public static void main(String args[]) 
{
	String Test = "ABCDEAAABCDA";
	String dummy = "";

	String[] arrTest = Test.split("");
	String longestSubString = "";

	List<String> seenChars  = new ArrayList<String>();
	List<String> subStrings = new ArrayList<String>();

	for (String strTest : arrTest) 
	{
		if (seenChars.contains(strTest)) 
		{
			if (dummy.length() > longestSubString.length())
				longestSubString = dummy;

			subStrings.add(dummy);
			dummy = "";
			seenChars.clear();
		}
		
		seenChars.add(strTest);
		dummy += strTest;
	}
	
	if (dummy.length() > longestSubString.length())
	{
		longestSubString = dummy;		
		subStrings.add(dummy);	
	}
	 
	System.out.println("Longest subString is: " + "'" + longestSubString + "'" + " and length is: "
			+ longestSubString.length());
}

2.Swapping Letters in 2 Words
Input

MUGIL XXX

Output

MXUXGXIL
public class Array1 
{
 static List arrNewList1 = new ArrayList();
 
 public static void main(String[] args) 
 { 
   String text1 = "MUGIL";
   String text2 = "XXX"; 
 
   String[] arrList1 = text1.split("");
   String[] arrList2 = text2.split("");
 
   if(arrList2.length > arrList1.length)
    swapArrays(arrList2, arrList1);
   else 
    swapArrays(arrList1, arrList2);

 
 
   StringBuilder strBr = new StringBuilder();
 
   for (int i = 0; i < arrNewList1.size(); i++) 
    strBr.append(arrNewList1.get(i));
 
    System.out.println(strBr); 
 }
 
 public static void swapArrays(String[] pBigList, String[] pSmallList)
 { 
   for (int i = 0; i < pBigList.length; i++) 
   { 
     arrNewList1.add(pBigList[i]);
 
     if(i < pSmallList.length)
      arrNewList1.add(pSmallList[i]);
   }
 }
}

Note

 .
 .
 String Text1 = "M,U,G,I,L";
 String Text2 = "MUGIL";
		
 System.out.println(Text1.split(",").length);
 System.out.println(Text2.split("").length);
 .
 .

Output

5
6

you may be puzzled why first prints 5 and second prints 6.Thats because the empty space between “M is taken as 1 element in Text2

3.Print String in Reverse Order
Input

asanam

Output

asanam
package com.mugil.test;

public class ReverseString {

 public static void main(String[] args) {
  String strName = "manasa";

  //char[] arrNames = strName.toCharArray();

  String[] arrNames = strName.split("");

  for (int i = arrNames.length - 1; i >= 0; i--) {
   System.out.print(arrNames[i]);
  }
 }
}

4.Print Left Right Angle Triangle
Input

Total number of Rows - 5

Output

* 
** 
*** 
**** 
***** 
package com.mugil.test;

public class Patterns2 {
 public static void main(String[] args) {
  int rows = 5;

  for (int i = 1; i <= rows; i++) {
   for (int j = 1; j <= i; j++) {
    System.out.print("*");
   }

   System.out.println(" ");
  }
 }
}

5.Inverted Right Triangle
Input

Total number of Rows - 5

Output

***** 
**** 
*** 
** 
*  
.
int rows = 5;

for (int i = rows; i >= 0; i--) {
 for (int j = 1; j <= i; j++)
  System.out.print("*");

 System.out.println(" ");
}
.

6.Print Right Angle Triangle
Input

Total number of Rows - 5

Output

     * 
    ** 
   *** 
  **** 
 *****  
int n = 5;
int j = 0;
int k = 0;

for (int i = 0; i < n; i++) {
 for (j = n - i; j > 1; j--)
  System.out.print(" ");

 for (j = 0; j <= i; j++)
  System.out.print("*");

 System.out.println();

}

7.Print Pyramid
Input

Total number of Rows - 5

Output

    * 
   * * 
  * * * 
 * * * * 
* * * * *  
int n = 5;
int j = 0;
int k = 0;

for (int i = 0; i < n; i++) 
{
 for (j = n - i; j > 1; j--)
  System.out.print(" ");

 for (j = 0; j <= i; j++)
  System.out.print("* ");

 System.out.println();

}

The only Difference between Right Angle Triangle and Pyramid is Space in *

8.Sum of Number Pairs in Array
Input

Array of Numbers - {0,1,2,2,3,4}
Sum - 4

Output

(0,4)
(1,3)
(2,2)
.
Integer[] arrNumbers = { 0, 1, 2, 2, 3, 4, 5};
int sum = 4;

for (int i = 0; i < arrNumbers.length; i++) {
 for (int j = i + 1; j < arrNumbers.length; j++) {
  if (arrNumbers[i] + arrNumbers[j] == sum) {
   System.out.println("(" + arrNumbers[i] + "," + arrNumbers[j] + ")");
  }
 }
}
.

9.Check 2nd List has all elements in 1st List
Input

2 List of Elements
{1,2,3,4}{2,3}
{1,2,3,4}{2,5}
{2,3}{2,5,6}  
{2,3}{2,3,9}  

Output

{1,2,3,4}{2,3}  -> true
{1,2,3,4}{2,5}  -> false
{2,3}{2,5,6}    -> false
{2,3}{2,3,9}    -> false
public boolean checkEquals()
{
  Integer orig[] = {1,2,3,4};
  Integer act[] = {2,3};
           
  List origList = new ArrayList(Arrays.asList(orig));
  List actList = Arrays.asList(act);
   
  origList.retainAll(actList);
  System.out.println(origList);
      
  if(actList.size()==origList.size())   
    return true;
  else
    return false; 
}

10.How to find String is anagram?
Output

Keep and Peek are anagrams
silent and listen are anagrams
MotherInLaw and HitlerWoman are anagrams
public class AnagramString 
{
 static void isAnagram(String str1, String str2) 
 {
  String s1 = str1.replaceAll("\\s", "");
  String s2 = str2.replaceAll("\\s", "");
  boolean status = true;

  if (s1.length() != s2.length()) 
   status = false;
  else 
  {
   char[] ArrayS1 = s1.toLowerCase().toCharArray();
   char[] ArrayS2 = s2.toLowerCase().toCharArray();
   Arrays.sort(ArrayS1);
   Arrays.sort(ArrayS2);
   status = Arrays.equals(ArrayS1, ArrayS2);
  }

  if (status)
   System.out.println(s1 + " and " + s2 + " are anagrams");
  else
   System.out.println(s1 + " and " + s2 + " are not anagrams");  
 }

 public static void main(String[] args) 
 {
  isAnagram("Keep", "Peek");
  isAnagram("silent", "listen");
  isAnagram("Mother In Law", "Hitler Woman");
 }
}

11.Check Palindrome
Input

malayalam

Output

Entered string is a palindrome
 public static void main(String args[])
 {
      String original, reverse = "";
      original = "madam";
 
      int length = original.length();
 
      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);
 
      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome."); 
  }

12.Reverse of String without for loop and array
Input

mugil

Output

ligum
static String reverseMe(String s) 
{
   if(s.length() <= 1)
     return s;

   return s.charAt(s.length() - 1) + reverseMe(s.substring(0,s.length()-1));
 }

13.Check palindrome using recursion
Input

malayalam

Output

palindrome

Call the above reverse function and check with string equals method

public static boolean isPalindrome(String input) 
{
 if (input == null)
  return false;

 String reversed = reverse(input);
 return input.equals(reversed);
}

public static String reverse(String str) 
{ 
 if (str == null) 
  return null;
 
 if (str.length() <= 1) 
  return str;
 
 return reverse(str.substring(1)) + str.charAt(0);
}

length – arrays (int[], double[], String[]) —- to know the length of the arrays.

length() – String related Object (String, StringBuilder etc)to know the length of the String

size()– Collection Object (ArrayList, Set etc)to know the size of the Collection

length is not a method, so it completely makes sense that it will not work on objects. Its only works on arrays.

Points to Note

  1. ArrayList allows NULL values many times
  2. Set and HashMap allows NULL once
  3. In HashMap the values will be overwritten when keys are same

List An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

MapAn object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Test.java

public class Test
{
 public static void main(String[] args) 
 {
 List<String> arrNames = new ArrayList<String>();
 
 arrNames.add("Hello");
 arrNames.add(null);
 arrNames.add(null);
 arrNames.add("Hi"); 
 System.out.println("List - "+arrNames.size());
 
 Map hmTest = new HashMap();
 hmTest.put("1", "mugil");
 hmTest.put(null, null);
 hmTest.put(null, null);
 hmTest.put("1", "vannan");
 System.out.println("HashMap - "+ hmTest.size());
 
 Set<Integer> setTest = new HashSet<Integer>();
 
 setTest.add(new Integer("10"));
 setTest.add(new Integer("20"));
 setTest.add(new Integer("10"));
 
 System.out.println("Set - "+ setTest.size());
 }
}

Output

List - 4
HashMap - 2
Set - 2

If I write something like this

 System.out.println(19);

Which type has the ’19’? Is it int or byte? Or doesn’t it have a type yet?
This 19 is known as an integer literal. There are all sorts of literals, floating point, String, character, etc.

What is integer literal
Integer data types consist of the following primitive data types: int,long, byte, and short.byte, int, long, and short can be expressed in decimal(base
10), hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals.

Examples:

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

Literal means any number,Text or Other information that represents a value.

Different Values that can be assigned to Integer Variable (Integer data type Literal) are

  1. Decimal Literals
  2. Octal Literals
  3. Hexadecimal Literals
  4. Binary Literals
  5. Long Literals
  6. Values with Underscore in Between

Why main() method is Static

  1. the main function is where a program starts execution. It is responsible for the high-level organization of the program’s functionality, and typically has access to the command arguments given to the program when it was executed.
  2. main() is static because at that point in the application’s lifecycle, the application stack is procedural in nature due to there being no objects yet instantiated.It’s a clean slate. Your application is running at this point, even without any objects being declared (remember, there’s procedural AND OO coding patterns). You, as the developer, turn the application into an object-oriented solution by creating instances of your objects and depending upon the code compiled within.
  3. static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.
  4. The method is static because there would be ambiguity: which constructor should be called? Especially if your class looks like this:
    public class JavaClass
    {
      protected JavaClass(int x){}
    
      public void main(String[] args)
      {
      }
    }
    

    If the JVM call new JavaClass(int)? What should it pass for x?

What would be the Output

public class Test 
{
    static
    {
      System.out.println("Hi static 1");        
    }

    public static void main(String[] args) 
    {
        System.out.println("Hi main");
    }

    static
    {
        System.out.println("Hi static 2");        
    }
}

Output

Hi static 1
Hi static 2
Hi main

What happens when you run a class without main() method
Say, file Demo.java contains source code as

public class Demo{ 

}

when this code is compiled as it’s compile successfully as

$javac Demo.java

but when it’s executed as

$java Demo

then it shows an exceptional error

Main method not found in class Demo, please define the main method as: public static void main(String[] args)

so compiler is not responsible to check whether main() method is present or not. JVM is responsible for it. JVM check for main() method with prototype as

public static void main(Strings[] args)

Why JVM search main() method? Is it possible to change main() method into any other method main_hero()?
JVM is instructed to find main() method from inside JVM. Yes, it’s possible to change main() method into main_hero() method but inside JVM you must have to instruct to search main_hero() method.

Why public?
call from anywhere public is used.

Why void?
JVM is going to call main() method but what can JVM do with return value if main() method return.

Can we declare main() method as private or protected or with no access modifier?
No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.

Can We Declare main() Method As non-static?
No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.

Most of the Errors incase of change of main() method are runtime errors

Suppose foo is a variable of type long. The following operation is not an atomic operation:

foo = 65465498L;

Indeed, the variable is written using two separate operations: one that writes the first 32 bits, and a second one which writes the last 32 bits. That means that another thread might read the value of foo, and see the intermediate state.

Making the operation atomic consists in using synchronization mechanisms in order to make sure that the operation is seen, from any other thread, as a single, atomic (i.e. not splittable in parts), operation. That means that any other thread, once the operation is made atomic, will either see the value of foo before the assignment, or after the assignment. But never the intermediate value.

A simple way of doing this is to make the variable volatile:

private volatile long foo;

Or to synchronize every access to the variable:

public synchronized void setFoo(long value) {
    this.foo = value;
}

public synchronized long getFoo() {
    return this.foo;
}
// no other use of foo outside of these two methods, unless also synchronized

Stack

  1. The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data.
  2. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order;
  3. the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.

Heap

  1. The heap is memory set aside for dynamic allocation. Unlike the stack, there’s no enforced pattern to the allocation and deallocation of blocks from the heap;
  2. you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time
  3. There are many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack, while there’s typically only one heap for the application

Which is faster – the stack or the heap? And why?
The stack is much faster than the heap. This is because of the way that memory is allocated on the stack. Allocating memory on the stack is as simple as moving the stack pointer up.

Where are the stack and heap stored?
Both stored in the computer’s RAM (Random Access Memory).

What is stored in stack and heap ?
Stack – Primitives, Calls to methods are stored on the stack
Heap – Class objects including method code and static fields in the heap space.

What determines the size of each of them?
The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed.

How do the stack and heap work in multithreading?
In a multi-threaded application, each thread will have its own stack. But, all the different threads will share the heap. Because the different threads share the heap in a multi-threaded application, this also means that there has to be some coordination between the threads so that they don’t try to access and manipulate the same piece(s) of memory in the heap at the same time.


How long does memory on the stack last versus memory on the heap?

Once a function call runs to completion, any data on the stack created specifically for that function call will automatically be deleted. Any data on the heap will remain there until it’s manually deleted by the programmer.

Can the stack grow in size? Can the heap grow in size?
The stack is set to a fixed size, and can not grow past it’s fixed size. So, if there is not enough room on the stack to handle the memory being assigned to it, a stack overflow occurs. This often happens when a lot of nested functions are being called, or if there is an infinite recursive call.

If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap by the operating system. This is one of the big differences between the heap and the stack.

Q1.Explain Exception Hierachy?

Q2.What are important methods of Java Exception Class?
String getMessage() – This method returns the message String of Throwable and the message can be provided while creating the exception by it’s constructor.
String getLocalizedMessage() – This method is provided so that subclasses can override it to provide locale-specific message to the calling program. Throwable class implementation of this method simply uses getMessage() method to return the exception message.
synchronized Throwable getCause() – This method returns the cause of the exception or null id the cause is unknown.
void printStackTrace() – This method prints the stack trace information to the standard error stream, this method is overloaded and we can pass PrintStream or PrintWriter as an argument to write the stack trace information to the file or stream.

Q3.What is multi-catch block?(or) How can I catch multiple Java exceptions in the same catch clause?
We can catch multiple exceptions in a single catch block. catching all the exceptions in a single try block, you will notice that catch block code looks very ugly and mostly consists of redundant code to log the error, keeping this in mind Java 7 one of the features was multi-catch block.

catch(IOException | SQLException | Exception ex){
     logger.error(ex);
     throw new MyException(ex.getMessage());
}

Q4.What is try-with-resources does?
try-with-resources was introduced because some resources used in Java (like SQL connections or streams) being difficult to be handled properly; as an example, in java 6 to handle a InputStream properly you had to do something like

InputStream stream = new MyInputStream(...);
try {
    // ... use stream
} catch(IOException e) {
   // handle exception
} finally {
    try {
        if(stream != null) {
            stream.close();
        }
    } catch(IOException e) {
        // handle yet another possible exception
    }
}

Using try-with-resources we can do the same thing as below and close() is automatically called, if it throws an IOException, it will be supressed

try (InputStream stream = new MyInputStream(...)){
    // ... use stream
} catch(IOException e) {
   // handle exception
}

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Q5.Advantages of try-with-resources does?

  1. Readable code and easy to write and Number of lines of code is reduced as no need for finally block
  2. We can open multiple resources in try-with-resources statement separated by a semicolon. For example, we can write following code.
  3. When multiple resources are opened in try-with-resources, it closes them in the reverse order to avoid any dependency issue. You can extend my resource program to prove that.
  4. Automatic resource management.

Q6.What is Exception Masking?
When code in a try block throws an exception, and the close method in the finally also throws an exception, the exception thrown by the try block gets lost and the exception thrown in the finally gets propagated. This is usually unfortunate, since the exception thrown on close is something unhelpful while the useful exception is the informative one. Using try-with-resources to close your resources will prevent from exception-masking from taking place.With try-with-resources, if the try block throws an exception and the close method also throws an exception, then the exception from the close block gets tacked on to the original exception. the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.On the other hand if your code completes normally but the resource you’re using throws an exception on close, that exception (which would get suppressed if the code in the try block threw anything) gets thrown.

Q7.How Autocloseable works?
A try-with-resources statement makes sure that all declared resources will be closed at the end of the statement.To close a resource its should implement the AutoCloseable interface Object of classes that implements autoCloseable are considered for deallocation in try-with-resources.JVM will call close() automatically for you

public class MyResource implements AutoCloseable 
{
    public void close() throws Exception 
    {
        System.out.println("Closing!");
    }
}

try(MyResource res = new MyResource()) 
{
    //use res here
}

Q8.Difference between Closeble and Autocloseable works?
Closeable.close() throws IOException. A lot of close() methods that could benefit of try-with-resources throw other checked exceptions (eg java.sql.Connection.close() so AutoCloseable.close() throws Exception. Changing the existing Closeable contract would break all existing applications/library relying on the contract that close() only throws IOException and not all (checked) exceptions.

Q9.How to get Suppressed Exceptions?
java 7 functionality has been provided to retrieve suppressed Exceptions. You can call public final java.lang.Throwable[] getSuppressed() function on the catched throwable object to view the suppressed Exceptions.

public final void addSuppressed(Throwable exception)
Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

public final Throwable[] getSuppressed()
Returns an array containing all of the exceptions that were suppressed, typically by the try-with-resources statement, in order to deliver this exception.

public static  T suppress(final T t, final Throwable suppressed) 
public static Throwable [] getSuppressed(final Throwable t) {

addSuppressed Example

public static void memberFunction() throws Exception
    {
        Throwable th = null;
        DirtyResource resource= new DirtyResource();
        try
        {
              resource.accessResource();
        }
        catch(Exception e)
        {
            th = e;
        }
        finally
        {
            try
            {
                resource.close();
            }
            catch(Exception e)
            {
                if(th != null)
                {
                    e.addSuppressed(th); //Add to primary exception
                    throw e;
                }
            }
        }
    }

getSuppressed Example

 public static void main(String[] arguments) throws Exception
   {
      try
      {
          memberFunction();
      }
      catch(Exception ex)
      {
          err.println("Exception encountered: " + ex.toString());
          final Throwable[] suppressedExceptions = ex.getSuppressed();
          final int numSuppressed = suppressedExceptions.length;
          if (numSuppressed > 0)
          {
              err.println("tThere are " + numSuppressed + " suppressed exceptions:");
              for (final Throwable exception : suppressedExceptions)
              {
                  err.println("tt" + exception.toString());
              }
          }
      }
   }

Errors vs Exception
Errors should not be caught or handled.An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
i.e AnnotationFormatError, AssertionError, LinkageError, VirtualMachineError

Example: OutOfMemoryError – Not much you can do as your program can no longer run.

Exceptions are often recoverable and even when not, they generally just mean an attempted operation failed, but your program can still carry on.

Example: IllegalArgumentException – Passed invalid data to a method so that method call failed, but it does not affect future operations.

What is difference between Checked Exception and Unchecked Exception?
Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

Can finally block be used without catch?
Yes, by try block. finally must be followed by either try or catch

Is there any case when finally will not be executed?
finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

Can subclass overriding method declare an exception if parent class method doesn’t throw an exception ?
Yes but only unchecked exception not checked.

What is exception propagation ?
Forwarding the exception object to the invoking method is known as exception propagation. By default Unchecked Exceptions are forwarded in calling chain (propagated).

 
 Method1()  //Exception Occured
 Method2()
 Method3()
 main()

In the above example exception occurs in Method1() method where it is not handled,so it is propagated to previous Method2() method where it is not handled, again it is propagated to Method3() before main method where exception is handled.