PUE AnswerKey
PUE AnswerKey
EVEN Semester
Department:CS/CSE/CS(AI)/CS(AI&ML)/CSIT/IT Course: B.Tech
Year: II Semester: IV
Subject Name: Object Oriented Programming with Java Subject Code: BCS 403
The Java Runtime Environment (JRE) is a software package that provides libraries, Java Virtual
Machine (JVM), and other components to run applications written in Java. The JVM is a part of
the JRE that executes Java bytecode and provides platform independence.
Difference:
Key Difference:
final: Immutability.
static: Shared class-level context.
1c. Byte Stream vs Character Stream
Byte streams handle raw binary data, using InputStream and OutputStream classes. Character
streams handle character data (Unicode), using Reader and Writer classes.
Example:
Transitions happen via methods like start(), sleep(), join(), or completion of run method.
In Java 14 and above, yield is a keyword used within a switch expression to return a value
from a case block.
Purpose:
To return a value from a case block when using switch expressions.
Example:
int day = 2;
String result = switch (day) {
case 1 -> "Monday";
case 2 -> {
yield "Tuesday";
}
default -> "Other Day";
};
System.out.println(result); // Output: Tuesday
1f. Functional Interface
A functional interface contains exactly one abstract method and may contain default or static
methods. It's used with lambda expressions.
@FunctionalInterface
interface Greeting {
void sayHello();
}
HashSet stores elements using hashing, offers constant time performance, but no order.
TreeSet uses a tree structure and stores elements in sorted order.
Use Case:
// Comparable in class
class Student implements Comparable<Student> {
public int compareTo(Student s) { return this.age - s.age; }
}
Section B (5 x 4 Marks)
Q2a. Polymorphism in Java
Polymorphism allows objects to take on many forms. It enables a single interface to
control access to a general class of actions. This allows flexibility and reusability in
code.
There are two main types:
1. Compile-time Polymorphism (Method Overloading): Methods with the same
name but different parameters within the same class. This is resolved by the
compiler.
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
Difference:
Overloading is resolved during compilation.
Overriding is resolved at runtime using dynamic dispatch.
Q2b. Student Grades Analysis
This program demonstrates the use of arrays and control statements to process student
scores. It calculates the highest and average score from the input.
import java.util.Scanner;
Switch Expression:
int day = 2;
String result = switch(day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Invalid";
};
System.out.println(result);
// Writing to file
try (Writer writer = new FileWriter("sample.txt")) {
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
}
A HashMap is a collection that stores data in key-value pairs. This program stores product names
and prices, then prints them.
import java.util.*;
products.put("Laptop", 55000.0);
products.put("Mouse", 500.0);
products.put("Keyboard", 1500.0);
hashMap.put("Apple", 1);
linkedHashMap.put("Banana", 2);
treeMap.put("Cherry", 3);
hashtable.put("Date", 4);
IoC is a principle where control of objects is transferred to the framework (e.g., Spring).
DI is a specific implementation of IoC where dependencies are injected into a class.
Example:
@Component
class Engine {
public String getType() { return "Petrol"; }
}
@Component
class Car {
@Autowired
Engine engine;
}
Here, Spring injects the Engine into Car, thus managing control (IoC).
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
Section C (5 x 6 Marks)
Q7a. BankAccount Class
This class models a simple bank account with fields for account number, holder name,
and balance. It also includes methods for depositing money and displaying account
details.
public class BankAccount {
private String accountNumber;
private String holderName;
private double balance;
static double interestRate = 3.5;
public BankAccount(String acc, String name, double bal) {
this.accountNumber = acc;
this.holderName = name;
this.balance = bal;
}
void display() {
System.out.println(title + " by " + author + " - Rs." + price);
}
}
class Bank {
double balance = 1000;
students.forEach(System.out::println);
}
}
groceries.remove("Eggs");
groceries.remove("Butter");
System.out.println("Remaining Items:");
Iterator<String> it = groceries.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
@Autowired
private StudentRepository repo;
@PostMapping("/")
public Student create(@RequestBody Student s) {
return repo.save(s);
}
@GetMapping("/{id}")
public Student get(@PathVariable int id) {
return repo.findById(id).orElse(null);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable int id) {
repo.deleteById(id);
}
}
@Autowired
private BookRepository repo;
@PostMapping("/")
public Book addBook(@RequestBody Book b) {
return repo.save(b);
}
@GetMapping("/{id}")
public Book getBook(@PathVariable int id) {
return repo.findById(id).orElse(null);
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable int id) {
repo.deleteById(id);
}
}