Java Interview Questions
Top 165 Java Interview Questions, that you need to know to ace your next Java interview. Top curated questions from the tech community
What is JDBC?
Answer
JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java without having to concern themselves with the underlying details of a particular database.
What is JVM? Why is Java called the "Platform Independent Programming Language”?
Answer
A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode.
Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform.
A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.
What are the two types of Exceptions in Java? Which are the differences between them?
Answer
Java has two types of exceptions: checked exceptions and unchecked exceptions.
Unchecked exceptions do not need to be declared in a method or a constructor’s throws clause if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary.
On the other hand, checked exceptions must be declared in a method or a constructor’s throws clause.
What is a Servlet?
Answer
The servlet is a Java programming language class used to process client requests and generate dynamic web content.
Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol.
What is the Difference between JDK and JRE?
Answer
The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution.
The Java Development Kit (JDK) is the full-featured Software Development Kit for Java, including the JRE, the compilers, and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.
What is the difference between an Applet and a Java Application?
Answer
Applets are executed within a Java-enabled browser.
Java application is a standalone Java program that can be executed outside of a browser.
However, they both require the existence of a Java Virtual Machine (JVM). Furthermore, a Java application requires a main method with a specific signature, in order to start its execution. Java applets don’t need such a method to start their execution. Finally, Java applets typically use a restrictive security policy, while Java applications usually use more relaxed security policies.
What is a JSP Page?
Answer
A Java Server Page (JSP) is a text document that contains two types of text:
static data and
JSP elements.
Static data can be expressed in any text-based format, such as HTML or XML. JSP is a technology that mixes static content with dynamically-generated content.
What is a Java Applet?
Answer
A Java Applet is a program that can be included in an HTML page and be executed in a java enabled client browser. Applets are used for creating dynamic and interactive web applications.
What does System.gc() and Runtime.gc() methods do?
Answer
These methods can be used as a hint to the JVM, in order to start a garbage collection. However, it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.
What are Directives?
Answer
What are the different types of Directives available in JSP? Directives are instructions that are processed by the JSP engine when the page is compiled to a servlet. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries. Directives are defined between < %@ and % >.The different types of directives are shown below:
Include directive: it is used to include a file and merges the content of the file with the current page.
Page directive: it is used to define specific attributes in the JSP page, like the error page and buffer.
Taglib: it is used to declare a custom tag library that is used on the page.