60 Core Java interview Question & Answer for fresher or Junior developer

For both freshmen and experienced applicants, we have covered about 50 crucial core Java interview questions in this article.

This article on JAVA Interview Questions was written to assist you in grasping the fundamental ideas of Java programming in preparation for interviews. For your ease of learning, all the key JAVA principles are taught here with examples.

This course goes over fundamental Java terminology, OOP ideas, Access specifiers, Collections, Exceptions, Threads, Serialization, and other JAVA subjects with examples to help you prepare thoroughly for any JAVA interview.

What is the difference between JDK, JRE, and JVM?
  • JDK stands for Java Development Kit. It is a software development environment used for developing Java applications. It includes the JRE, as well as tools for developing, debugging, and compiling Java code.
  • JRE stands for Java Runtime Environment. It is a package that contains the necessary libraries and other files to run a Java program. It does not include the tools for developing Java programs.
  • JVM stands for Java Virtual Machine. It is a virtual machine that runs Java bytecode and is used to execute Java programs.
What is a Java Class?
  • A Java class is a template for creating objects. It defines the properties and behaviors of an object.
What is an object in Java?
  • An object is an instance of a Java class. It is a real-world entity that has state (data) and behavior (methods).

What is a Java constructor?

  • A Java constructor is a special method that is used to create and initialize an object. It has the same name as the class and is called when an object is created.

 

What is inheritance in Java?

  • Inheritance is a way to create a new class that is a modified version of an existing class. The new class is called the subclass, and the existing class is the superclass. The subclass inherits all the properties and behaviors of the superclass, and can also have additional properties and behaviors of its own.

What is polymorphism in Java?

  • Polymorphism is the ability of a single object to take on multiple forms. In Java, polymorphism is achieved through inheritance and method overriding.

What is an interface in Java?

  • An interface in Java is a collection of abstract methods that must be implemented by any class that implements the interface. It is used to define a set of behaviors that a class must have, without specifying the implementation of those behaviors.

What is an abstract class in Java?

  • An abstract class in Java is a class that cannot be instantiated and must be subclassed. It can have both abstract methods (methods with no implementation) and concrete methods (methods with an implementation).

What is an exception in Java?

  • An exception in Java is an error that occurs during the execution of a program. It can be handled by using a try-catch block, or it can be thrown to the caller of the method that caused the exception.

What is the difference between checked and unchecked exceptions in Java?

  • Checked exceptions are exceptions that are checked by the compiler at compile-time. They must be handled or declared in the method where they are thrown. Unchecked exceptions are exceptions that are not checked by the compiler. They include runtime exceptions and errors, and do not need to be handled or declared.

What is a Java package?

  • A Java package is a collection of related Java classes. It is used to organize classes into a logical hierarchy and prevent naming conflicts.

What is a Java wrapper class?

  • A Java wrapper class is a class that wraps (encapsulates) a primitive data type and converts it to an object. There is a wrapper class for each primitive data type in Java.

What is the difference between static and non-static methods in Java?

  • In Java, a static method is a method that belongs to the class rather than an instance of the class. This means that it can be called directly on the class, without the need to create an object of the class first. A non-static method, also known as an instance method, belongs to an instance of a class and can only be called on an object of that class.

    There are a few key differences between static and non-static methods in Java:

    • Access Modifiers: static methods can be called without creating an instance of the class, so they can be declared as public, whereas non-static methods should be called on an object, so they can be declared as private, protected, and package-private etc.
    • Memory allocation: When a static method is called, it does not require the creation of an object in memory, whereas a non-static method requires the existence of an object in memory to be called.
    • Variables: Non-static methods have access to both static and non-static variables, whereas static methods can only access static variables.
    • this and super keyword: The this keyword can be used in a non-static method to refer to the current object, whereas it cannot be used in a static method. Similarly, the super keyword can be used in a non-static method to refer to the parent class, but cannot be used in a static method.

What is the difference between final, finally, and finalize in Java?

  • final is a modifier that can be used with variables, methods, or classes. It indicates that the variable or method cannot be changed, and that the class cannot be subclassed.
  • finally is a block of code that is always executed after a try-catch block, whether or not an exception is thrown. It is used to clean up resources that were used in the try block.
  • finalize is a method that is called by the garbage collector before an object is garbage collected. It can be overridden to specify any cleanup actions that need to be taken for the object before it is garbage collected.

What is a Java array?

  • A Java array is a collection of elements of the same data type. It can be used to store a fixed-size sequential collection of elements.

What is an enumeration in Java?

  • An enumeration in Java is a special type of class that represents a fixed number of constants. It is used to define a set of named values that a variable can take on.

What is the difference between a while loop and a do-while loop in Java?

  • A while loop in Java executes a block of code as long as a certain condition is true. The condition is checked at the beginning of each iteration. A do-while loop is similar, but the condition is checked at the end of each iteration. This means that the block of code will be executed at least once, even if the condition is false.

