Skip to main content

30+ Java Basic Interview Questions with Answers, Explanations & Use Cases

 

Basic Java Interview Questions for Freshers

🟢 30 Basic Java Interview Questions for Freshers (with Examples & Use Cases)

If you are preparing for interviews in service-based companies like TCS, Infosys, Accenture, Wipro, Tech Mahindra, these are the most commonly asked Java interview questions for freshers (<1 year experience).

Each question has:
✅ Simple explanation
✅ Real-life use case
✅ Small code example


1. What are the main features of Java?

Answer:

  • Object-Oriented

  • Platform Independent

  • Simple, Secure, Robust

  • Multithreaded & Portable

👉 Use Case: Run same .class file on Windows & Linux.

public class HelloWorld { public static void main(String[] args) { System.out.println("Java is platform independent!"); } }

2. What is JVM, JRE, and JDK?

Answer:

  • JVM: Runs Java bytecode.

  • JRE: Libraries + JVM.

  • JDK: JRE + tools (javac, debugger).

// javac HelloWorld.java → JDK compiles to bytecode // java HelloWorld → JRE runs program inside JVM

3. Why is Java platform independent?

Because Java code is compiled into bytecode, which runs on any JVM.

class Demo { public static void main(String[] args) { System.out.println("Runs anywhere with JVM!"); } }

4. Difference between == and equals()?

  • == → compares memory reference.

  • equals() → compares values.

String s1 = new String("Java"); String s2 = new String("Java"); System.out.println(s1 == s2); // false System.out.println(s1.equals(s2)); // true

5. Difference between Primitive and Wrapper?

  • Primitive → int, char (not objects).

  • Wrapper → Integer, Character (objects with utilities).

int a = 5; // primitive Integer b = Integer.valueOf(5); // wrapper System.out.println(b.toString()); // wrapper provides methods

6. What are Constructors?

Special method used to initialize objects.

class Student { String name; Student(String n) { name = n; } } public class Test { public static void main(String[] args) { Student s = new Student("Dip"); System.out.println(s.name); } }

7. Difference between Overloading and Overriding?

  • Overloading: Same method, different parameters.

  • Overriding: Subclass changes parent method.

// Overloading class MathUtil { int add(int a, int b) { return a+b; } double add(double a, double b) { return a+b; } } // Overriding class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } }

8. What is Inheritance?

One class acquires properties of another.

class Animal { void eat() { System.out.println("eating"); } } class Dog extends Animal { void bark() { System.out.println("barking"); } }

9. What is Polymorphism?

One method behaves differently depending on context.

class Animal { void sound() { System.out.println("Generic sound"); } } class Cat extends Animal { void sound() { System.out.println("Meow"); } } Animal a = new Cat(); a.sound(); // "Meow"

10. What is Abstraction?

Hiding implementation, showing only behavior.

abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("Drawing Circle"); } }

11. What is Encapsulation?

Binding variables & methods, restricting direct access.

class Bank { private int balance = 1000; public int getBalance() { return balance; } public void deposit(int amt) { balance += amt; } }

12. Difference between Abstract Class & Interface?

  • Abstract: Can have abstract + concrete methods.

  • Interface: Only abstract (till Java 7), can have default/static (Java 8+).

interface Vehicle { void drive(); } class Car implements Vehicle { public void drive() { System.out.println("Driving"); } }

13. What are Access Modifiers?

  • public, private, protected, default.

class Example { private int a = 5; // only inside class public int b = 10; // accessible everywhere }

14. Difference between final, finally, finalize()?

  • final: Constant/prevent inheritance.

  • finally: Executes after try-catch.

  • finalize(): Cleanup before GC.

final int x = 10; // cannot be changed try { int a=5/0; } catch(Exception e) { } finally { System.out.println("Always runs"); }

15. Difference between String, StringBuffer, StringBuilder?

  • String → Immutable.

  • StringBuffer → Mutable + thread-safe.

  • StringBuilder → Mutable + fast.

StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb); // Hello World

16. What are Static Variables & Methods?

Shared by all objects, belongs to class.

class Counter { static int count=0; Counter() { count++; } }

17. Use of this keyword?

Refers to current object.

class Student { String name; Student(String name) { this.name = name; } }

18. Checked vs Unchecked Exceptions?

  • Checked: Must be handled (IOException).

  • Unchecked: Runtime (NullPointerException).

