Top 40 Core Java Interview Questions You Must Prepare

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

To become a proficient Core Java developer, it's essential to understand that Java is a powerful language, widely used for data processing, application development, and large-scale systems. Java offers robust tools and frameworks that enable complex computational tasks and the creation of sophisticated models to manage and analyze data efficiently.

Why Choose Java?

  1. Industry Proven: Java has been a dominant force in the software development industry, trusted by many organizations for building scalable and secure applications. It has established itself as a reliable toolkit for enterprise-level application development.

  2. Open-Source Libraries: Java boasts a vast array of open-source libraries, providing various approaches to achieving similar outcomes. This flexibility allows developers to select the best tools for their specific needs.

  3. Competitive Edge: In today’s competitive technology landscape, developers and engineers are constantly looking for ways to process information efficiently, extract meaningful insights, and build models that handle massive datasets. Java’s performance and scalability make it an ideal choice for these tasks.

  4. Versatility: Java is a versatile platform that supports a wide range of libraries and frameworks, enabling developers to tackle various tasks, from data processing to application development. Being well-versed in these libraries will enhance the robustness and speed of your development work.

 

  1. What is Java Language?

Java is a Object Oriented programming language and platform independent. So, java is secure and reliable for coding mobile apps and enterprise software to big data applications and server-side technologies.

  1. What is java virtual machine?

Java virtual machine is known as JVM, it allows to run java programs on any device or operating system based on “write once run anywhere” principal and all the java programs are compiled for the JVM.

  1. Is Java Platform Independent?

Yes, Java is Platform Independent because all APIs compiled into bytecodes and the virtual machine taking care of bytecode form different platforms. 

  1. Explain difference between Heap and Stack memory in Java?

Basically, stack memory is small in size when compared to heap size and stack having temporary variables and data but heap used large amount of data. If stack having no space to create new object it throws an error message like java.lang.StackOvreflowError but if heap size is full it throws java.lang.OutOfMemoryError.

  1. Explain difference between Instance variable and Local variable?

Instance variables are declared out side of the class but local variables are declared inside of method or constructor.

  1. What is marker interface?

Marker Interface does not have any method inside. JDK having built in marker interfaces are Serializable interface, Cloneable interface and Remote interface.

  1. What is functional interface?

Functional interface means it allows only one abstract method and it provides from the java version 1.8 onwards. We use @FunctionalInterface annotation to make an interface into fuctional interface.
We have four type of functional interfaces and these are applied at multiple situations

  • Consumer
  • Predicate
  • Function
  • Supplier

Example for user defined functional interface

@FunctionalInterface
interface A
{
    int a();
}
  1. What is singleton class?

Singleton class allows only one instance of class exist.
Follow some rules for Singleton class
Declare Access modifier must be private for all construtors and static method returns a reference to the instance.
The instance must be stored into private static variable.
Example:

class NIT // class NIT to make singleton
{
    private static NIT instance_var = null; // Here static variable instance_var refers to NIT  
    public String str;
   
    public NIT() {
        str = "Welcome to NareshIT";
    }
   
   
    public static NIT getInstance_var() {
        if(instance_var ==null)
            instance_var = new NIT();
       
        return instance_var;
    }
}
public class InterviewTest {
    public static void main(String[] args) {
        NIT nit1 = NIT.getInstance_var();
        NIT nit2 = NIT.getInstance_var();
        if(nit1==nit2)
        {
            System.out.println("Singleton");
        }
       
        else
            System.out.println("No instance");
    }
}
  1. What is subclass?

A class that derived from another class or parent/super class is known as subclass.

class NIT
{
   
}
class Naresh extends NIT // Naresh is subclass of NIT
{
   
}
  1. What is constructor in java?

A construcor is special method of class and the declare same name of class if required. Whenever object is created the constructor automatically called.
Construcor does not have any return rype and it calls only once at the time of object creation.

class NIT
{
    //Constructor
    public NIT() {
        System.out.println("Constructor");
    }
}
public class InterviewTest {
    public static void main(String[] args) {
        NIT nit = new NIT(); // object creation for NIT class
    }
}
  1. What are the various types of interfaces in java?

There are mainly three types of interfaces
1.    Noramal interface
Examples: List, Set, Map
2.    Functional interface
Example: Runnable
3.    Marker interface
Example: Serilizable interface

  1. What is the difference between Set and List?

The major difference is List allows duplicate values but Set does not allows duplicate values and List allows multiple null values but set allows only one null value.
Example for List and Set interface difference

class NIT
{
    public void nit() {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(1);
        list.add(2);
        list.add(null);
        list.add(null);
        System.out.println(list);
       
        Set<Integer> set = new HashSet<Integer>();
        set.add(1);
        set.add(2);
        set.add(1);
        set.add(2);
        set.add(null);
        set.add(null);
        System.out.println(set);
    }
}
public class InterviewTest {
    public static void main(String[] args) {
        NIT nit = new NIT();
        nit.nit();
    }
}
  1. What is linked list?

