Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - Date and TimeJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example
Java - Performance - Interview Questions

What are the key areas that impact the performance of Java programs?

 FAQ

A lot of factors can impact the performance of an application developed in Java programming language. At a high level following areas can be optimized to improve Java application performance.

JVM

  • JVM Optimizations
  • Garbage Collection Optimizations
  • Memory Optimizations

Java program

  • Program logic optimizations
  • Algorithm and collection usage optimizations
  • Multi-threading and synchronization optimizations.

Connectivity optimizations

  • Network connectivity
  • Database connectivity

What are the benefits and dis-advantages of garbage collector?

 FAQ

Benefits:

  • Garbage collector automatically manages the JVM memory by deleting objects that are no longer referenced and used. A Java developer can concentrate on program logic instead of worrying about object deletion and memory management.
  • The garbage collector has inbuilt efficient algorithms which determine when to run the garbage collector.

Dis-advantages:

  • There is a possibility that during garbage collection process the application performance may be impacted. In certain situations, called 'stop the world', the application process is completely stopped while the garbage collection process takes place.
  • You can indicate to the JVM to run the garbage collector, but it is not guaranteed. So as a developer you do not know when the garbage collection process happens.

What is 'Stop The World'?

 FAQ

During garbage collection process, the JVM may stop all the application process threads from execution. This freezing of application processes is termed as 'Stop The World'

What do you understand by generational structure of Java heap?

 FAQ

Java heap is structured into following sections, also called as generations.

New Generation - All new objects created by a Java program are put into the new generation section of the heap. Garbage collection runs on the new generation and removes all short-lived objects.

New generation section is further split into two sections. Eden space and Survivor space.

  • Eden Space - All new objects are put into eden space. When the eden space fills up, garbage collection runs and removes all objects that have no references. All objects that still have references are promoted to the survivor space.
  • Survivor space - Every garbage collection run in the new generation section, will remove the objects from Survivor space if the objects no longer is referenced. If the object is still referenced, it will increment the age of that object. After the increment reaches a certain number, usually starting at 15 depending on the JVM implementation, the object will be promoted to the old generation section.

Old Generation - Objects that survive the survivor section of the new generation section are promoted to the old generation section. The old generation section is much larger than the new generation. A separate garbage collection process, also called as FullGC, happens in the Old generation section.

PermGen - JVM uses PermGen to store the meta-data about classes.

What are the different garbage collection algorithms? Which algorithm is better?

 FAQ

Following are some of the algorithms available for garbage collection

Serial GC - Designed for single CPU machines. Stops all application processes during garbage collection. Goes through all objects, marks objects for garbage collection, removes them.

Parallel GC - Similar to Serial, except used multiple threads for garbage collection.

Concurrent Mark Sweep - Concurrent Mark Sweep does most of the garbage collection concurrently with the application processes. Hence, the amount of time that all application process are completely stopped are greatly reduced.

G1GC (Garbage first garbage collector) - G1GC is a concurrent parallel connector, that does not have the same fragmentation problems as the CMS.

Can you force the JVM to perform garbage collection from a Java program? How?

 FAQ

You cannot force the JVM to perform garbage collection. you can only suggest or indicate to the JVM to perform garbage collection using 'System.gc()'.

What does the finalize() method do?

 FAQ

The finalize() method is called by the garbage collector on an object before it releases the object from memory. The garbage collector releases an object from memory when it determines that there are no more references to the object.

We cannot control when garbage control happens and should not depend on the finalize() method to perform clean up activities.

What are the common flags that you can use to optimize the JVM and garbage collector?

 FAQ

Following are some common flags you would set to tune JVM

  • -Xms512m - Sets the initial heap size to 512m
  • -Xmx1024m - Sets the maximum heap size to 1024m
  • -XX:PermSize64m - Set initial PermGen Size.
  • -XX:MaxPermSize128m - Set the maximum PermGen Size.
  • -XX:NewSize -
  • XX:MaxNewSize -
  • XX:NewRatio -
  • XX:SurvivorRatio -

Flags for setting the garbage collection algorithms.

  • -XX:UseParallelGC - Enable Parallel GC algorithm
  • -XX:UseParallelOldGC - Enable Parallel GC algorithms for full collections
  • -XX:UseConcMarkGC - Enable concurrent mark sweep algorithm

What are memory leaks? How do you diagnose memory leaks?

 FAQ

Memory leaks in a Java program happen when references to objects are not released. This fills up the heap space and new objects cannot be allocated in the heap. This eventually crashes the program with an OutOfMemoryError error.

The OutOfMemoryError contains detailed information that helps us to determine the root cause for the memory leak. Following are some causes for the OutOfMemoryError.

1. Resources cannot be allocated for an object in the heap - This can be because the heap size is lower than what the program requires or it could be a memory leak.
2. The PermGen space where methods and objects are stored is full.
3. The program tries to create an array greater than the heap size.

Sometimes the application crashes without the OutOfMemoryError. In such cases we can take heap dumps to further analyze the problem.

 
Important Keywords to Remember

Garbage Collector
Stop the World
Generational Structure Of Heap
New Generation
Eden Space
Survivor Space
Old Generation
PermGen
Garbage collection algorithms
Serial GC
Parallel GC
Concurrent Mark Sweep
G1GC
System.gc()
finalize()
Java Interview Guide

$49.95

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$49.95

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Ad
EARN SIDE INCOME

Earn income using your skills and hobbies that you are passioniate about.

Earn Side Income with Photos
Earn Side Income with Writing
Ad
FINANCIAL INDEPENDENCE

Transition out of you full-time job by creating businesses that you are passioniate about.