Section A – Short Answer (2×7=14)
(a) Difference between Reference Variable and Object (CO1, K1):
Reference Variable stores the address of the object.
Object is an instance of a class created using the new keyword.
class Demo {
int x = 10;
}
Demo obj = new Demo(); // 'obj' is the reference variable; new Demo()
creates the object.
(b) Method Overloading vs Overriding (CO1, K2):
Overloading: Same method name with different parameters in the same class.
Overriding: Subclass provides specific implementation of a method defined in the
superclass.
// Overloading
class Calc {
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"); }
}
(c) Interfaces in Java (CO2, K2):
Interfaces define a contract with abstract methods.
interface Shape {
double area();
}
class Circle implements Shape {
double r;
Circle(double r) { this.r = r; }
public double area() { return Math.PI * r * r; }
}
(d) Functional Interface (CO2, K2):
An interface with exactly one abstract method.
@FunctionalInterface
interface MyFunction {
int operation(int a, int b);
}
MyFunction add = (a, b) -> a + b;
(e) Method Reference (CO3, K2):
Simplifies Lambda Expressions using ::
Types: Static, Instance, Constructor
class Demo {
static void sayHello() { System.out.println("Hello!"); }
}
Runnable r = Demo::sayHello;
(f) Difference between List and Set (CO4, K2):
List: Ordered, allows duplicates. (ArrayList, LinkedList)
Set: Unordered, no duplicates. (HashSet, TreeSet)
(g) RESTful Web Services (CO5, K1):
REST (Representational State Transfer): An architectural style using HTTP methods
(GET, POST, PUT, DELETE) to interact with resources identified by URIs.
Section B – Long Answer (7×3=21)
Q2(a)(i) Java Compilation Process (CO1, K2):
1. Write Source Code (.java)
2. Compile using javac → bytecode (.class)
3. Execute using JVM → platform-independent execution.
(a)(ii) Types of Inheritance:
Single, Multilevel, Hierarchical
(Java does not support Multiple Inheritance via classes to avoid ambiguity –
Diamond Problem. It is supported via Interfaces.)
class A {}
class B extends A {}
class C extends B {}
Q2(b)(i) BankAccount with Threads:
class BankAccount {
private int balance = 1000;
synchronized void deposit(int amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient Balance");
}
}
}
(b)(ii) Multiple Exception Handling:
try {
int a = 10 / 0;
int[] arr = new int[3];
System.out.println(arr[5]);
FileReader fr = new FileReader("test.txt");
} catch (ArithmeticException e) {
System.out.println("Arithmetic Error");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Error");
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} finally {
System.out.println("Cleanup");
}
Q2(c)(i) Base64 Encoding/Decoding:
import java.util.Base64;
String original = "Hello World";
String encoded = Base64.getEncoder().encodeToString(original.getBytes());
String decoded = new String(Base64.getDecoder().decode(encoded));
(c)(ii) Functional Interface and Lambda:
@FunctionalInterface
interface Operation {
int op(int a, int b);
}
public class LambdaCalc {
public static void main(String[] args) {
Operation add = (a, b) -> a + b;
System.out.println(add.op(5, 3));
}
}
Q2(d)(i) Collections Framework:
Diagram includes: Iterable → Collection → List/Set/Queue
List: Ordered, duplicates
Set: No duplicates
Queue: FIFO structure
(d)(ii) Student Record using HashMap:
Map<Integer, String> students = new HashMap<>();
students.put(1, "Alice");
students.put(2, "Bob");
students.remove(1);
students.put(2, "Bobby");
System.out.println(students.get(2));
Q2(e)(i) Constructor vs Setter Injection (Spring):
@Component
class Car {
Engine engine;
@Autowired
Car(Engine engine) { this.engine = engine; }
}
// Setter
@Component
class Car {
Engine engine;
@Autowired
public void setEngine(Engine engine) {
this.engine = engine;
}
}
(e)(ii) Spring Bean Life Cycle:
@PostConstruct for init
@PreDestroy for destroy
@Component
class MyBean {
@PostConstruct
void init() { System.out.println("Init"); }
@PreDestroy
void destroy() { System.out.println("Destroy"); }
}
Section C (7×4=28)
Q3(a) String vs StringBuilder vs StringBuffer:
String: Immutable
StringBuilder: Mutable, not thread-safe
StringBuffer: Mutable, thread-safe
String s = "hello";
StringBuilder sb = new StringBuilder("hello");
StringBuffer sf = new StringBuffer("hello");
Q3(b) Constructor Overloading:
class Student {
String name;
int age;
Student() {}
Student(String name) { this.name = name; }
Student(String name, int age) {
this.name = name; this.age = age;
}
}
Q4(a) Two Threads Odd-Even:
class Even extends Thread {
public void run() {
for (int i = 2; i <= 20; i += 2)
System.out.println("Even: " + i);
}
}
class Odd extends Thread {
public void run() {
for (int i = 1; i < 20; i += 2)
System.out.println("Odd: " + i);
}
}
Q4(b) Stream API:
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.sorted(Comparator.reverseOrder())
.forEach(System.out::println);
Q5(a) Lambda to Count Words:
String sentence = "This is a sample sentence";
long count = Arrays.stream(sentence.split(" "))
.count();
System.out.println(count);
Q5(b) File Write/Read using Try-With-Resources:
try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) {
bw.write("Hello File");
}
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
}
Q6(a) Comparable vs Comparator:
Comparable: natural ordering, one method compareTo
Comparator: custom ordering, multiple logic
class Student implements Comparable<Student> {
int id;
public int compareTo(Student s) {
return this.id - s.id;
}
}
Q6(b) LinkedList Operations:
LinkedList<String> list = new LinkedList<>();
list.addFirst("Start");
list.addLast("End");
list.add(1, "Middle");
list.remove("Middle");
System.out.println(list.contains("Start"));
list.forEach(System.out::println);
Q7 Difference Between Dependency Injection (DI) and Inversion of Control
(IoC)
Aspect Inversion of Control (IoC) Dependency Injection (DI)
A specific type of IoC where
A broad principle where control of object
dependencies are provided
Definition creation and dependency management is
(injected) into a class by the
given to the framework/container.
framework.
General concept; includes strategies like DI,
Scope One technique to implement IoC.
Service Locator, Factory Method, etc.
The framework injects required
Who controls The framework (Spring container) creates
objects into the class via
object creation? and injects objects.
constructors, setters, or fields.
Implemented using constructor
Can be implemented using DI, events,
Implementation injection, setter injection, or field
template method, or service locator.
injection.
@Autowired in Spring to inject
Example Spring Container managing object lifecycle.
dependencies.
@Component
class Engine {
public void start() {
System.out.println("Engine started");
}
}
@Component
class Car {
private Engine engine;
// Constructor-based Dependency Injection
@Autowired
public Car(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
System.out.println("Car is moving");
}
}
Q7(b) 1. Spring Container
Definition:
The Spring Container is the core of the Spring Framework. It is responsible for:
o Creating objects (beans)
o Managing their lifecycle
o Injecting dependencies
o Managing the configuration
Types of Containers:
o BeanFactory – Basic container, lazy initialization
o ApplicationContext – More advanced, supports internationalization, event
propagation, AOP, etc.
Bean Lifecycle (using Annotations):
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean is initialized");
}
@PreDestroy
public void destroy() {
System.out.println("Bean is about to be destroyed");
}
}
How It Works:
The container reads configuration (XML or annotations), creates beans, wires
dependencies, and manages the entire lifecycle.
✅ 2. Aspect-Oriented Programming (AOP)
Definition:
AOP is a programming paradigm used to separate cross-cutting concerns (aspects
of a program that affect multiple parts, like logging, security, transactions).
Why Use AOP?
o Avoids code duplication (DRY principle)
o Keeps business logic clean
o Enhances modularity
Common AOP Use Cases:
o Logging
o Transaction management
o Security checks
o Exception handling
Spring AOP Example:
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void logBefore() {
System.out.println("Logging before method execution");
}
}
Terminology:
o Aspect – The cross-cutting concern
o Advice – The action taken (e.g., before, after, around a method)
o Pointcut – Specifies join points (where advice applies)
o Join Point – A point in execution (e.g., method call)
o Weaving – Linking aspects with application code