Linked list is one of linear data structure and data and next node consider as single node. Where single node stores the data and address of the next node and the last node is contains NULL value.
Different types of Linked list
1.    Single Linked list
2.    Double Linked List
3.    Circular Linked List

  1. What is ArrayList?

ArrayList is a class implemented by using List interface. The functionality of ArrayList is dynamic array and there is no fixed size of an array. ArrayList is found in java.util package. It contains duplicate values and maintain insertion order. By using add() method to add new elements into ArrayList
Syntax:
ArrayList<E> al = new  ArrayList<E>();
Example:
ArrayList<String> al = new  ArrayList<String>();

  1. What is Deque?

DeQue stands for Double-ended-queue, it is linear data structure which allows the data manipulation the data from both the ends.

  1. What is TreeSet?

TreeSet is a class which provides an implementation of Set interface and stored the elements or objects in ascending order.
Syntax:
TreeSet<E> tree = new  TreeSet<E>();
Example:

class NIT
{
    public void nit() {
        TreeSet<Integer> tree = new TreeSet<Integer>();
        tree.add(2);
        tree.add(5);
        tree.add(1);
        System.out.println(tree);
    }
}
public class InterviewTest {
    public static void main(String[] args) {
        NIT nit = new NIT();
        nit.nit();
    }
}
  1. What is Iterator in java?

Iterator is a loop for collection framework to retrieve the elements one after other by using iterator method. In Iterator we have three methods called as
next() : It prints next element if present otherwise it throws NoSuchElementException
hasNext() : It return all the elements available in the collection
remove() : It remove next element in the in iterator.
Example:

class NIT
{
    public void nit() {
        ArrayList<String> al = new ArrayList<String>();
        al.add("Java");
        al.add("Python");
        al.add("Angular");
        Iterator<String> iterator = al.iterator();
        System.out.println(iterator.next());
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
  1. What is hash map?

HashMap is the implementation of Map interface and it stores the data in the form if key and value pair.
It is availabe in java.util package. If we try to insert duplicate key and it will replace the element of correspondent key.
Example of HashMap:

class NIT
{
    public void nit() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("Java", "Language");
        map.put("Java", "Computer Language");
        System.out.println(map);
    }
}
public class InterviewTest {
    public static void main(String[] args) {
        NIT nit = new NIT();
        nit.nit();
    }
}
  1. What is the super class of Object?

Object is a parent class of all classes in java.

  1. What is the difference between method overloading and method overriding?

Method overloading is compile time polymorphism and method overriding run time polymorphism.
Method overloading shares more than one method with different arguments in the class but method overriding is based on inheritance which develops from parent class.

  1. What is the difference between method interface and abstract class?

Abstract class having abstract and non-abstract methods but interface having abstract, default and static methods.
Abstract class does not support multiple inheritance but Interface supports multiple inheritance.

  1. Explain Constructor overloading?

Constructor overloading is more than one Constructor with different parameters of same class name.
Example:

class NIT
{
    public NIT() {
       
    }
    public NIT(int a) {
       
    }
    public NIT(String a) {
       
    }
    public NIT(String a, int b) {
       
    }
}
  1. Can main() method is overloaded in java?

main() method is overloaded but according to java syntax first jvm calls main method like “public static void main(String[] args)” only

public class InterviewTest {
    public static void main(String[] args) {
        System.out.println("main method");
    }
   
    public static void main(char[] args) {
        System.out.println("Char main method");
    }
   
    public static void main(int[] args) {
        System.out.println("int main method");
    }
}
  1. What is Lambda function?

Lambda function is commonly known as anonymous function and it is user defined function without name. In java we have lambda expression for express instances of functional interfaces.
Lambda Expression are divided into three parts one is Arguments next Arrow token and Body of Lambda expression
Syntax:
Lambda operator -> body

  1. What is polymorphism?

Polymorphism in general known as many forms and we have two types of polymorphism
1.    Run time polymorphism (method overriding) : same name from different class and this is called as “Late binding”.
2.    Compile polymorphism (method overloading) : Same name with different parameters

