0% found this document useful (0 votes)
4 views26 pages

Unit 05 Java OOP

The document discusses key concepts in data structures, specifically stacks and queues, and introduces the Java Stream API and Spring Boot framework. It covers the differences between Insertion and Deletion methods, Stream API operations, and the advantages of Spring Boot over traditional frameworks. Additionally, it provides examples of CRUD operations using Spring Boot and explains Dependency Injection in the context of Spring.
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)
4 views26 pages

Unit 05 Java OOP

The document discusses key concepts in data structures, specifically stacks and queues, and introduces the Java Stream API and Spring Boot framework. It covers the differences between Insertion and Deletion methods, Stream API operations, and the advantages of Spring Boot over traditional frameworks. Additionally, it provides examples of CRUD operations using Spring Boot and explains Dependency Injection in the context of Spring.
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

Insertion and deletion occur from same end Insertion and deletion occur from different ends

Uses push() and pop() methods Uses enqueue and dequeue operations

Example: Stack of books Example: Queue at ticket counter

Example

●​ Stack → Last inserted element is removed first.


●​ Queue → First inserted element is removed first.

Conclusion:

Short conceptual question​


Basic DS understanding required

UNIT-05

Topic–01 – STREAM API (TOP PRIORITY – BOTH YEARS)


Asked in:

• 2023–24​
• 2024–25

Questions:

Q1. “Explain Java stream API and its applications. Describe Intermediate and
terminal operations with suitable examples. Write a program to print the sum of all
even numbers from an ArrayList containing all integers from 1 to 10.”​
→ 2023–24 | Section C | 7 Marks | Theory + Program

Answer:

Introduction
Stream API was introduced in Java 8 to process collections of data efficiently. It supports functional-style
operations and reduces code complexity.

A stream does not store data; it processes data from collections such as List, Set, etc.
Applications of Stream API
1.​ Filtering data
2.​ Sorting elements
3.​ Performing mathematical operations
4.​ Reducing code complexity
5.​ Supporting functional programming

Types of Stream Operations

1. Intermediate Operations
Intermediate operations transform a stream into another stream. They are lazy in nature.

Examples

●​ filter()
●​ map()
●​ sorted()

Example

[Link]().filter(x -> x > 5);

2. Terminal Operations
Terminal operations produce the final result and terminate the stream.

Examples

●​ forEach()
●​ collect()
●​ reduce()
●​ count()

Example

[Link]().forEach([Link]::println);

Program to Print Sum of Even Numbers from 1 to 10


import [Link];
class Demo

public static void main(String args[])

ArrayList<Integer> list = new ArrayList<Integer>();

for(int i=1; i<=10; i++)

[Link](i);

int sum = [Link]()

.filter(x -> x % 2 == 0)

.mapToInt(x -> x)

.sum();

[Link]("Sum of Even Numbers = " + sum);

Output
Sum of Even Numbers = 30

Explanation
1.​ stream() converts collection into stream.
2.​ filter() selects even numbers.
3.​ mapToInt() converts stream into integer stream.
4.​ sum() calculates total sum.
Q2. “What is the Stream API in Java? How does it help in functional-style
operations?”​
→ 2024–25 | Section B | 7 Marks | Theory

Answer:

Introduction
Stream API was introduced in Java 8 to process collections of data efficiently. It supports functional
programming and allows operations on collections in a simple and readable way.

A stream is a sequence of elements used for processing data from collections such as List, Set, etc.

Stream API in Java


The Stream API provides methods to perform operations such as:

●​ Filtering
●​ Sorting
●​ Mapping
●​ Searching
●​ Aggregation

Streams do not store data; they only process data.

Syntax

[Link]();

Features of Stream API


1.​ Reduces code complexity
2.​ Supports functional programming
3.​ Improves readability
4.​ Enables parallel processing
5.​ Provides efficient data processing

Functional-Style Operations in Stream API


Functional-style operations are operations performed using lambda expressions and functional interfaces.

Stream API supports these operations using methods like:

●​ filter()
●​ map()
●​ sorted()
●​ reduce()
●​ forEach()

