� UNIT 3 – Java 8 Features
1. What is a Lambda Expression in Java?
Lambda Expressions were introduced in Java 8 as a way to enable
functional programming in Java. A lambda expression is a short block of
code which takes in parameters and returns a value. Lambda expressions
can be used primarily to define the inline implementation of a functional
interface (an interface with a single abstract method).
Syntax:
java
CopyEdit
(parameters) -> expression
Or for multi-line:
java
CopyEdit
(parameters) -> {
// statements
}
Example:
java
CopyEdit
// Before Java 8
Runnable r = new Runnable() {
public void run() {
[Link]("Running thread");
}
};
// With Lambda
Runnable r = () -> [Link]("Running thread");
Advantages:
Reduces boilerplate code.
Enhances the readability of code.
Supports functional programming style.
2. Define Stream API.
The Stream API, introduced in Java 8, is used to process collections
(like Lists and Sets) in a functional style. It allows you to perform
complex operations such as filtering, mapping, and reducing data using a
simple, fluent syntax.
Example:
java
CopyEdit
List<String> list = [Link]("Java", "Python", "C++",
"JavaScript");
[Link]()
.filter(str -> [Link]("J"))
.forEach([Link]::println);
Features:
Lazy execution: operations are only performed when a terminal
operation is invoked.
Supports both sequential and parallel processing.
Doesn't modify the original data source.
Main Components:
Intermediate Operations: map, filter, sorted
Terminal Operations: collect, forEach, reduce
3. What is the use of the default method in interfaces?
Before Java 8, interfaces could only declare methods but not implement
them. Java 8 introduced default methods, which allow interfaces to
provide a default implementation for methods.
Syntax:
java
CopyEdit
interface MyInterface {
default void show() {
[Link]("This is a default method");
}
}
Why useful:
Allows adding new methods to interfaces without breaking the
existing implementation.
Enables backward compatibility with older codebases.
Supports code reuse within interfaces.
4. How do method references simplify code in Java?
Method references are a shorthand notation of lambda expressions to
call a method directly by its name. It makes the code cleaner and more
readable.
Types:
1. Reference to a static method – ClassName::staticMethod
2. Reference to an instance method – instance::instanceMethod
3. Reference to a constructor – ClassName::new
Example:
java
CopyEdit
// Using Lambda
[Link](s -> [Link](s));
// Using Method Reference
[Link]([Link]::println);
Advantages:
Shorter syntax
Better readability
Reusability of existing methods
5. Discuss Text Block in Java.
Text blocks are a feature introduced in Java 13 (as a preview) and
officially in Java 15. They allow developers to declare multi-line strings
in a readable and concise way using triple quotes ( """).
Example:
java
CopyEdit
String html = """
<html>
<body>
<h1>Hello, World</h1>
</body>
</html>
""";
Benefits:
Eliminates the need for \n and string concatenation.
Preserves formatting.
Improves readability for large string values such as JSON, XML,
or HTML content.
� UNIT 4 – Java Collection Framework
1. What is the Collection Framework in Java?
The Java Collection Framework is a unified architecture that provides
interfaces and classes to store, retrieve, and manipulate groups of objects
efficiently. It includes interfaces like List, Set, Map, Queue, and classes
like ArrayList, HashMap, LinkedList, etc.
Main Interfaces:
List – Ordered collection (ArrayList, LinkedList)
Set – No duplicate elements (HashSet, TreeSet)
Map – Key-value pairs (HashMap, TreeMap)
Queue – FIFO (LinkedList, PriorityQueue)
Advantages:
Reusable data structures
Standardized operations like sorting, searching
Reduces development time and effort
2. Mention Two Uses of ArrayList
ArrayList is a resizable array implementation of the List interface.
Uses:
1. Dynamic Storage – Automatically grows or shrinks as needed,
unlike arrays.
2. Index-based Access – Allows fast random access to elements via
index.
Example:
java
CopyEdit
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]([Link](0)); // Apple
3. Define HashMap
HashMap is a part of Java's Collection Framework and stores data in key-
value pairs. It uses a hashing technique to allow constant-time
performance for get and put operations.
Example:
java
CopyEdit
HashMap<Integer, String> map = new HashMap<>();
[Link](1, "Java");
[Link](2, "Python");
[Link]([Link](1)); // Output: Java
Features:
Keys are unique; values can be duplicated.
Doesn't maintain order.
Allows one null key and multiple null values.
4. Describe Queue Interface
The Queue interface in Java represents a collection designed for holding
elements prior to processing. It follows the FIFO (First-In-First-Out)
principle.
Implementations:
LinkedList
PriorityQueue
ArrayDeque
Example:
java
CopyEdit
Queue<String> queue = new LinkedList<>();
[Link]("A");
[Link]("B");
[Link]([Link]()); // Output: A
Common Methods:
add(), remove(), peek(), poll()
5. What is the Iterator Interface?
The Iterator interface provides a way to traverse a collection
sequentially without exposing its underlying representation.
Example:
java
CopyEdit
Iterator<String> it = [Link]();
while ([Link]()) {
[Link]([Link]());
}
Main Methods:
hasNext() – checks if next element exists
next() – returns the next element
remove() – removes the current element (optional)
Use Case:
Useful for safely removing elements during iteration.
� UNIT 5 – Spring Framework & Spring Boot
1. What is Dependency Injection in Spring?
Dependency Injection (DI) is a core feature of Spring. It refers to the
process of injecting object dependencies externally rather than the object
creating them itself. This helps in creating loosely coupled and easily
testable code.
Types:
Constructor Injection
Setter Injection
Example:
java
CopyEdit
@Component
class Engine {}
@Component
class Car {
private final Engine engine;
@Autowired
public Car(Engine engine) {
[Link] = engine;
}
}
Advantages:
Promotes loose coupling
Enhances code testability and reusability
Simplifies unit testing with mock objects
2. Name any two RESTful HTTP methods
RESTful APIs use standard HTTP methods to perform CRUD
operations.
Common HTTP methods:
GET – Retrieve data from the server
POST – Submit new data to the server
Other methods: PUT (update), DELETE (delete), PATCH (partial
update)
3. Define @RestController
@RestController is a Spring annotation that marks a class as a web
controller where every method returns a domain object rather than a
view. It is a combination of @Controller and @ResponseBody.
Example:
java
CopyEdit
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
Purpose:
To build RESTful web services
Automatically converts Java objects into JSON or XML
4. What is Spring Framework?
Spring Framework is a powerful, lightweight, and open-source Java
application framework that provides infrastructure support for building
Java applications.
Key Features:
Inversion of Control (IoC)
Aspect-Oriented Programming (AOP)
MVC Web Framework
Data Access and Transaction Management
Modules in Spring:
Spring Core
Spring AOP
Spring MVC
Spring Data
Spring Security
5. What is Spring Boot?
Spring Boot is a part of the Spring ecosystem designed to simplify the
development of stand-alone, production-grade Spring applications with
minimal configuration.
Key Features:
Auto-configuration of Spring beans
Embedded servers (Tomcat, Jetty)
Opinionated 'starter' dependencies
Production-ready features like health checks, metrics via Spring
Boot Actuator
Example:
java
CopyEdit
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
[Link]([Link], args);
}
}
Benefits:
Rapid application development
Easy integration with cloud and microservices
Simplifies deployment with minimal XML or configuration files