[Go to site: main page, start]

0% found this document useful (0 votes)
23 views5 pages

Java Interview Notes With Answers

The document provides comprehensive notes on Java interview topics, covering core concepts such as OOP, collections, exception handling, multithreading, Java 8 features, JVM, file handling, JDBC, servlets, JSP, Spring, Spring Boot, JPA/Hibernate, CRUD APIs, and frequently asked interview questions. Each section includes definitions, comparisons, and key features relevant to Java programming. The notes serve as a study guide for candidates preparing for Java-related interviews.

Uploaded by

rahmathulla42627
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Java Interview Notes With Answers

The document provides comprehensive notes on Java interview topics, covering core concepts such as OOP, collections, exception handling, multithreading, Java 8 features, JVM, file handling, JDBC, servlets, JSP, Spring, Spring Boot, JPA/Hibernate, CRUD APIs, and frequently asked interview questions. Each section includes definitions, comparisons, and key features relevant to Java programming. The notes serve as a study guide for candidates preparing for Java-related interviews.

Uploaded by

rahmathulla42627
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA INTERVIEW NOTES – FULL ANSWERS (Core + Advanced + Spring Boot)

DAY 1 — OOPS (WITH ANSWERS)

OOPS is Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, Abstraction.

Class = blueprint, Object = instance.

Encapsulation hides data using private + getters/setters.

Inheritance enables reuse using 'extends'.

Polymorphism: method overloading (compile time) and overriding (runtime).

Abstraction hides implementation using interfaces/abstract classes.

Interface vs Abstract Class:

Interface supports multiple inheritance; abstract class cannot.

DAY 2 — COLLECTIONS

List:

ArrayList – fast read, dynamic array.

LinkedList – fast insert/delete.

Set:

HashSet – unique, unordered.

LinkedHashSet – unique, ordered.

Map:

HashMap – key-value, fast.

LinkedHashMap – ordered.

Comparator vs Comparable:

Comparable uses compareTo() for natural sorting.

Comparator uses compare() for custom sorting.

DAY 3 — EXCEPTION HANDLING

Checked exceptions: compile-time (SQLException).

Unchecked: runtime (ArithmeticException).


try-catch-finally:

try holds risky code; catch handles error; finally always executes.

throw: manually throw exception.

throws: method declares exception.

Custom exception: extend Exception class.

DAY 4 — MULTITHREADING

Thread: smallest execution unit.

Create thread via:

1. extends Thread

2. implements Runnable

Synchronization prevents multiple threads from accessing a resource simultaneously.

Deadlock: two threads waiting for each other.

DAY 5 — JAVA 8 FEATURES

Lambda expressions simplify functional code: () -> {}

Functional interface contains one abstract method.

Stream API: filter, map, sorted, collect.

Optional avoids NullPointerException.

DAY 6 — JVM

JDK = JRE + JVM.

Heap stores objects; Stack stores method calls.

Garbage collector removes unused objects.

DAY 7 — FILE HANDLING

FileReader/FileWriter – text files.

BufferedReader/Writer – faster I/O.

Serialization: object → byte stream.

DAY 8 — JDBC
Steps:

1. Load driver

2. Establish connection

3. Create Statement / PreparedStatement

4. Execute query

5. Close connection

PreparedStatement prevents SQL injection.

DAY 9 — SERVLET

Servlet lifecycle: init(), service(), destroy().

doGet() for reading, doPost() for submitting.

Session management: Cookies, HttpSession, URL rewriting.

DAY 10 — JSP

Scriptlets <% %>, Expressions <%= %>, Declarations <%! %>.

JSTL simplifies loops and conditions.

MVC: Model (data), View (JSP), Controller (Servlet).

DAY 11 — SPRING

IoC (Inversion of Control) lets Spring manage objects.

Dependency Injection injects dependencies automatically.

Bean Scopes: singleton (default), prototype.

DAY 12 — SPRING BOOT

Reduces configuration with auto-configuration.

Embedded servers like Tomcat included.

Important annotations:

@RestController – REST APIs

@Service – service layer

@Repository – DB layer

@Autowired – DI
DAY 13 — JPA / HIBERNATE

@Entity marks the class as a table.

@Id is primary key.

@GeneratedValue generates IDs.

Relations:

@OneToMany, @ManyToOne, @OneToOne, @ManyToMany.

Lazy loading loads on demand; Eager loads everything.

DAY 14 — CRUD API

HTTP Methods:

GET – fetch

POST – create

PUT – update

DELETE – delete

Status codes:

200 OK, 201 Created, 400 Bad Request, 404 Not Found, 500 Server Error.

DAY 15 — MOST ASKED INTERVIEW Q/A

Q1: Why is String immutable?

A: For security, caching in String pool, and thread-safety.

Q2: HashMap internal working?

A: Uses array + linked list + red-black tree (Java 8). Key -> hash -> bucket.

Q3: Difference between HashMap & Hashtable?

