What is the OOPs concept?

OOPs concept is to build the class by combining data and methods. There are mainly four concepts:

  • Abstraction
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction: It is the concept of either creating a placeholder or hiding unnecessary/implementation information. This can be achieved by creating an idea of functionality without concrete implementation or by creating private attributes and methods that are unnecessary and should be hidden from other objects.
  • Encapsulation: It is the concept of bundling or wrapping attributes and methods in a single entity as a class. This helps restrict access to information, reduce complexity, and enhance safety.
  • Inheritance: It is the concept of code modularity by allowing reuse and building “Is a “ relationships between classes. The child class inherited all attributes and functions of the parent (super) class.
  • Polymorphism: This means many forms. This concept allows for the use of a single functionality with different underlying implementations. It can be run-time (overriding) or compilation time (overloading).

What is an association vs aggregation, vs composition?

  • Association: can be defined as a relationship between entities based on how they interact or depend on each other.
  • Aggregation: a relationship where the entity can exist independently of the associated entity. For example, a person can be associated with multiple banks, and a bank can have various people’s accounts, but no one can own the other.
  • Composition: a relationship where the entity can’t exist independently without an associated entity. Example: book (parent) and Chapter (child). Book has chapters.

How ‘IS-A ‘ and “HAs-A relationships in OOPs Java?

OOPs Code is based on “is a” and “has-a” relationships.
“Is a “ relationship is “inheritance. Parent and child relationship
“Has a “ relationship is “composition.”. Makes OOP code modular, allowing the use of instance variables of other classes (using other class references as attributes/property of the class)

What makes Java platform independent?

Java programs are compiled into bytecode that can be run on any operating system. It uses the Java virtual machine to run the code.

What is the difference between JDK and JRE?

JDK is a Java Development Kit. It is required for the development purpose where JRE is Java runtime environment create environment to execute the Java programs.

What are hashcode and equals methods?

hashCode() and equals() compare the equality of the objects. These methods are implicit define in class. The default implementation of “equals” compares object references of the two objects. hashCode() returns the integer hash value of the object. If two objects are equal according to equals() method then the hashcode of these objects must return the same integer. Java programs give the option to override these methods. Below are the principles that should be followed:

The equals() method must be:

  • reflexive: an object must equal itself
  • symmetric: x.equals(y) must return the same result as y.equals(x)
  • transitive: if x.equals(y) and y.equals(z), then also x.equals(z)
  • consistent: value of equals() should change only if a property that is contained in equals() changes (no randomness allowed)

What is the marker interface and what is used?

The marker interface is the empty interface in other words no constant or method is defined. It provides run time indication to JVM that the class has additional functionality or purpose. Example: Serializable, Remote and clonable.
Annotation is an alternative way to achieve the same.

What is the difference between Interface and @interface?

interface: defines a contract between the class and implementer class. This is enforced at the compilation time

@interface: defines the contract for an annotation. An annotation type declaration specifies a new annotation type, a special kind of interface type.
https://docs.oracle.com/javase/specs/jls/se13/html/jls-9.html#jls-9.6

What is Object Cloning?

Object cloning refers to creating an exact copy of an object using a Cloneable interface. There are two types of cloning: shallow and deep cloning.

  • Shallow cloning is the default implementation. It uses reflection to create a copy of the instance. Any change made in the original copy will reflect on copy and vice-versa.
  • Deep cloning is creating a new object for the referenced object by overriding the clone() method

What is a singleton in Java? And How to implement a singleton class?

Singleton is a class that can have only one instance of the class per JVM. The idea is to restrict the limit of the number of object creations to only one. This can be done by making a constructor private and public static method with return type object of singleton class. Lazy initialization can also be used to write this method.

Is String mutable or immutable and why?

A string is immutable as it cannot be modified once it is created. String objects are cached in the String pool. String pools is a collection of strings in Java’s Heap memory. When new string object created, JVM first checks for the presence of the object in the pool. If available, the same object reference is shared with the variable; otherwise, a new object is created.

What is a final key keyword in Java?

Final indicates that a variable, method, or class cannot be modified or extended.

What are the types of memory locations in Java?

There are two main types of memory in Java – stack memory and heap memory.
The stack memory contains local variables and method-specific values. Java uses stack memory for thread execution. In an application, each thread has its own stack. This is fixed that is determined by the JVM at runtime. It is based on LIFO order,
heap memory: Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. We can define the initial heap size using the -Xms parameter and the maximum heap size using the -Xmx parameter

further divided into several generations.

  • Young generation. It is the place that stores new objects
  • Old generation
  • PermGen: Starting with Java 8, PermGen was replaced with Metaspace. PermGen always had a fixed maximum size. Metaspace, by default, automatically increases its size depending on the underlying OS. The Metaspace is a part of Native Memory and not JVM Memory. It has efficient garbage collection.

Native memory is the memory used by the JVM as it runs on the OS

What is garbage collection in Java?

It has been a long journey for gc from JDK 8 to JDK 18.
There are three metrics used for measuring performance.

  • Throughput represents the amount of work that can be done in a given time unit.
  • Latency gives an indication of how long a single operation of the application takes
  • Memory footprint in the context of a GC means how much extra memory beyond the application’s Java heap memory usage the GC needs for proper operation.
  1. The Parallel GC focuses on throughput by trying to get work done as quickly as possible with minimal regard to latency ( stop the world/ ) and generational collection It is the default collector for JDK 8 and earlier.
  2. Garbage First (G1) focuses on balanced performance. It tries to balance throughput and latency concerns through generational collection, It runs concurrent threads, which decreases maximum pause times significantly. The G1 GC has been the default collector since JDK 9.
  3. Z Garbage Collector (ZGC) focuses on latency at the cost of throughput. It tires to do all garbage collection work without noticeable pauses. It was introduced in JDK 15.
  4. Shenandoah GC focuses on latency as ZGC. It was introduced in JDK 12
  5. Serial GC focuses on footprint and startup time. It slower version of the Parallel GC, as it uses only a single thread for all work within Stop The World (pauses) along with generation.

What is the time and space complexity of collections?

https://www.bigocheatsheet.com/

How does HashMap work?

HaspMap is collection to store key and value. HashMap works on hashing principle. Hashing is a process of converting an object into integer form by using hashCode().. Node consists of :

  • int hash value
  • K key
  • V value
  • Node next


When put method is called, the hashcode() method of the key object and the hash function of the map can find a bucket location to store the value object, which is actually an index of the internal array of the bucket HashMap internally stores mapping in the form of Map.Entry object, which contains both key and value objects
If two different objects have the same hash, it uses a linked list to resolve the conflict. Java 8 made updates to the internal implementation. Java automatically stores data in Treemap once a certain threshold level is reached.

What is the importance of reflection in Java?

Reflection is a feature of Java that allows the program to examine or “introspect” itself and manipulate the internal properties of the program. Many modern frameworks use reflection extensively for dynamic loading of class information, dependency injection, logging, debugging, autowiring, component scan, bean creation, JPA ( entity, repository, query, and so on).