Freshers Java Interview Questions

What is encapsulation in Java?

Encapsulation is hiding the data of a class and allowing access only through methods. You make the fields private and write public getters and setters, which can check the values.

class Account {
    private double balance;

    public double getBalance() { return balance; }

    public void deposit(double amount) {
        if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
        balance += amount;
    }
}

public class Main {
    public static void main(String[] args) {
        Account a = new Account();
        a.deposit(500);
        System.out.println(a.getBalance());
    }
}
Output

500.0

Read Java Encapsulation and Access Modifiers.

What are the access modifiers in Java?

ModifierSame classSame packageSubclassOther packages
privateyesnonono
default (no word)yesyesnono
protectedyesyesyesno
publicyesyesyesyes

Use the most restricted level which works. Fields are usually private, and the methods which others must use are public. See Encapsulation.

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

PointArrayArrayList
SizeFixedDynamic
TypesPrimitives and objectsObjects only (use wrappers)
Lengtharr.lengthlist.size()
Add or removeNot possibleadd(), remove()
GenericsNoYes
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[3];
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);
        System.out.println(arr.length + " " + list.size());
    }
}
Output

3 2

See Java Arrays and Java ArrayList.

Why is the main method public static void in Java?

The JVM starts a program by calling public static void main(String[] args).

  • public: the JVM is outside your class, so the method must be visible.
  • static: the JVM calls it before any object exists.
  • void: it does not return a value to the JVM.
  • String[] args: the command line arguments.

Since Java 25 you can write a short form for small programs, with no class and no public static:

void main() {
    IO.println("Hello from a compact source file");
}
Output

Hello from a compact source file

See Your First Java Program.

What is garbage collection in Java?

In Java you create objects with new but you never delete them. The garbage collector of the JVM finds the objects that no variable can reach anymore, and frees their memory.

public class Main {
    public static void main(String[] args) {
        Object o = new Object();
        o = null;                  // now nothing points to the first object
        System.gc();               // only a request; the JVM decides
        System.out.println("Done");
    }
}
Output

Done

  • You can not force garbage collection. System.gc() is only a hint.
  • Memory leaks can still happen, for example when a static list keeps growing.

What is a package in Java?

A package groups related classes and interfaces, like folders group files. The first line of the file is package com.shop; and the folder must be com/shop. Packages avoid name clashes and give package level access.

  • java.lang is imported automatically (String, Math).
  • Other packages need an import, like import java.util.List;.

See Java Packages and Imports for a tested example with javac -d.

What is an exception in Java? What are checked and unchecked exceptions?

An exception is an event which stops the normal flow of the program. Java gives an object of the exception, and you can catch it with try and catch.

TypeMust handle?Example
CheckedYes, the compiler forces itIOException, SQLException
Unchecked (RuntimeException)NoNullPointerException, ArithmeticException
ErrorNo, do not catchOutOfMemoryError
public class Main {
    public static void main(String[] args) {
        try {
            System.out.println(10 / 0);
        } catch (ArithmeticException e) {
            System.out.println("Caught: " + e.getMessage());
        } finally {
            System.out.println("finally runs always");
        }
    }
}
Output

Caught: / by zero
finally runs always

See Java Exceptions.

What are autoboxing and unboxing in Java?

Each primitive has a wrapper class: int – Integer, double – Double, boolean – Boolean. Java converts between them by itself:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Integer boxed = 5;                // autoboxing
        int plain = boxed;                // unboxing

        List<Integer> list = new ArrayList<>();
        list.add(10);                     // autoboxing
        int first = list.get(0);          // unboxing
        System.out.println(boxed + plain + first);

        Integer a = 127, b = 127, c = 1000, d = 1000;
        System.out.println(a == b);       // true: small values are cached
        System.out.println(c == d);       // false: different objects
        System.out.println(c.equals(d));
    }
}
Output

20
true
false
true

Compare wrapper objects with equals(), not with ==. Unboxing a null gives a NullPointerException.

What is a JAR file in Java and how to create one?

A JAR (Java ARchive) file is a ZIP file which holds compiled .class files, resources and a manifest. It is the normal way to share a Java library or an application.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Running from a JAR");
    }
}
Terminal

$ javac Hello.java
$ jar cfe app.jar Hello Hello.class
$ java -jar app.jar
Running from a JAR
$ jar tf app.jar
META-INF/
META-INF/MANIFEST.MF
Hello.class

  • c create, f file name, e entry point (the class with main).
  • jar tf lists the content of a JAR.

Real projects use Maven or Gradle to build the JAR.

Which Java version should I learn? What are the LTS versions?

Java has a new version every 6 months (in March and September). Every 2 years, one of them is an LTS (long term support) release, which gets updates for many years.

LTS versionReleased
Java 8March 2014
Java 11September 2018
Java 17September 2021
Java 21September 2023
Java 25September 2025

Learn the newest LTS (Java 25), because almost everything of Java 8, 11, 17 and 21 works in it, and you also learn the modern features. In a job you may meet Java 8, 11 or 17 code, so the older features are in this tutorial too.

Terminal

$ java -version 2>&1 | head -1
openjdk version "25.0.5" 2026-10-20 LTS

Java vs Python: which language should you learn first?

Both are among the most popular languages. In the TIOBE index of November 2026 Python is first (20.9%) and Java is third (8.9%), and in the PYPL index of Google tutorial searches the two are on the first and the second place.

PointPythonJava
StartVery easy, short syntaxMore code to write, stricter rules
TypingDynamic (types checked when running)Static (types checked by the compiler)
SpeedSlower for heavy workFaster (JIT compiler)
Strong inData, AI, scripts, automationBig back ends, Android, banking, large teams
RunInterpreterCompile to bytecode, run on the JVM

If you want to start fast, begin with Python. If you want to learn how programs are built with types and classes, or you plan an enterprise or Android career, Java is a great choice. After one, the other becomes easy.

What is the difference between throw and throws in Java?

Pointthrowthrows
KindStatement inside a methodKeyword in the method declaration
UseCreate and send an exception objectWarn that the method can throw exceptions
Followed byOne exception objectOne or many exception types
import java.io.IOException;

public class Main {
    static void check(int age) throws IOException {                // throws: declaration
        if (age < 0) {
            throw new IOException("Age can not be negative");       // throw: action
        }
        System.out.println("Age ok");
    }

    public static void main(String[] args) {
        try {
            check(10);
            check(-1);
        } catch (IOException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
Output

Age ok
Caught: Age can not be negative

See Java Exceptions.