A: HashMap not synchronized, faster; Hashtable synchronized, slower.

Q4: What is Spring Boot?

A: Framework built on Spring to simplify development using auto-configuration.

Q5: What is REST API?

A: Architecture using HTTP methods for CRUD.


END OF FULL NOTES.

Common questions

Powered by AI

Spring’s Dependency Injection is a cornerstone feature that supports Inversion of Control (IoC). It allows the framework to handle the instantiation and life-cycle of objects, thereby inverting the control flow compared to traditional production patterns. Dependency Injection separates the creation of object dependencies from business logic, leading to loosely coupled code and easier unit testing. IoC is achieved by enabling objects to acquire their dependencies at runtime rather than compile-time setup, which fosters more flexible and reusable components .

The Servlet lifecycle facilitates web application development by providing a structured approach to request processing and resource management. It comprises three key methods: init(), service(), and destroy(). The init() method initializes the servlet and is executed once when the servlet is loaded. The service() method processes client requests and is invoked for each request, allowing the servlet to read data from the client's request and create responses accordingly. The destroy() method is called once the servlet is being taken out of service, allowing for cleanup of resources. This lifecycle ensures efficient handling of requests, session management, and resource allocation, which are essential for robust web application solutions .

Encapsulation in Object-Oriented Programming works by restricting direct access to some of an object's components and can be achieved using access modifiers. It involves wrapping the data (variables) and the code acting on the data (methods) together as a single unit. The data is not accessible directly; one must use getter and setter methods. The benefits include enhanced security of data by hiding it from outside interference and misuse, improved modularity by hiding the complexity of an object's operations from other classes, and flexibility and maintainability of code .

Within the Java Virtual Machine (JVM), the garbage collector functions to automatically remove objects that are no longer in use to free up resources; this is crucial for long-running applications. It primarily operates in the heap memory, where it identifies and discards unreachable objects, i.e., objects that cannot be accessed in any possible scenario. This process helps in managing memory efficiently by reclaiming memory consumed by abandoned objects and thereby preventing memory leaks .

Java's HashMap manages collisions using a combination of arrays, linked lists, and a red-black tree. Initially, the HashMap uses an array of linked lists where each bucket in the array corresponds to a hash code. When a collision occurs (multiple keys hash to the same bucket), these entries are stored in a linked list at that bucket. Starting with Java 8, if the number of entries in a bucket exceeds a certain threshold (default is 8), that linked list is transformed into a red-black tree, which provides faster search times, ensuring O(log n) performance compared to O(n) in linked lists for large buckets .

Java's abstract classes differ from interfaces primarily in their flexibility and usage. An abstract class can contain both complete and incomplete members (i.e., methods with or without a body), whereas an interface can typically only contain method signatures (Java 8 and onwards allow default and static methods). Abstract classes can maintain state via instance variables, while interfaces cannot. This affects multiple inheritance because interfaces allow a form of it as a class can implement multiple interfaces, whereas a class can only extend one abstract class - adhering to single inheritance. This distinction impacts design decisions when a class needs to inherit behavior from multiple sources .

The Stream API in Java 8 enhances data processing by providing a high-level abstraction for operations on collections of objects. It allows developers to write concise, efficient, and parallelizable code to perform bulk operations on data sets, such as filtering, mapping, and reducing. Streams facilitate a functional programming approach, enabling developers to focus on the 'what' instead of the 'how' by abstracting away the process of iteration. This leads to more readable and maintainable code. Additionally, the Stream API can handle lazy computation, where operations are evaluated only as needed, optimizing performance .

The difference between 'implements Runnable' and 'extends Thread' pertains to class hierarchy and scalability. 'implements Runnable' allows a class to extend another class because it does not require the class to be a child of Thread, so it can maintain a flexible class hierarchy. This approach is preferred for large projects as it decouples the task from the execution mechanism, making it more scalable and manageable . 'extends Thread', on the other hand, should be used when you want to override specific methods of the Thread class as it creates a subclass of Thread, tightly coupling the task with Thread, which is less flexible in inheritance scenarios .

Hibernate implements object-relational mapping (ORM) by mapping Java classes to database tables and Java data types to SQL data types, allowing for seamless data manipulation and retrieval using object-centric logic. Annotations such as @Entity and @Id play crucial roles. @Entity marks a class that should be persisted to a database, and @Id defines the primary key of the entity, essential for identifying specific records. These annotations simplify configuration and help in maintaining cleaner, more maintainable code, leveraging Hibernate’s capabilities of transparent caching, lazy loading, and optimized performance through intelligent fetching strategies .

A Checked Exception in Java is one that is checked at compile time, which means the programmer must handle these exceptions to make the program compile. They often indicate recoverable conditions and examples include SQLException and IOException . In contrast, an Unchecked Exception is not checked at compile time. These are typically runtime exceptions such as ArithmeticException and NullPointerException that usually signify programming defects such as logic errors or improper use of an API .

You might also like