Types of Stream Operations

1. Intermediate Operations
These operations return another stream and are lazy in nature.

Examples

●​ filter()
●​ map()
●​ sorted()

Example

[Link]().filter(x -> x > 5);

2. Terminal Operations
These operations produce the final result.

Examples

●​ forEach()
●​ collect()
●​ count()
●​ reduce()

Example

[Link]().forEach([Link]::println);

Example of Stream API


import [Link];

import [Link];
class Demo

public static void main(String args[])

List<Integer> list = [Link](1,2,3,4,5,6);

[Link]()

.filter(x -> x % 2 == 0)

.forEach([Link]::println);

Output
2

Conclusion:

MOST IMPORTANT UNIT–05 TOPIC​


Asked in both years (100%)​
Focus on:

●​ Intermediate vs Terminal operations


●​ Functional programming style
●​ Program using streams

Topic–02 – SPRING BOOT (VERY HIGH PRIORITY)


Asked in:

• 2023–24​
• 2024–25

Questions:

Q1. “Describe following: Spring boot framework and its benefits”​


→ 2023–24 | Section C | 7 Marks (part question) | Theory

Answer:

Spring Boot Framework and its Benefits

Introduction
Spring Boot is a Java-based framework used to develop stand-alone and production-ready Spring applications
easily. It is built on top of the Spring Framework and reduces configuration complexity.

Spring Boot was introduced to simplify the development of Spring applications.

Spring Boot Framework


Spring Boot provides:

●​ Auto configuration
●​ Embedded servers
●​ Ready-to-use features
●​ Faster application development

It helps developers create web applications with minimal configuration.

Features of Spring Boot


1.​ Auto configuration support
2.​ Embedded Tomcat server
3.​ Simplified dependency management
4.​ Production-ready applications
5.​ Faster development process

Architecture of Spring Boot


Benefits of Spring Boot
1.​ Reduces XML configuration
2.​ Simplifies application setup
3.​ Provides embedded servers
4.​ Easy dependency management
5.​ Faster development and deployment
6.​ Supports microservices architecture
7.​ Production-ready features available

Example of Spring Boot Application


import [Link];

import [Link];

@SpringBootApplication

class DemoApplication

public static void main(String args[])

[Link]([Link], args);

}
Q2. “What is Spring Boot? How is it different from the traditional Spring
framework?”​
→ 2024–25 | Section B | 7 Marks | Theory

Answer:

Introduction
Spring Boot is a Java-based framework built on top of the Spring Framework. It is used to create stand-alone
and production-ready applications with minimal configuration.

Spring Boot simplifies the development process by providing auto configuration and embedded servers.

Spring Boot
Spring Boot helps developers create applications quickly without writing large amounts of XML configuration.

Features of Spring Boot

1.​ Auto configuration


2.​ Embedded Tomcat server
3.​ Simplified dependency management
4.​ Production-ready applications
5.​ Faster application development

Example of Spring Boot Application


import [Link];

import [Link];

@SpringBootApplication

class DemoApplication

public static void main(String args[])

[Link]([Link], args);
}

Difference Between Spring Boot and Traditional Spring Framework


Spring Boot Traditional Spring Framework

Minimal configuration required Requires extensive configuration

Provides auto configuration Manual configuration needed

Embedded servers available External server required

Faster development Development takes more time

Easy dependency management Dependency management is complex

Production-ready features available Additional setup required

Advantages of Spring Boot


1.​ Simplifies application development
2.​ Reduces boilerplate code
3.​ Provides embedded server support
4.​ Easy deployment
5.​ Supports microservices architecture

Q3. “Describe the structure of a basic Spring Boot application. What are the
advantages of using Spring Boot over traditional web frameworks?”​
→ 2024–25 | Section C | 7 Marks | Theory

Answer:

Introduction
Spring Boot is a framework used to develop stand-alone and production-ready Java applications easily. It
simplifies application development by reducing configuration and setup complexity.

Structure of a Basic Spring Boot Application


