This lesson covers important Java interview questions for beginners and intermediate learners. These questions help students prepare for technical interviews, practical exams, and entry-level developer jobs.
Java is a high-level, class-based programming language used to develop desktop applications, web applications, backend systems, and many other types of software.
Important features include:
| Term | Meaning |
|---|---|
| JDK | Java Development Kit; provides development tools such as the compiler. |
| JRE | Java Runtime Environment; traditionally describes the runtime components needed to run Java applications. |
| JVM | Java Virtual Machine; executes Java bytecode. |
Modern Java distributions may package runtime components differently, and a separate JRE download is not always provided.
Java source code is compiled into bytecode. A compatible JVM on a supported operating system can execute that bytecode.
Source Code (.java)
|
v
Java Compiler (javac)
|
v
Bytecode (.class)
|
v
Compatible JVM
|
v
Program Execution
This is why Java is commonly described as "Write Once, Run Anywhere", provided a compatible runtime and required dependencies are available.
A class defines fields and methods. An object is an instance of a class.
class Student {
String name = "Rahul";
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
Output:
Rahul
Inheritance allows a class to acquire accessible fields and
methods from another class using the extends keyword.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output:
Animal is eating
Dog is barking
Polymorphism means that the same method name or operation can represent different behavior.
Java supports method overloading and method overriding.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(2, 3));
System.out.println(c.add(2, 3, 4));
}
}
Output:
5
9
This is method overloading because the method name is the same but the parameter lists differ.
| Overloading | Overriding |
|---|---|
| Methods have the same name but different parameter lists. | A subclass provides a new implementation of an inherited instance method. |
| Usually occurs within the same class. | Occurs between a superclass and subclass. |
| Resolved using the method's parameter types and arguments at compile time. | Overridden instance-method behavior is selected dynamically at runtime. |
A constructor initializes a new object. Its name matches the class name, and it has no return type.
class Student {
String name;
Student(String studentName) {
name = studentName;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Amit");
System.out.println(s.name);
}
}
Output:
Amit
| Interface | Abstract Class |
|---|---|
| Defines a contract that implementing classes agree to follow. | Provides a base class that can contain shared implementation and state. |
| A class can implement multiple interfaces. | A class can extend only one class. |
| Can contain abstract, default, static, and private methods, subject to Java language rules. | Can contain abstract and concrete methods, constructors, and instance fields. |
Encapsulation combines data and related methods in a class and controls how the data is accessed.
class Student {
private int age;
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setAge(20);
System.out.println(s.getAge());
}
}
Output:
20
For object references, == checks whether two
references point to the same object. The equals()
method can compare logical content when the class implements
it appropriately.
public class Main {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
Output:
false
true
Exception handling allows a program to handle certain errors
or exceptional conditions using try,
catch, finally, throw,
and throws.
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}
Output:
Cannot divide by zero
IOException.
RuntimeException. Example:
NullPointerException.
A String represents a sequence of characters.
String objects are immutable, meaning their contents cannot
be changed after creation.
public class Main {
public static void main(String[] args) {
String name = "Java";
System.out.println(name.length());
System.out.println(name.toUpperCase());
}
}
Output:
4
JAVA
| Class | Description |
|---|---|
| String | Immutable character sequence. |
| StringBuilder | Mutable character sequence without synchronized methods; often suitable for single-threaded string modification. |
| StringBuffer | Mutable character sequence with synchronized methods. |
An array stores a fixed number of values of the same type. Its elements are accessed using zero-based indexes.
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]);
System.out.println(numbers.length);
}
}
Output:
10
3
The Collections Framework provides interfaces and classes for storing and working with groups of objects.
List: An ordered collection that can contain duplicates.Set: A collection that does not allow duplicate elements.Queue: A collection designed for holding elements before processing.Map: Stores key-value mappings and is separate from the Collection interface hierarchy.import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Amit");
System.out.println(names);
}
}
Output:
[Rahul, Amit]
Multithreading allows a program to execute multiple threads of work. Threads share the process's memory, so shared data may require synchronization.
public class Main {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("Thread is running");
});
t.start();
}
}
Possible Output:
Thread is running
The call to start() schedules the thread to run
independently. Calling run() directly would
execute the method as an ordinary method call instead.
AutoCloseable, such as
file streams and database connections.
The static keyword declares a member associated
with the class rather than with each individual object.
class Student {
static String school = "Soopro Pathshala";
}
public class Main {
public static void main(String[] args) {
System.out.println(Student.school);
}
}
Output:
Soopro Pathshala
The this keyword refers to the current object.
It is often used to distinguish an instance field from a
parameter with the same name.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Here, this.name refers to the object's field,
while name refers to the constructor parameter.
The super keyword refers to superclass members
and can be used to invoke a superclass constructor.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
Garbage collection automatically reclaims heap memory used by objects that are no longer reachable, subject to the JVM's implementation and execution decisions.
Java developers normally do not manually free object memory. However, they should still close files, sockets, and database resources when finished with them.
A package groups related classes and interfaces. It helps organize code and avoid naming conflicts.
package myapp;
public class Student {
public void display() {
System.out.println("Student class");
}
}
The package declaration must appear before imports and class declarations in a source file.
JDBC stands for Java Database Connectivity. It provides APIs that Java applications use to connect to databases and execute SQL.
Common JDBC interfaces and classes include:
ConnectionPreparedStatementStatementResultSetDriverManagerPreparedStatement for SQL statements containing
user-provided values. It supports parameter binding and helps
prevent SQL injection when used correctly.
A lambda expression provides a concise implementation of a functional interface, which has one abstract method.
interface Greeting {
void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = () -> System.out.println("Hello Java");
g.sayHello();
}
}
Output:
Hello Java
| ArrayList | LinkedList |
|---|---|
| Uses a resizable array internally. | Uses a doubly linked list internally. |
| Usually provides fast indexed access. | Indexed access requires traversing the list. |
| Inserting or removing elements in the middle can require shifting elements. | Insertion or removal can be efficient once the position is reached, but finding that position may require traversal. |
An enum defines a fixed set of named constants.
enum Day {
MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Main {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println(today);
}
}
Output:
MONDAY
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output:
1
2
4
5
Practice writing programs for the following questions:
Choose the correct answer for each question.