try { FileReader f = new FileReader("abc.txt"); // checked } catch (IOException e) { e.printStackTrace(); }

19. What is Garbage Collection?

Automatic memory management.

public class GCExample { public void finalize() { System.out.println("Object destroyed"); } public static void main(String[] args) { GCExample obj = new GCExample(); obj = null; System.gc(); } }

20. What are Threads?

Lightweight processes for multitasking.

class MyThread extends Thread { public void run() { System.out.println("Running..."); } } new MyThread().start();

21. What is Synchronization?

Ensures one thread at a time.

class Table { synchronized void printTable(int n) { for(int i=1;i<=5;i++) System.out.println(n*i); } }

22. Difference between Array & ArrayList?

  • Array: Fixed size.

  • ArrayList: Dynamic.

int[] arr = {1,2,3}; ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2);

23. Difference between HashMap & Hashtable?

  • HashMap → Not synchronized, allows null.

  • Hashtable → Synchronized, no null keys.

Map<Integer,String> map = new HashMap<>(); map.put(1,"A");

24. Difference between Iterator & ListIterator?

  • Iterator → one direction.

  • ListIterator → both directions.

List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); ListIterator<String> it = list.listIterator(); while(it.hasNext()) System.out.println(it.next());

25. Difference between throw & throws?

  • throw → used to throw exception.

  • throws → declares exceptions in method.

void myMethod() throws IOException { throw new IOException("File error"); }

26. Compile-time vs Runtime Polymorphism?

  • Compile-time: Overloading.

  • Runtime: Overriding.

class Parent { void show() { System.out.println("Parent"); } } class Child extends Parent { void show() { System.out.println("Child"); } }

27. What are Packages?

Collection of related classes.

package mypack; public class Demo { }

28. Why public static void main(String[] args)?

  • public: accessible

  • static: no object needed

  • void: no return

  • main: entry point


29. Heap vs Stack Memory?

  • Stack: stores method calls & local vars.

  • Heap: stores objects.


30. Process vs Thread?

  • Process: Independent, own memory.

  • Thread: Part of process, shared memory.

class Task extends Thread { public void run() { System.out.println("Thread running"); } }


Comments

Popular posts from this blog

25+ Spring Data JPA Interview Questions with Answers, Explanations & Use Cases

  📘 Spring Data JPA Interview Questions (with Answers, Explanations & Use Cases) 1. What is JPA and how is it related to Spring Data JPA? Answer: JPA (Java Persistence API) is a Java specification for managing relational data. Spring Data JPA is a part of Spring Data that simplifies JPA usage by reducing boilerplate code. Use Case: Persisting Java objects (like User ) to a relational database without writing SQL. 2. What are the key annotations used in JPA? Answer: @Entity , @Table , @Id , @GeneratedValue , @Column , @ManyToOne , @OneToMany , etc. Explanation: These annotations map Java objects to database tables and relationships. Use Case: Creating a User entity with an auto-generated ID and fields mapped to table columns. 3. What is the difference between JPA and Hibernate? Answer: JPA is a specification; Hibernate is an implementation of that specification. Use Case: Using Hibernate as the default JPA provider in Spring Boot. 4. How do you define a p...

How to Send Emails in Spring Boot Using SMTP (With and Without Attachments)

Sending emails is a common requirement in modern web applications — for things like user registration, password resets, or notifications. In this tutorial, we’ll walk through how to send emails in a Spring Boot application using SMTP , specifically with Gmail’s SMTP server , and demonstrate how to send both plain emails and emails with attachments . 📺 Video Demo If you prefer watching over reading, here’s a full demo of this tutorial in action: 📁 GitHub Repo  Want the complete working code? Clone the GitHub link provided below which contains all the source code. Source Code GitHub Link: https://github.com/TheDipDeveloper/Spring-Boot-Sending-Email 🧰 Prerequisites Java 17 or above Maven Spring Boot 3.x A Gmail account  🚀 Step 1: Add Spring Boot Mail Dependency First, add all the required dependency on pom.xml file < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring...

Create a Real-World Banking System with Spring Boot 3, JPA, MySQL & Postman

Are you looking to build a real-world project using Spring Boot and MySQL? In this tutorial, we'll walk you through creating a complete Banking Service REST API that supports full CRUD operations, money transfers, deposits, and withdrawals. Whether you're preparing for interviews or enhancing your portfolio, this hands-on project will give you practical experience with Spring Boot 3, Spring Data JPA, and RESTful API design. In this post, you'll learn how to build a Banking Service REST API using: ✅ Spring Boot 3.x ✅ Java 17 ✅ MySQL ✅ Postman for API testing ✅ IntelliJ IDEA ✅ GitHub Repo : https://github.com/TheDipDeveloper/Banking-Service-Application-REST-Api By the end, you'll have a complete backend application that supports: Creating bank accounts Fetching account data Deposits and withdrawals Transferring funds between accounts Deleting accounts 🛠️ Tech Stack Java 17 Spring Boot 3.x Spring Data JPA MySQL Lombok ...