A basic Spring Boot application mainly contains:

1.​ Main Application Class


2.​ Controller Class
3.​ Service Layer
4.​ Repository Layer
5.​ Configuration Files

Basic Structure Diagram

Main Application Class


The main class contains the main() method and @SpringBootApplication annotation.
Example

import [Link];

import [Link];

@SpringBootApplication

class DemoApplication

public static void main(String args[])

[Link]([Link], args);

Controller Class
The controller handles client requests.

Example

import [Link];

import [Link];

@RestController

class DemoController

@GetMapping("/")

public String show()

return "Hello Spring Boot";


}

Advantages of Spring Boot Over Traditional Web Frameworks


Spring Boot Traditional Web Framework

Minimal configuration required Requires heavy configuration

Embedded server support External server required

Faster development Slower development

Auto configuration available Manual configuration needed

Easy dependency management Complex dependency management

Additional Advantages of Spring Boot


1.​ Simplifies application setup
2.​ Reduces boilerplate code
3.​ Easy deployment
4.​ Production-ready applications
5.​ Supports microservices architecture

Conclusion:

VERY FREQUENT THEORY QUESTION​


Focus areas:

●​ Definition
●​ Advantages
●​ Comparison with Spring
●​ Application structure
Topic–03 – DEPENDENCY INJECTION (SPRING CORE) (HIGH
PRIORITY)
Asked in:

• 2023–24​
• 2024–25

Questions:

Q1. “Explain the difference between Dependency Injection (DI) and Inversion of
Control (IoC) in Spring.”​
→ 2023–24 | Section B | 7 Marks | Theory

Answer:

Introduction
In Spring Framework, IoC and DI are important concepts used for loose coupling and better object management.

●​ IoC is a design principle.


●​ DI is a technique used to implement IoC.

Inversion of Control (IoC)


Inversion of Control means transferring the control of object creation and management from the programmer to
the Spring container.

The Spring container creates and manages objects automatically.

Features of IoC

1.​ Reduces dependency between classes


2.​ Improves flexibility
3.​ Managed by Spring container
4.​ Simplifies object management

Dependency Injection (DI)


Dependency Injection is a technique in which one object provides dependencies to another object.

Spring injects required objects automatically instead of creating them manually.

Types of DI

1.​ Constructor Injection


2.​ Setter Injection

Difference Between IoC and DI


IoC DI

Design principle Technique to implement IoC

Transfers control to container Injects dependencies into objects

Manages object creation Provides required objects

Implemented using DI Part of IoC mechanism

Example of Dependency Injection


class Engine

void start()

[Link]("Engine Started");

class Car
{

Engine e;

Car(Engine e)

this.e = e;

void drive()

[Link]();

Advantages of IoC and DI


1.​ Loose coupling
2.​ Better code reusability
3.​ Easier testing
4.​ Simplified maintenance
5.​ Better flexibility

Q2. “Explain the concept of Dependency Injection and its types in Spring. Develop a
simple API in Spring Boot to perform CRUD operations using a list as storage.”​
→ 2024–25 | Section C | 7 Marks | Theory + Application (Mixed)

Answer:

Introduction
Dependency Injection (DI) is an important feature of the Spring Framework used to achieve loose coupling
between classes. In DI, the Spring container provides required objects automatically instead of creating them
manually.
Dependency Injection (DI)
Dependency Injection means injecting one object into another object as a dependency.

Spring container manages object creation and injection automatically.

Types of Dependency Injection

1. Constructor Injection
Dependencies are injected through constructor.

Example

class Car

Engine e;

Car(Engine e)

this.e = e;

2. Setter Injection
Dependencies are injected using setter methods.

Example

class Car

Engine e;
void setEngine(Engine e)

this.e = e;

Advantages of Dependency Injection


1.​ Loose coupling
2.​ Better code reusability
3.​ Easy testing
4.​ Simplifies maintenance

Spring Boot CRUD API Using List Storage

Program
import [Link].*;

import [Link].*;

@RestController

class StudentController

List<String> students = new ArrayList<String>();

@GetMapping("/students")

public List<String> getStudents()

return students;

}
@PostMapping("/add/{name}")