  1. What is encapsulation?

Encapsulation is one of the fundamental concept of Object-Oriented-Programming language and we encapsulate both variables(data) and data(method) in class, In other words this is a process of wrapping the data and code  together in single unit.
Binding the data by using private keyword for variables and to access the data from setter and getter method.
Syntax:
<Access_Modifier> class <Class_Name> {
private <Data_Members>;
private <Data_Methods>;
}
Example:

class NIT
{
    private int id;
    private String course;
    private String faculty;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public String getFaculty() {
        return faculty;
    }
    public void setFaculty(String faculty) {
        this.faculty = faculty;
    }
   
}
  1. What is String pool?

String pool means Object allocation of String and it stores in java heap memory and which is maintained by String class, by default it is empty.
When create the String, JVM checks in string pool if the same string object is available or not and if not available the string it returns new String Object otherwise it return reference of the same String.
Example:

public class InterviewTest {
    public static void main(String[] args) {
        //Java is case sensitive
        String a = "Java";
        String b = "java";
        String c = "Java";
        if(a==b) //false
            System.out.println("a equals b");
        else if(a == c) // true
            System.out.println("a equals c");
        else
            System.out.println("false");
    }
}
  1. What the difference is between extends and implements keywords in java?

Extends keyword is used to inherit classes or interfaces and implements keyword is used to implement the interfaces.
Extends keyword may override some of the methods from superclass to subclass but a class must implement all the methods from interfaces.
Examples:

class NIT
{
   
}
class Naresh extends NIT
{
   
}
interface INIT
{
   
}
interface INaresh extends INIT
{
    void min();
    void max();
}
class Test implements INaresh
{
    @Override
    public void min() {
       
    }
    @Override
    public void max() {
       
    }
   
}
  1. Can we use single try block and multiple catch blocks in java?

Yes, we have multiple catch blocks for single try block but it does not allows once you declare ‘Exception’ in catch block, it is end of the catch block because Exception class is parent class of all type of Exceptions.
Example:

class NIT
{
    void test()
    {
        try {
           
        }catch (ArrayIndexOutOfBoundsException e) {
           
        } catch (Exception e) {
           
        }
       
        }
}
  1. Can we define private and protected modifiers for data members (fields) in interfaces?

We cannot define private and protected modifiers for data members (fields) in interfaces because in java interfaces by default it allows public modifier and static and final are permitted.

interface INIT
{
    public int a;
    private int b; //error
    protected String i; //error
}

  1. Is it possible to define a class inside an interface?

Yes, it possible to define a class inside an interface
Example 1

interface INIT
{   
    class Faculty implements INIT
    {
       
    }
}


Example 2

class NIT implements INIT {
   
}
interface INIT {
    class Faculty {
    }
}
  1. Is interface have a constructor?

Interfaces cannot have constructors. Because interface attributes are by default public,   static and final.

  1. What is nested interface?

Nested interface is nothing but we declare interface within class or interface. Nested interface cannot access directly it must be referred by outer class or interface and it must be public.
Example 1

interface INIT {
    interface inside_INIT
    {
       
    }
}


Example 2

class NIT{   
    interface INIT
    {
       
    }
}
  1. What are the modifiers are allowed for methods in an interface?

Public and abstract are the modifiers are allowed for methods in an interface
Example:

interface INIT {
    abstract void test();
    public void end();
    private void start(); // error
}
  1. Can an interface have instance and static blocks?

Interfaces does not allow static block but it allows public final static members.
Example 1:

interface INIT {
    static {}//error
}


Example 2:

interface INIT {
    public static void test() {
       
    }
}
  1. Can an interface be final?

Interface cannot declare as final because all the methods are abstract and only public and abstract are allowed.

final interface INIT { // error
    public static void test() {
       
    }
}
  1. Can abstract classes have constructors in Java?

Abstract classes can have constructors, but they cannot be instantiated directly. The constructors are used when a concrete subclass is created.

abstract class NIT{   
    //constructor
    public NIT() {
       
    }
    NIT nit = new NIT(); // error: cannot instantiated
}
  1. Can an abstract class be final in Java?

Abstract class cannot be final in java because abstract methods are not to be final but abstract class has final methods.
Example 1:

final abstract class NIT{ // error
   
   
}


Example 2:

abstract class NIT{
    final abstract void info(); //error
   
    final void test(){}
}
  1. Is it necessary for an abstract class to have an abstract method?

An abstract class does not required to have an abstract method but any class or super class have abstract method it must be implement abstract for the class.
Example 1: not required abstract method when declare class as abstract

abstract class NIT{
   
}


Example 2: when abstract method is created you must declare class as abstract

class NIT{ //error
   
    private void test() {
       
    }
   
    abstract void info(); // here you must declare abstract as class   
}


Example 3:

abstract class NIT{
   
    private void test() {
       
    }
    abstract public void til();
}
class NIT1 extends NIT // NIT1 must implement the inherited abstract method NIT.til()
{
   
}
  1. Can an abstract method be declared with private modifier?

Abstract methods are to be declare as private, it allows can be public, default and public
Example:

abstract class NIT{
    private abstract void test(); //error
    public abstract void test1();
    abstract void test2();
    public abstract void test3();
}  

Scope @ NareshIT:

NareshIT's Core Java Online Training program offers extensive hands-on training across front-end, middleware, and back-end technologies.

  • Real-World Projects: The program equips you with practical skills through phase-end and capstone projects based on real business scenarios.

  • Expert Guidance: You'll learn from leading industry experts, with content meticulously structured to ensure relevance to current industry needs.

  • Comprehensive Learning: The program covers end-to-end application development, enabling you to build applications with exciting and practical features.

  • Certification: Upon completion, you'll earn an industry-recognized course completion certificate, validating your expertise in Core Java development.