Top 50 Java Interview Questions and Answers
In this post, We are going to cover some of the Java Interview Questions
Core Java Interview Questions | Basic to Advance java interview questions | We are going to cover only some of the basics questions first then will cover questions based on different framework like spring, spring boot, etc.
- What is Java, and what is the use of Java, why we need Java language?
- Core Java is a part of Java Standard Edition (SE) where we can develop desktop based apps using the basic concepts of Java like: Classes and objects i.e. basic OOPS programming, Operators, data types, AWT and swing, threading, exception handling, overloading, overriding and collections. Core Java has the basic set of instruction through which we can create applications. It is the base for all the Java frameworks.
- Using core Java, we can build mostly general purpose desktop based applications. Advance Java covers topics like web services, JSP, servlets, dB connectivity, EJB, etc. We are going to cover all of these topics in shorts just to give you proper meaning and understanding on it.
- Read more about core java here
- Explain the main features of Java?
- Java offers various features, like OOPS, Multithreading, automatic memory management, exception handling, security see here
- Tell us the difference between a JDK, JRE, and JVM?
- JDK (Java Development Kit) is an application development kit (ADP) used for software that contains all the tools and libraries required to compile, debug and test Java apps. It includes programming tools like debuggers, compilers and javadoc that help programmers write testable Java code. Developers looking to create their own applications from scratch often turn to the JDK as it also includes JRE (Java Runtime Environment).
- JRE (Java Runtime Environment) A JRE, or the Java Runtime Environment, provides minimal resources necessary for running Java applications on computers without needing to develop them from scratch. This environment includes the JVM, core libraries and any necessary components needed by end-users who simply want to run them without developing them themselves.
- JVM (Java Virtual Machine)
- The JVM (Java Virtual Machine) is an abstract machine that serves as the runtime environment for running Java bytecode, translating from its compiler and running it on host machines. The JVM creates a layer of abstraction between Java applications and hardware/operating system interactions and keeps them platform independent.
- JDK and JRE are used by developers and end-users respectively for creating Java applications; JVM serves both applications. JDK contains JRE as part of its subset; however, JRE does not include JDK.
- JDK includes all the tools and libraries necessary for Java development, while JRE contains only those resources essential to running applications written using the language. JVM interprets bytecode generated by the compiler before executing it on a host machine.
- How do you differentiate between an Interface and an Abstract class?
- Implementation
An abstract class can contain both abstract and non-abstract methods, while interfaces only support abstract methods. An abstract class may provide default implementations for its non-abstract methods while an interface cannot provide any implementation whatsoever. - Inheritance A class can extend only one abstract class while simultaneously implementing multiple interfaces; that means it may inherit from an abstract class while also implementing any number of interfaces simultaneously.
- Accessibilite
Methods in an interface are by default public and abstract, while those in an abstract class can have any access modifier; such as public, private, protected or default. - Purpose
An abstract class serves to provide a consistent platform for its subclasses, providing common behavior or attributes across classes while an interface acts as a contract between implementers of its methods, meaning any class that implements an interface must comply with all its methods.
- Implementation
- What is the use of the ‘final’ keyword in Java? Why we need final in Java ?
- Java programming language offers various keywords with specific meaning and functionality, with one of the most frequently utilized being ‘final’ – used to declare variables, methods or classes so they cannot be overridden by another class or method. We will discuss its various applications here.
- Final Variable
A final variable is defined as any variable which can only ever be initialized once and cannot be altered afterwards; its value cannot be altered once assigned; here are some uses of final variables in Java: - Create Constants: Final variables can be used to define constants, which are values that remain the same throughout a program. By assigning one of these final variables as constant values, you can ensure its value remains constant over time.
- Enhancing Performance: Final variables can improve program performance as the Java Virtual Machine (JVM) optimizes code by replacing variables with their values and replacing them with variable finalizations.
- Avoid Bugs: Creating a final variable can help protect against bugs by making sure that its value cannot be altered accidentally.
- Final Method in Java
A final method refers to any method which cannot be overridden by subclasses; its purpose may vary. Here are some uses for final methods in Java: - To prevent changes to important methods: A final method can be used to stop any subclass from changing an important method.
- Enhance performance: To optimize program performance, using final methods can significantly boost its speed since JVM optimizes code by inlining method calls and optimizing inlining of methods into code.
- Final Class
A final class in Java cannot be subclassed, and offers several uses: - For protecting important classes: When used correctly, final classes can help stop subclasses from changing an important class.
- Enhance security: To strengthen protection, final classes can be used to restrict any subclass from accessing sensitive data or methods.
- How is Java different from other programming languages?
- What is the difference between a constructor and a method?
- How does Java handle memory management?
- What is a singleton class, and when should you use it?
- How does Java handle multi-threading?
- Can you explain the concept of inheritance in Java?
- What is an exception, and how do you handle exceptions in Java?
- What is the use of the ‘synchronized’ keyword in Java?
- Explain Collection framework in Java, and why is it used?
- Can you explain the concept of polymorphism in Java?
- What is Thread in Java, what is the use of threading?
- What is deamon thread in Java? Is main method a deamon thread?
- Difference between String and String buffer class.
- Why Strings are immutable in Java ?
- What are the different ways to create a Java Object ?
- Explain Exception hierarchy in Java.
- How to break singleton design pattern ?
- What is volatile in Java?
- What is a Thread Local class and why it is used ?
- When to use Array vs List?
- Difference between Map and Set
- What are fail-safe and fail-fast iterators?
- Why we need interfaces in Java ?
- What is an object class ?
- What is the signature of main () method, explain the args of it.
- Difference between ArrayList and LinkedList.
- What are the various ways to handle exceptions in Java?
- Difference between method overloading and method overriding.
- What is thread life cycle?
- What is the difference between yield() and sleep() ?
- Why Thread class methods are present in Object class, What is the reason behind it?
- What are different types of locks available in Java ?
- What is the use of clonable in Java?
- Explain the difference between deep copy and shallow copy.
- What is concurrent hash map and how it is different from hash map ?
- How to create immutable class in Java?
- What are Wrapper classes in Java ?
- Explain Java class loading ?
- Explain how Garbage collection works in Java?
- Can we override a static method in Java ?
- What is Dynamic method dispatch in Java ?
- Checked Vs Unchecked exceptions.
- How to take thread dump in Java ?
- Stack vs Heap in Java ?
- Difference between Hashtable and Hashmap.