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());
}
}
500.0
What are the access modifiers in Java?
| Modifier | Same class | Same package | Subclass | Other packages |
|---|---|---|---|---|
| private | yes | no | no | no |
| default (no word) | yes | yes | no | no |
| protected | yes | yes | yes | no |
| public | yes | yes | yes | yes |
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?
| Point | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic |
| Types | Primitives and objects | Objects only (use wrappers) |
| Length | arr.length | list.size() |
| Add or remove | Not possible | add(), remove() |
| Generics | No | Yes |
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());
}
}
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");
}
Hello from a compact source file
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");
}
}
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.langis imported automatically (String,Math).- Other packages need an
import, likeimport 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.
| Type | Must handle? | Example |
|---|---|---|
| Checked | Yes, the compiler forces it | IOException, SQLException |
| Unchecked (RuntimeException) | No | NullPointerException, ArithmeticException |
| Error | No, do not catch | OutOfMemoryError |
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");
}
}
}
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));
}
}
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");
}
}
$ 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
ccreate,ffile name,eentry point (the class withmain).jar tflists 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 version | Released |
|---|---|
| Java 8 | March 2014 |
| Java 11 | September 2018 |
| Java 17 | September 2021 |
| Java 21 | September 2023 |
| Java 25 | September 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.
$ 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.
| Point | Python | Java |
|---|---|---|
| Start | Very easy, short syntax | More code to write, stricter rules |
| Typing | Dynamic (types checked when running) | Static (types checked by the compiler) |
| Speed | Slower for heavy work | Faster (JIT compiler) |
| Strong in | Data, AI, scripts, automation | Big back ends, Android, banking, large teams |
| Run | Interpreter | Compile 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?
| Point | throw | throws |
|---|---|---|
| Kind | Statement inside a method | Keyword in the method declaration |
| Use | Create and send an exception object | Warn that the method can throw exceptions |
| Followed by | One exception object | One 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());
}
}
}
Age ok
Caught: Age can not be negative
See Java Exceptions.
- « Previous
- 1
- 2