diff --git a/README.md b/README.md
index 2d05a5c..380fef3 100644
--- a/README.md
+++ b/README.md
@@ -1,348 +1,371 @@
-## Java, J2EE, JSP, Servlet, Hibernate Interview Questions
+# Java Basics
-*Click
if you like the project. Pull Request are highly appreciated.*
+> *Click ★ if you like the project. Your contributions are heartily ♡ welcome.*
+
-### Table of Contents
+## Related Topics
-* *[Java 8 Interview Questions](java8-questions.md)*
-* *[Multithreading Interview Questions](multithreading-questions.md)*
-* *[Collections Interview Questions](collections-questions.md)*
-* *[Hibernate Interview Questions](hibernate-questions.md)*
-* *[JDBC Interview Questions](JDBC-questions.md)*
+* *[Multithreading](multithreading-questions.md)*
+* *[Collections](collections-questions.md)*
+* *[Java Database Connectivity (JDBC)](JDBC-questions.md)*
* *[Java Programs](java-programs.md)*
* *[Java String Methods](java-string-methods.md)*
-* *[JSP Interview Questions](jsp-questions.md)*
-* *[Servlets Interview Questions](servlets-questions.md)*
-* *[Java Design Pattern Questions](java-design-pattern-questions.md)*
+* *[Jakarta Server Pages (JSP)](jsp-questions.md)*
+* *[Servlets](servlets-questions.md)*
* *[Java Multiple Choice Questions](java-multiple-choice-questions-answers.md)*
+* *[Java Design Pattern](https://github.com/learning-zone/java-design-patterns)*
+* *[Hibernate](https://github.com/learning-zone/hibernate-basics)*
+* *[Spring Framework Basics](https://github.com/learning-zone/spring-basics)*
-## Q. ***What are the types of Exceptions? Explain the hierarchy of Java Exception classes?***
-Exception is an error event that can happen during the execution of a program and disrupts its normal flow.
+## Table of Contents
+
+* [Introduction](#-1-introduction)
+* [Java Architecture](#-2-java-architecture)
+* [Java Data Types](#-3-java-data-types)
+* [Java Methods](#-4-java-methods)
+* [Java Functional programming](#-5-java-functional-programming)
+* [Java Lambda expressions](#-6-java-lambda-expressions)
+* [Java Classes](#-7-java-classes)
+* [Java Constructors](#-8-java-constructors)
+* [Java Array](#-9-java-array)
+* [Java Strings](#-10-java-strings)
+* [Java Reflection](#-11-java-reflection)
+* [Java Streams](#-12-java-streams)
+* [Java Regular Expressions](#-13-java-regular-expressions)
+* [Java File Handling](#-14-java-file-handling)
+* [Java Exceptions](#-15-java-exceptions)
+* [Java Inheritance](#-16-java-inheritance)
+* [Java Method Overriding](#-17-java-method-overriding)
+* [Java Polymorphism](#-18-java-polymorphism)
+* [Java Abstraction](#-19-java-abstraction)
+* [Java Interfaces](#-20-java-interfaces)
+* [Java Encapsulation](#-21-java-encapsulation)
+* [Java Generics](#-22-java-generics)
+* [Miscellaneous](#-23-miscellaneous)
-**Types of Java Exceptions**
+
-**1. Checked Exception**: The classes which directly inherit `Throwable class` except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
-**2. Unchecked Exception**: The classes which inherit `RuntimeException` are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
-**3. Error**: Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
+## # 1. INTRODUCTION
-**Hierarchy of Java Exception classes**
-The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error.
+
-
+## Q. What are the important features of Java 8 release?
+
+* Interface methods by default;
+* Lambda expressions;
+* Functional interfaces;
+* References to methods and constructors;
+* Repeatable annotations
+* Annotations on data types;
+* Reflection for method parameters;
+* Stream API for working with collections;
+* Parallel sorting of arrays;
+* New API for working with dates and times;
+* New JavaScript Nashorn Engine ;
+* Added several new classes for thread safe operation;
+* Added a new API for `Calendar`and `Locale`;
+* Added support for Unicode 6.2.0 ;
+* Added a standard class for working with Base64 ;
+* Added support for unsigned arithmetic;
+* Improved constructor `java.lang.String(byte[], *)` and method performance `java.lang.String.getBytes()`;
+* A new implementation `AccessController.doPrivileged` that allows you to set a subset of privileges without having to check all * other access levels;
+* Password-based algorithms have become more robust;
+* Added support for SSL / TLS Server Name Indication (NSI) in JSSE Server ;
+* Improved keystore (KeyStore);
+* Added SHA-224 algorithm;
+* Removed JDBC Bridge - ODBC;
+* PermGen is removed , the method for storing meta-data of classes is changed;
+* Ability to create profiles for the Java SE platform, which include not the entire platform, but some part of it;
+* Tools
+ * Added utility `jjs` for using JavaScript Nashorn;
+ * The command `java` can run JavaFX applications;
+ * Added utility `jdeps` for analyzing .class files.
+
+In Java, there are three different ways for reading input from the user in the command line environment ( console ).
+**1. Using Buffered Reader Class:**
-**Aggregation**: We call aggregation those relationships whose **objects have an independent lifecycle, but there is ownership**, and child objects cannot belong to another parent object.
+This method is used by wrapping the System.in ( standard input stream ) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line.
-Example: Since Organization has Person as employees, the relationship between them is Aggregation. Here is how they look like in terms of Java classes
```java
-public class Organization {
- private List employees;
-}
+/**
+ * Buffered Reader Class
+ */
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
-public class Person {
- private String name;
+public class Test {
+ public static void main(String[] args) throws IOException {
+ // Enter data using BufferReader
+ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+
+ // Reading data using readLine
+ String name = reader.readLine();
+
+ // Printing the read line
+ System.out.println(name);
+ }
}
```
-**Composition**: We use the term composition to refer to relationships whose objects **don’t have an independent lifecycle**, and if the parent object is deleted, all child objects will also be deleted.
+**2. Using Scanner Class:**
+
+The main purpose of the Scanner class is to parse primitive types and strings using regular expressions, however it is also can be used to read input from the user in the command line.
-Example: Since Engine is-part-of Car, the relationship between them is Composition. Here is how they are implemented between Java classes.
```java
-public class Car {
- //final will make sure engine is initialized
- private final Engine engine;
-
- public Car(){
- engine = new Engine();
+/**
+ * Scanner Class
+ */
+import java.util.Scanner;
+
+class GetInputFromUser {
+ public static void main(String args[]) {
+ // Using Scanner for Getting Input from User
+ Scanner in = new Scanner(System.in);
+
+ String s = in.nextLine();
+ System.out.println("You entered string " + s);
+
+ int a = in.nextInt();
+ System.out.println("You entered integer " + a);
+
+ float b = in.nextFloat();
+ System.out.println("You entered float " + b);
}
}
+```
-class Engine {
- private String type;
+**3. Using Console Class:**
+
+It has been becoming a preferred way for reading user\'s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used ( like System.out.printf() ).
+
+```java
+/**
+ * Console Class
+ */
+public class Sample {
+ public static void main(String[] args) {
+ // Using Console to input data from user
+ String name = System.console().readLine();
+ System.out.println(name);
+ }
}
```
-| Aggregation | Composition |
|---|---|
| Aggregation is a weak Association. | Composition is a strong Association. |
| Class can exist independently without owner. | Class can not meaningfully exist without owner. |
| Have their own Life Time. | Life Time depends on the Owner. |
| A uses B. | A owns B. |
| Child is not owned by 1 owner. | Child can have only 1 owner. |
| Has-A relationship. A has B. | Part-Of relationship. B is part of A. |
| Denoted by a empty diamond in UML. | Denoted by a filled diamond in UML. |
| We do not use "final" keyword for Aggregation. | "final" keyword is used to represent Composition. |
| Examples: - Car has a Driver. - A Human uses Clothes. - A Company is an aggregation of People. - A Text Editor uses a File. - Mobile has a SIM Card. | Examples: - Engine is a part of Car. - A Human owns the Heart. - A Company is a composition of Accounts. - A Text Editor owns a Buffer. - IMEI Number is a part of a Mobile. |
-
-
+
+
| Abstraction | Encapsulation |
|---|---|
| Abstraction is a process of hiding the implementation details and showing only functionality to the user. | -Encapsulation is a process of wrapping code and data together into a single unit |
| Abstraction lets you focus on what the object does instead of how it does it. | -Encapsulation provides you the control over the data and keeping it safe from outside misuse. |
| Abstraction solves the problem in the Design Level. | -Encapsulation solves the problem in the Implementation Level. |
| Abstraction is implemented by using Interfaces and Abstract Classes. | -Encapsulation is implemented by using Access Modifiers (private, default, protected, public) |
| Abstraction means hiding implementation complexities by using interfaces and abstract class. | -Encapsulation means hiding data by using setters and getters. |
| Method | Description |
|---|---|
| public final Class getClass() | returns the Class class object of this object. The Class class can further be used to get the metadata of this class. |
| public int hashCode() | returns the hashcode number for this object. |
| public boolean equals(Object obj) | compares the given object to this object. |
| protected Object clone() throws CloneNotSupportedException | creates and returns the exact copy (clone) of this object. |
| public String toString() | returns the string representation of this object. |
| public final void notify() | wakes up single thread, waiting on this object\'s monitor. |
| public final void notifyAll() | wakes up all the threads, waiting on this object\'s monitor. |
| public final void wait(long timeout)throws InterruptedException | causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait(long timeout,int nanos)throws InterruptedException | causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait()throws InterruptedException | causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). |
| protected void finalize()throws Throwable | is invoked by the garbage collector before object is being garbage collected. |
+
+
+
+
| Aggregation | Composition |
|---|---|
| Aggregation is a weak Association. | Composition is a strong Association. |
| Class can exist independently without owner. | Class can not meaningfully exist without owner. |
| Have their own Life Time. | Life Time depends on the Owner. |
| A uses B. | A owns B. |
| Child is not owned by 1 owner. | Child can have only 1 owner. |
| Has-A relationship. A has B. | Part-Of relationship. B is part of A. |
| Denoted by a empty diamond in UML. | Denoted by a filled diamond in UML. |
| We do not use "final" keyword for Aggregation. | "final" keyword is used to represent Composition. |
| Examples: - Car has a Driver. - A Human uses Clothes. - A Company is an aggregation of People. - A Text Editor uses a File. - Mobile has a SIM Card. | Examples: - Engine is a part of Car. - A Human owns the Heart. - A Company is a composition of Accounts. - A Text Editor owns a Buffer. - IMEI Number is a part of a Mobile. |
| Abstraction | Encapsulation |
|---|---|
| Abstraction is a process of hiding the implementation details and showing only functionality to the user. | +Encapsulation is a process of wrapping code and data together into a single unit |
| Abstraction lets you focus on what the object does instead of how it does it. | +Encapsulation provides you the control over the data and keeping it safe from outside misuse. |
| Abstraction solves the problem in the Design Level. | +Encapsulation solves the problem in the Implementation Level. |
| Abstraction is implemented by using Interfaces and Abstract Classes. | +Encapsulation is implemented by using Access Modifiers (private, default, protected, public) |
| Abstraction means hiding implementation complexities by using interfaces and abstract class. | +Encapsulation means hiding data by using setters and getters. |
System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen. If there is no memory space for creating a new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space
+* java.util.AbstractMap
+* java.util.Object
+* java.util.Map
-## Q. ***How to create marker interface?***
-An interface with no methods is known as marker or tagged interface. It provides some useful information to JVM/compiler so that JVM/compiler performs some special operations on it. It is used for better readability of code. Example: **Serializable, Clonnable** etc.
+## Q. What is a Memory Leak?
-Syntax:
-```java
-public interface Interface_Name {
+The standard definition of a memory leak is a scenario that occurs when **objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory** – because they\'re still being referenced. As a result, the application consumes more and more resources – which eventually leads to a fatal OutOfMemoryError.
+
+Some tools that do memory management to identifies useless objects or memeory leaks like:
+
+* HP OpenView
+* HP JMETER
+* JProbe
+* IBM Tivoli
+
+**Example:**
-}
-```
-Example:
```java
/**
-* Java program to illustrate Maker Interface
-*
-**/
-interface Marker { }
+ * Memory Leaks
+ */
+import java.util.Vector;
-class A implements Marker {
- //do some task
+public class MemoryLeaksExample {
+ public static void main(String[] args) {
+ Vector v = new Vector(214444);
+ Vector v1 = new Vector(214744444);
+ Vector v2 = new Vector(214444);
+ System.out.println("Memory Leaks Example");
+ }
}
+```
-class Main {
- public static void main(String[] args) {
- A obj = new A();
- if (obj instanceOf Marker){
- // do some task
- }
- }
-}
+Output
+
+```java
+Exception in thread "main" java.lang.OutOfMemoryError: Java heap space exceed
```
+
+**Types of Memory Leaks in Java:**
+
+* Memory Leak through static Fields
+* Unclosed Resources/connections
+* Adding Objects With no `hashCode()` and `equals()` Into a HashSet
+* Inner Classes that Reference Outer Classes
+* Through `finalize()` Methods
+* Calling `String.intern()` on Long String
+
-## Q. ***How serialization works in java?***
-Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
+## Q. The difference between Serial and Parallel Garbage Collector?
-Example:
-```java
-/**
-* Serialization and Deserialization
-* example of a Java object
-*
-**/
-import java.io.*;
-
-class Employee implements Serializable {
-private static final long serialversionUID =
- 129348938L;
- transient int a;
- static int b;
- String name;
- int age;
-
- // Default constructor
- public Employee(String name, int age, int a, int b) {
- this.name = name;
- this.age = age;
- this.a = a;
- this.b = b;
- }
-}
-
-public class SerialExample {
+**1. Serial Garbage Collector:**
+
+Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs.
+
+Turn on the `-XX:+UseSerialGC` JVM argument to use the serial garbage collector.
+
+**2. Parallel Garbage Collector:**
+
+Parallel garbage collector is also called as throughput collector. It is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection.
- public static void printdata(Employee object1) {
- System.out.println("name = " + object1.name);
- System.out.println("age = " + object1.age);
- System.out.println("a = " + object1.a);
- System.out.println("b = " + object1.b);
- }
-
- public static void main(String[] args) {
- Employee object = new Employee("ab", 20, 2, 1000);
- String filename = "shubham.txt";
-
- // Serialization
- try {
- // Saving of object in a file
- FileOutputStream file = new FileOutputStream(filename);
- ObjectOutputStream out = new ObjectOutputStream(file);
-
- // Method for serialization of object
- out.writeObject(object);
-
- out.close();
- file.close();
-
- System.out.println("Object has been serialized\n"
- + "Data before Deserialization.");
- printdata(object);
- // value of static variable changed
- object.b = 2000;
- }
- catch (IOException ex) {
- System.out.println("IOException is caught");
- }
-
- object = null;
-
- // Deserialization
- try {
- // Reading the object from a file
- FileInputStream file = new FileInputStream(filename);
- ObjectInputStream in = new ObjectInputStream(file);
-
- // Method for deserialization of object
- object = (Employee)in.readObject();
-
- in.close();
- file.close();
- System.out.println("Object has been deserialized\n"
- + "Data after Deserialization.");
- printdata(object);
- System.out.println("z = " + object1.z);
- }
- catch (IOException ex) {
- System.out.println("IOException is caught");
- }
- catch (ClassNotFoundException ex) {
- System.out.println("ClassNotFoundException is caught");
- }
- }
-}
-```
-## Q. ***What are the various ways to load a class in Java?***
+## Q. What is difference between WeakReference and SoftReference in Java?
+
+**1. Weak References:**
-**a). Creating a reference**:
-```java
-SomeClass someInstance = null;
-```
+Weak Reference Objects are not the default type/class of Reference Object and they should be explicitly specified while using them.
-**b). Using Class.forName(String)**:
```java
- Class.forName("SomeClass");
-```
+/**
+ * Weak Reference
+ */
+import java.lang.ref.WeakReference;
-**c). Using SystemClassLoader()**:
-```java
-ClassLoader.getSystemClassLoader().loadClass("SomeClass");
+class MainClass {
+ public void message() {
+ System.out.println("Weak References Example");
+ }
+}
+
+public class Example {
+ public static void main(String[] args) {
+ // Strong Reference
+ MainClass g = new MainClass();
+ g.message();
+
+ // Creating Weak Reference to MainClass-type object to which 'g'
+ // is also pointing.
+ WeakReference| Method | Description |
|---|---|
| public final Class getClass() | returns the Class class object of this object. The Class class can further be used to get the metadata of this class. |
| public int hashCode() | returns the hashcode number for this object. |
| public boolean equals(Object obj) | compares the given object to this object. |
| protected Object clone() throws CloneNotSupportedException | creates and returns the exact copy (clone) of this object. |
| public String toString() | returns the string representation of this object. |
| public final void notify() | wakes up single thread, waiting on this object's monitor. |
| public final void notifyAll() | wakes up all the threads, waiting on this object's monitor. |
| public final void wait(long timeout)throws InterruptedException | causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait(long timeout,int nanos)throws InterruptedException | causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method). |
| public final void wait()throws InterruptedException | causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). |
| protected void finalize()throws Throwable | is invoked by the garbage collector before object is being garbage collected. |
+
+