What is the difference between a for loop and a for-each loop in Java?

  • A for loop in Java is used to execute a block of code a specific number of times. It has a loop counter that is initialized, tested, and incremented on each iteration. A for-each loop is used to iterate over the elements of an array or collection. It does not have a loop counter, and the loop variables are automatically set to the elements of the array or collection on each iteration.

What is a Java thread?

  • A Java thread is a separate path of execution within a Java program. It allows multiple tasks to be performed concurrently within a single program.

What is the difference between a process and a thread?

  • A process is an instance of a program that is executing. It has its own memory space and resources. A thread is a separate path of execution within a process. It shares the process’s memory and resources, and can run concurrently with other threads in the same process.

What is a Java applet?

  • A Java applet is a small program that is written in Java and can be embedded in a web page. It is executed by the web browser on the client side, rather than on the server side.

What is a Java servlet?

  • A Java servlet is a program that is written in Java and runs on a web server. It is used to process requests and generate dynamic content for web pages.

What is a JavaBean?

  • A JavaBean is a Java class that follows a set of conventions for naming and coding. It is a reusable software component that can be easily used in different Java programs.

What is a Java stream?

  • A Java stream is a sequence of data elements that supports various operations for processing the data. It is used to perform input and output operations on collections, arrays, and other data sources.

What is a Java Collection?

  • A Java Collection is a group of objects that are stored in a single unit. It is used to store, retrieve, and manipulate data.

What is the difference between a Set and a List in Java?

  • A Set is a collection that cannot contain duplicate elements. It is unordered, meaning that the elements are not stored in a specific order. A List is a collection that can contain duplicate elements. It is ordered, meaning that the elements are stored in a specific order.

 What is a Map in Java?

  • A Map is a collection that maps keys to values. It is used to store data in key-value pairs.

What is a LinkedList in Java?

  • A LinkedList is a implementation of the List interface in Java. It is a linear data structure that stores elements in nodes, with each node containing a reference to the next node. It allows for fast insertion and deletion of elements, but slower access to elements.

What is a HashMap in Java?

  • A HashMap is a implementation of the Map interface in Java. It uses a hash table to store the elements, with each element being stored in a bucket based on its hash code. It allows for fast insertion, deletion, and retrieval of elements.

What is a TreeMap in Java?

  • A TreeMap is a implementation of the Map interface in Java. It stores the elements in a sorted, ascending order based on the keys. It uses a red-black tree data structure to store the elements.

What is a Stack in Java?

  • A Stack is a Last-In-First-Out (LIFO) data structure. It allows for the insertion and deletion of elements only at the top of the stack.

What is a Queue in Java?

  • A Queue is a First-In-First-Out (FIFO) data structure. It allows for the insertion of elements at the end of the queue, and the deletion of elements from the front of the queue.

What is a Deque in Java?

  • A Deque (Double-Ended Queue) is a data structure that allows for the insertion and deletion of elements at both ends of the queue.

What is a PriorityQueue in Java?

  • A PriorityQueue is a queue that orders the elements based on their priority. The element with the highest priority is dequeued first.

What is an Iterator in Java?

  • An Iterator is an interface that is used to iterate over the elements of a collection. It allows for the traversal of the elements in a collection and the removal of elements from the collection.

What is a Comparator in Java?

  • A Comparator is an interface that is used to compare two objects. It is used to specify the order in which the elements in a collection should be sorted.

What is a Comparable in Java?

  • A Comparable is an interface that is used to compare an object with other objects of the same type. It is used to specify the natural ordering of the elements in a collection.

What is a ListIterator in Java?

  • A ListIterator is an iterator that is specifically designed for use with a List. It allows for the traversal of the elements in a list in either direction, and for the modification of elements in the list.

What is a ConcurrentModificationException in Java?

  • A ConcurrentModificationException is an exception that is thrown when an object is modified concurrently while it is being iterated over. It indicates that the object has been modified in a way that violates the iterator’s fail-fast property.

What is a Stream in Java 8?

  • A Stream in Java 8 is a sequence of elements that supports various operations for processing the elements. It is used to perform functional-style operations on collections, arrays, and other data sources.

What is a Lambda Expression in Java 8?

  • A Lambda Expression in Java 8 is a concise way to represent a function as an object. It allows for the implementation of functional interfaces (interfaces with a single abstract method) in a more concise and convenient way.

In Java 8, a lambda expression is a way to represent a functional interface (an interface with a single abstract method) using an anonymous function. It is a concise and more readable way to write functional-style code in Java.

A lambda expression has the following syntax:

(parameters) -> {function body}

The parameters are the input to the lambda expression, and the function body is the code that is executed when the lambda expression is called.

For example, consider the following functional interface:

interface MathOperation {
int operation(int a, int b);
}

This interface defines a single method, operation, that takes two integers as input and returns an integer. A lambda expression can be used to implement this interface:

MathOperation addition = (int a, int b) -> a + b;

This lambda expression takes two integers as input and returns the sum of the two integers. The type of the input parameters can be inferred by the compiler, so it can be written as:

MathOperation addition = (a, b) -> a + b;

