Java Interview Questions

JavaCodeDepot list of Java interview questions that you may be asked to answer during an interview or see on a Java certification test. If you haven’t interviewed for a Java position in a while, it’s best to go over the questions and answers so you feel confident and look like a potential Java developer. It’s good to be prepared in advance to have the best chance for success.


Question

How do you make a Java Class Immutable?

Answer

Declare the class as final. Declare all variables as private and final. Take a look at the Create Immutable Java Class sample code.


Question

What is the Java Classloader?

Answer

The Java Classloader is a part of the Java Runtime Environment (JRE) that dynamically loads Java Classes into the Java Virtual Machine (JVM). Typically classes are only loaded on demand.


Question

When the JVM is started, what three class loaders are used?

Answer

  • Bootstrap class loader
  • Extensions class loader
  • System class loader

Question

What Java package is imported automatically?

Answer

Java is useless without much of the functionality in the java.lang package, which is why it is implicitly imported by the compiler for all programs.


Question

What is the difference between JRE, JVM, and JDK?

Answer

JRE stands for Java Runtime Environment and is the environment which Java bytecode can be executed. JVM stands for Java Virtual Machine and is the virtual machine that enables a computer to run Java programs. JDK stands for Java Development Environment and is the tools necessary to compile, document, and package Java programs.


Question

What are the different access modifiers for Java classes?

Answer

The different types of access specifiers for classes include:

  • Public
  • Protected
  • Private
  • Default


Question

What is Java Encapsulation?

Answer

Encapsulation is the wrapping up of data under a single unit. Java Encapsulation is the ability for a Java object to hide or reveal its methods and data. Take a look at a Java Encapsulation example.


Question

What is Java Polymorphism?

Answer

Polymorphism is the ability of an object to take on many forms. The common example of this in Java is when a sub class extends a base class. Take a look at a Java Polymorphism example.


Question

What is public static void main(String[] args)?

Answer

Public is the access modifier. Static means something belongs to the class and there can only be one instance of a static variable. Void is a return type that means nothing will be returned. Main is the starting or entry point of a program. String[] args is an array of strings passed into the application.


Question

Why is Java Platform Independent?

Answer

The phrase “write once, run anywhere” was coined by Java. The compiled byte code can be run on multiple platforms without the need to re-compile per platform.


Question

Is Java a 100% Object Oriented Programming language?

Answer

No because there are eight primitive datatypes in Java that are not Objects. Boolean, byte, char, int float, double, long, and short are not Objects.


Question

What function to Java wrapper classes perform?

Answer

Java wrapper classes “wrapprimitive datatypes that are not Objects into Objects.

  • Boolean wraps a boolean
  • Byte wraps a byte
  • Character wraps a char
  • Integer wraps an int
  • Float wraps a float
  • Double wraps a double
  • Long wraps a long
  • Short wraps a short

Take a look at the Java Wrapper Classes tutorials and further explanation.


Question

What is a Constructor in Java?

Answer

A constructor is a block of code used to initialize an Object. It will always have the same name as the Class, no return type, and is called automatically when an Object is created. A class can have multiple constructors as long as they all have different input parameters.


Question

What is a Java Singleton Class?

Answer

A Singleton Class is a Class that can only have one instance at any given time per JVM. Making the constructor private will make a Class a singleton.


Question

What is the difference between Stack and Heap memory?

Answer

Stack memory is used by only one thread of execution, can’t be accessed by other threads, and exists only until the end of thread execution. Heap memory is used by all parts of application, objects stores in heap are accessible globally, and heap memory exists until the application exits.


Question

What is Inheritance in Java?

Answer

Inheritance in Java allows you to build code on top of code that already exists. If you have a base class that includes some of the functionality you are working on, it’s easier to inherit the base class and build a subclass upon that. Take a look at the Java Inheritance sample code.


Question

What is method Overloading?

Answer

Method Overloading allows you to have multiple methods with the same name that have different signatures or parameters. Take a look at the method overloading sample code.


Question

Is multiple inheritance supported in Java?

Answer

Java supports multiple inheritance only through its interfaces, but through classes.


Question

Can a top level class be private or protected?

Answer

Top level classes in Java can’t be private or protected, but inner classes can be. No one can see a top level private class and therefore it could never be used.


Question

Are “throw” and “throws” the same in Java Exceptions?

Answer

Not the same. The “throw” keyword is used to throw an Exception from a method or block. The “throws” keyword is used to indicate an Exception can be thrown by a method or block.


Question

How are pointers used in Java?

Answer

There are no pointers in the Java programming language.


Question

Can you change the size of an Array after declaration?

Answer

No because Arrays are static and once created the size can’t be changed.


Question

What is the base class in Java for all Classes?

Answer

java.lang.object


Question

What is Multi-Threading in Java?

Answer

Multi-Threading is a programming concept to execute multiple tasks at the same time in a single program. Threads share same process stack and running in parallel.


Question

What is the base class in Java for all Exceptions?

Answer

java.lang.Throwable


Question

What is the difference between Checked and Unchecked Excpetion?

Answer

In Java, a Checked Exception (Compile Time) must be handled within a try-catch block or declared in a throws clause. An Unchecked Exception (Runtime) is not required to be handled or declared.


Question

What is a Lambda Expression?

Answer

Lambda Expression was introduced in Java 8 and is an anonymous function that accepts a set of input parameters and returns a result.


Question

Name a Java Collection that is thread-safe?

Answer

ConcurrentHashMap


Question

?

Answer