public String addStudent(@PathVariable String name)

[Link](name);

return "Student Added";

@PutMapping("/update/{index}/{name}")

public String updateStudent(@PathVariable int index,

@PathVariable String name)

[Link](index, name);

return "Student Updated";

@DeleteMapping("/delete/{index}")

public String deleteStudent(@PathVariable int index)

[Link](index);

return "Student Deleted";

}
CRUD Operations
Operation HTTP Method

Create POST

Read GET

Update PUT

Delete DELETE

Explanation
1.​ @RestController creates REST API controller.
2.​ @GetMapping retrieves data.
3.​ @PostMapping adds data.
4.​ @PutMapping updates data.
5.​ @DeleteMapping removes data.
6.​ ArrayList is used as temporary storage.

Conclusion:

IMPORTANT CONCEPTUAL QUESTION​


Often mixed with Spring Boot / API questions​
Focus on:

●​ DI types
●​ IoC vs DI

Topic–04 – REST API / WEB SERVICES (MODERATE PRIORITY)


Asked in:

• 2024–25

Questions:
Q1. “Explain the concept of Dependency Injection and its types in Spring. Develop a
simple API in Spring Boot to perform CRUD operations using a list as storage.”​
→ 2024–25 | Section C | 7 Marks (mixed question)

Answer:

Introduction
Dependency Injection (DI) is a technique used in Spring Framework to provide objects automatically to another
object. It helps in achieving loose coupling and improves code maintainability.

Spring Boot uses DI extensively for managing application components.

Dependency Injection (DI)


Dependency Injection means injecting dependencies into a class instead of creating them manually.

The Spring container manages object creation and injection automatically.

Types of Dependency Injection

1. Constructor Injection
Dependencies are provided through constructor.

Example

class Car

Engine e;

Car(Engine e)

this.e = e;
}

2. Setter Injection
Dependencies are provided using setter methods.

Example

class Car

Engine e;

void setEngine(Engine e)

this.e = e;

Advantages of Dependency Injection


1.​ Loose coupling
2.​ Easy testing
3.​ Better maintainability
4.​ Improved code reusability

Spring Boot CRUD API Using List Storage

Program
import [Link].*;

import [Link].*;
@RestController

class StudentController

List<String> students = new ArrayList<String>();

@GetMapping("/students")

public List<String> getStudents()

return students;

@PostMapping("/add/{name}")

public String addStudent(@PathVariable String name)

[Link](name);

return "Student Added";

@PutMapping("/update/{index}/{name}")

public String updateStudent(@PathVariable int index,

@PathVariable String name)

[Link](index, name);

return "Student Updated";


}

@DeleteMapping("/delete/{index}")

public String deleteStudent(@PathVariable int index)

[Link](index);

return "Student Deleted";

CRUD Operations
Operation HTTP Method

Create POST

Read GET

Update PUT

Delete DELETE

Explanation
1.​ @RestController creates REST API controller.
2.​ @GetMapping retrieves data.
3.​ @PostMapping adds data.
4.​ @PutMapping updates existing data.
5.​ @DeleteMapping removes data.
6.​ ArrayList is used as temporary storage.

Conclusion:
Appears as part of mixed question​
Focus on:

●​ Basic REST concepts


●​ CRUD operations

Topic–05 – GET vs POST (LOW PRIORITY)


Asked in:

• 2024–25

Questions:

Q1. “What is a GET vs POST request?”​


→ 2024–25 | Section A | 2 Marks | Conceptual

Answer:

Difference Between GET and POST Request


GET Request POST Request

Used to retrieve data from server Used to send data to server

Data is sent through URL Data is sent in request body

Less secure More secure

Limited data size Large amount of data can be sent

Faster request Slightly slower request

Example

●​ GET → Fetching webpage data


●​ POST → Submitting login or registration form
Conclusion:

Short conceptual question​


Basic web knowledge required

ALL THE VERY BEST - BITWISE LEARNING

You might also like