Lambda expressions are often used with functional interfaces provided by the Java API, such as java.util.function package, like Predicate, Function, Consumer, Supplier, UnaryOperator, BinaryOperator etc. They are also used in functional programming constructs like streams, collectors, and parallel processing in Java 8.

What is the Optional class in Java 8?

  • The Optional class in Java 8 is a container class that is used to represent the presence or absence of a value. It is used to avoid null pointer exceptions and to improve code readability.

What is the Stream API in Java 8?

  • The Stream API in Java 8 is a set of classes and interfaces that support functional-style operations on streams of elements. It allows for the processing of collections, arrays, and other data sources in a more concise and efficient way.

What is the difference between the Stream API and the Collection API in Java?

  • The Stream API is a set of classes and interfaces that support functional-style operations on streams of elements. It allows for the processing of collections, arrays, and other data sources in a more concise and efficient way. The Collection API is a set of interfaces and classes that define various types of collections, such as lists, sets, and maps. It is used to store and manipulate groups of objects.

What is a Functional Interface in Java 8?

  • A Functional Interface in Java 8 is an interface that has a single abstract method. It is used to represent a function as an object, and can be implemented using a lambda expression.

What is the difference between a Iterator and a Spliterator in Java 8?

  • An Iterator is an interface that is used to iterate over the elements of a collection. It allows for the traversal of the elements in a collection and the removal of elements from the collection. A Spliterator is a new interface introduced in Java 8 that is used to split the elements of a collection into smaller parts. It is used to perform parallel operations on the elements of the collection.

What is the difference between a for loop and a for-each loop in Java?

  • A for loop in Java is used to execute a block of code a specific number of times. It has a loop counter that is initialized, tested, and incremented on each iteration. A for-each loop is used to iterate over the elements of an array or collection. It does not have a loop counter, and the loop variables are automatically set to the elements of the array or collection on each iteration.

What is the difference between a try-with-resources block and a try-finally block in Java?

  • A try-with-resources block is a try block that declares one or more resources, such as files or streams, within the try block. These resources are automatically closed when the block is exited, whether or not an exception is thrown. A try-finally block is a try block that is followed by a finally block. The finally block is always executed, regardless of whether an exception is thrown or caught. It is used to clean up resources that were used in the try block.

What is the difference between an exception and an error in Java?

  • An exception in Java is an abnormal condition that occurs during the execution of a program. It can be handled by using a try-catch block, or it can be thrown to the caller of the method that caused the exception. An error in Java is a serious condition that indicates a problem with the system. It cannot be handled or recovered from, and

 

What is the difference between static and dynamic binding in Java?

  • Static binding in Java refers to the binding of method calls to the corresponding method definitions at compile-time. It occurs when the method call and the method definition have the same signature and are in the same class or a subclass. Dynamic binding, also known as late binding, refers to the binding of method calls to the corresponding method definitions at runtime. It occurs when the method call and the method definition have the same signature, but are in different classes.

What is the difference between overloading and overriding in Java?

  • Method overloading in Java refers to the ability of a class to have multiple methods with the same name, but different signatures. It allows for the creation of methods that perform similar tasks, but with different input parameters. Method overriding in Java refers to the ability of a subclass to provide a different implementation of a method that is inherited from the superclass. It allows for the modification or extension of the behavior of the inherited method.

What is the difference between a class and an interface in Java?

  • A class in Java is a template for creating objects. It defines the properties and behaviors of an object. An interface in Java is a collection of abstract methods that must be implemented by any class that implements the interface. It is used to define a set of behaviors that a class must have, without specifying the implementation of those behaviors.

What is a Java annotation?

  • A Java annotation is a special type of metadata that can be added to Java code. It provides additional information about the code, and can be used by tools, frameworks, or other programs to process the code in specific ways.

What is the difference between a Java array and an ArrayList?

  • A Java array is a collection of elements of the same data type. It has a fixed size, and the elements are stored in contiguous memory locations. An ArrayList is a resizable array implementation of the List interface in Java. It allows for the dynamic addition and removal of elements, and the elements are stored in an array that is automatically resized as needed.

What is a Java Enum?

  • A Java Enum is a special type of class that represents a fixed number of constants. It is used to define a set of named values that a variable can take on.

What is a Java String?

  • A Java String is a sequence of characters. It is an immutable class, meaning that the characters in a String object cannot be changed once the object is created.

What is a Java StringBuilder?

  • A Java StringBuilder is a mutable sequence of characters. It allows for the modification of the characters in the sequence without creating a new object. It is often used for constructing strings in a more efficient way.

What is the difference between a String and a StringBuilder in Java?

  • A String in Java is an immutable sequence of characters. It cannot be modified once it is created. A StringBuilder is a mutable sequence of characters. It allows for the modification of the characters in the sequence without creating a new object.

What is the difference between a try-catch block and a try-catch-finally block in Java?

  • A try-catch block in Java is used to handle exceptions that may be thrown during the execution of a program. It consists of a try block that contains the code

Leave a Comment