ST3 Last Year
ST3 Last Year
Printed Pages:
Session: 2023-24
Semester:
Course Code:BCS 403 Roll No.:
Course Name: OOPS with Java Time: 1 Hr 30 Mins
Maximum Marks: 40
Instructions:
1. Attempt all sections.
2. If require any missing data, then choose suitably.
Q. No. Question
1 Attempt ANY ONE part from the following
What is the difference between ArrayList and Vector?
ArrayList Vector
Comparable Comparator
5) We can sort the list elements of We can sort the list elements of
Comparable type Comparator type
by Collections.sort(List) method. by Collections.sort(List,
Comparator) method.
Write an overview of the Collection Framework hierarchy, including the main interfaces and classes.
Hierarchy of Collection Framework
b)
1. Collection Interface: The root interface of the collection hierarchy, which represents a group of
objects. Key subinterfaces include List, Set, and Queue.
2. List Interface: Extends Collection and represents an ordered collection (sequence). Key
implementations are ArrayList, LinkedList, and Vector.
3. Set Interface: Extends Collection and represents a collection that does not allow duplicate
elements. Key implementations include HashSet, LinkedHashSet, and TreeSet.
4. Queue Interface: Extends Collection and represents a collection designed for holding elements
prior to processing. Key implementations are LinkedList, PriorityQueue, and ArrayDeque.
5. Map Interface: Represents a collection of key-value pairs and does not extend the Collection
interface. Key implementations include HashMap, LinkedHashMap, TreeMap, and Hashtable.
import java.util.Stack;
public class StackExample {
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
Explain the Map interface in Java. Implement a program that uses HashMap to store and retrieve
employee information (id, name, department) efficiently. Demonstrate the use of key-value pairs.
Solution:
Solution: The Map interface in Java represents a collection of key-value pairs, where each key
maps to exactly one value. It does not allow duplicate keys, but values can be duplicated. The
Map interface provides methods to add, remove, and access elements based on keys.
put(K key, V value): Associates the specified value with the specified key in the map.
get(Object key): Returns the value to which the specified key is mapped, or null if the
map contains no mapping for the key.
remove(Object key): Removes the mapping for a key from the map if it is present.
containsKey(Object key): Returns true if the map contains a mapping for the
specified key.
containsValue(Object value): Returns true if the map maps one or more keys to the
specified value.
b) keySet(): Returns a Set view of the keys contained in this map.
values(): Returns a Collection view of the values contained in this map.
entrySet(): Returns a Set view of the mappings contained in this map.
class Employee {
private int id;
private String name;
private String department;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
'}';
}
}
// Remove an employee by id
employeeMap.remove(102);
System.out.println("\nAfter removing employee with ID 102:");
for (Map.Entry<Integer, Employee> entry : employeeMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
In Spring, the lifecycle of a bean is managed by the Spring IoC container. The lifecycle
includes several stages, such as instantiation, dependency injection, initialization, and
destruction. The diagram you provided outlines these stages, including custom initialization
and destruction methods.
@Component
public class MyBean implements InitializingBean, DisposableBean {
public MyBean() {
System.out.println("Bean Instantiated");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Custom Init Method");
}
@Override
public void destroy() throws Exception {
System.out.println("Custom Destroy Method");
}
@PostConstruct
public void postConstruct() {
System.out.println("PostConstruct Method");
}
@PreDestroy
public void preDestroy() {
System.out.println("PreDestroy Method");
}
}
Explain REST API with a suitable example.
Solution:
Definition
Key Characteristics
1. Stateless: Each request from a client to the server must contain all the information the
server needs to fulfill the request. The server does not store client context between
requests.
2. Client-Server Architecture: The client and server are separate entities. The client sends
requests and the server responds, enabling a clear separation of concerns.
3. Cacheable: Responses from the server can be marked as cacheable to improve
performance.
4. Uniform Interface: Resources are identified using URIs (Uniform Resource Identifiers), and
standard HTTP methods (GET, POST, PUT, DELETE) are used for operations.
b) 5. Layered System: The architecture can have multiple layers, improving scalability and
security.
1. Scalability: REST APIs are stateless, allowing servers to handle more requests efficiently.
2. Flexibility: REST APIs can be consumed by any client that understands HTTP, including
browsers, mobile devices, and other servers.
3. Maintainability: The separation of client and server concerns simplifies code maintenance
and updates.
4. Performance: Caching of responses can reduce server load and improve performance.
5. Interoperability: REST APIs can be used across different platforms and programming
languages, facilitating integration.
5 Attempt ANY ONE part from the following
Explain Spring Boot Architecture with a suitable diagram.
The Spring Boot is built on top of the core Spring framework. It is a simplified and automated
version of the spring framework. The spring boot follows a layered architecture in which each
layer communicates to other layers(Above or below in hierarchical order).
Spring Boot Layers
The spring boot consists of the following four layers:
1. Presentation Layer – Authentication & Json Translation
2. Business Layer – Business Logic, Validation & Authorization
3. Persistence Layer – Storage Logic
4. Database Layer – Actual Database
a)
Explanation:
The Client makes an HTTP request(GET, PUT, POST, etc.)
The HTTP request is forwarded to the Controller. The controller maps the request.
It processes the handles and calls the server logic.
The business logic is performed in the Service layer. The spring boot performs all the
logic over the data of the database which is mapped to the spring boot model class
through Java Persistence Library(JPA).
The JSP page is returned as Response from the controller.
Describe the Spring IoC container. What are its main responsibilities?
The Spring framework can be considered as a collection of sub-frameworks, also referred to as layers,
such as Spring AOP, Spring ORM, Spring Web Flow, and Spring Web MVC. You can use any of
these modules separately while constructing a Web application. The modules may also be grouped
together to provide better functionalities in a web application.
Spring provides two types of Containers namely as follows:
1. BeanFactory Container
2. ApplicationContext Container
Here are its main responsibilities:
1. Bean Definition and Configuration
Loading Bean Definitions: The container reads configuration metadata from various sources
(XML, Java annotations, or Java code) to understand how to create and configure beans.
Bean Scope Management: It supports different scopes for beans, such as singleton (one
instance per container) and prototype (new instance every time).
4. Configuration Management
5. Event Handling
Application Events: The container supports publishing and listening to application events,
allowing beans to communicate without being tightly coupled.
7. Resource Management
Type Conversion: It handles type conversion and formatting, ensuring that configuration
properties and dependencies are correctly converted to the required types.
6 Attempt ANY ONE part from the following
Write a code for simple Spring application using annotations, xml or java to configure beans and
demonstrate dependency injection.
Code: Pojo Class
package in.abes.CSEDS;
import org.springframework.context.ApplicationContext;
import in.abes.CSEDS.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Student s=(Student)context.getBean("stdId");
s.display();
}}
Explain the concept of Dependency Injection and IOC in Spring. How does it promote loose
coupling in applications? Provide an example using constructor injection.
Solution: Inversion of Control (IoC)
IoC is a design principle in software engineering where the control of object creation and
management is inverted from the application's code to a framework or container. In
traditional programming, the application code is responsible for creating and managing the
lifecycle of its dependencies. With IoC, the control is shifted to a framework or container
(like the Spring container).
String greet();
@Override
// Constructor Injection
@Autowired
this.greetingService = greetingService;
System.out.println(greetingService.greet());
3. Configure Spring (Java Configuration)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public GreetingService greetingService() {
return new GreetingServiceImpl();
}
@Bean
public GreetingController greetingController(GreetingService greetingService) {
return new GreetingController(greetingService);
}
}
4. Main Application:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;