Java Week10
Java Week10
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 10_Q1
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : COD
1. Problem Statement
7
7
01
01
01
01
A city traffic management system needs to track vehicles entering a toll
01
01
01
01
10
10
10
24
24
24
system should allow adding vehicles to a record, ensuring that no
duplicate registration numbers exist. The vehicles should be stored in a
HashSet, which does not guarantee any specific order.
Input Format
The first line of input contains an integer N - the number of vehicles.
7
7
01
01
01
1
10
01
01
01
The next N lines contain details of each vehicle in the format: "RegNumber
0
10
10
10
10
24
24
24
24
OwnerName VehicleType"
17
7
01
01
01
10
01
01
01
0
10
10
10
2. OwnerName (String) - The name of the vehicle owner.
24
24
24
24
3. VehicleType (String, Car, Bike, or Truck) - The type of vehicle.
If a vehicle with the same registration number is already present, ignore the
duplicate entry.
Output Format
The output prints the unique vehicle records in any order (since HashSet does
not maintain order).
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 5
KA01AB1234 John Car
MH02CD5678 Alice Bike
DL03EF9012 Bob Truck
TN04GH3456 Mike Car
KA01AB1234 John Car
7
7
01
01
01
01
Output: TN04GH3456 Mike Car
01
01
01
01
10
10
10
10
KA01AB1234 John Car
24
24
24
24
MH02CD5678 Alice Bike
DL03EF9012 Bob Truck
Answer
import [Link];
import [Link];
import [Link];
class Vehicle {
private String regNumber;
7
01
01
1
10
01
01
0
10
10
10
10
24
24
24
24
public Vehicle(String regNumber, String ownerName, String vehicleType) {
17
7
01
01
01
[Link] = regNumber;
10
01
01
01
0
[Link] = ownerName;
10
10
10
10
[Link] = vehicleType;
24
24
24
24
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != [Link]()) return false;
7
17
17
01
01
Vehicle vehicle = (Vehicle) o;
10
0
01
01
01
00
return [Link]([Link]);
10
10
10
1
}
24
24
24
24
@Override
public int hashCode() {
return [Link]();
}
@Override
public String toString() {
return regNumber + " " + ownerName + " " + vehicleType;
7
7
01
01
01
01
}
01
01
01
01
}
10
10
10
10
24
24
24
24
class TrafficSystem {
7
01
01
01
01
01
10
10
10
24
24
24
24
17
7
01
01
01
if ([Link] == 3) {
10
01
01
01
0
10
10
10
String ownerName = parts[1];
24
24
24
24
String vehicleType = parts[2];
[Link](newVehicle);
}
}
17
17
01
01
[Link](vehicle);
10
0
01
01
01
00
}
10
10
10
1
24
24
24
24
[Link]();
}
}
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 10_Q2
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : COD
1. Problem Statement
7
7
01
01
01
01
John is organizing a fruit festival, and the quantities of various fruits are
01
01
01
01
10
10
10
stored in a HashMap where fruit names are keys and quantities are values. 10
24
24
24
24
Help him develop a program to find the total quantity of fruits for the
festival by summing up the values in the HashMap.
Input Format
The input consists of fruit quantities in the format 'fruitName:quantity', where
fruitName is the name of the fruit(a string), and quantity is a double value
representing the quantity.
7
01
01
1
10
01
01
01
Output Format
10
10
10
10
24
24
24
24
The output prints a double value, representing the sum of values in the
17
7
01
01
01
HashMap, rounded off to two decimal places.
10
01
01
01
0
10
10
10
10
If the value is not numeric, print "Invalid input".
24
24
24
24
If any special characters other than ':' are entered, print "Invalid format".
17
17
01
01
10
0
Orange:56.3
01
01
01
00
Mango:47.3
10
10
10
1
24
24
24
24
done
Output: 118.80
Answer
import [Link];
import [Link];
import [Link];
class FruitFestival {
7
01
01
01
01
Scanner scanner = new Scanner([Link]);
01
01
01
01
10
10
10
10
Map<String, Double> fruitQuantities = new HashMap<>();
24
24
24
24
double totalQuantity = 0.0;
String line;
7
01
01
01
1
10
01
01
01
0
10
10
10
10
if ([Link] != 2) {
24
24
24
24
[Link]("Invalid format");
17
7
01
01
01
return;
10
01
01
01
0
}
10
10
10
10
24
24
24
24
String quantityStr = parts[1].trim();
try {
double quantity = [Link](quantityStr);
String fruitName = parts[0].trim();
[Link](fruitName, quantity);
totalQuantity += quantity;
} catch (NumberFormatException e) {
7
17
17
01
01
[Link]("Invalid input");
10
0
01
01
01
00
return;
10
10
10
1
}
24
24
24
24
}
[Link]("%.2f%n", totalQuantity);
[Link]();
}
}
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 10_Q3
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : COD
1. Problem Statement
7
7
01
01
01
01
Priya is analyzing encrypted messages in a research project. She wants to
01
01
01
01
10
10
10
24
24
24
characters should be stored in a TreeMap so that the output is sorted in
ascending order of characters automatically.
7
01
01
01
1
10
01
01
01
0
10
10
10
10
Input Format
24
24
24
24
The first line of input contains an integer n, the number of lines in the message.
17
7
01
01
01
10
01
01
01
0
The next n lines each contain a string (the encrypted message line).
10
10
10
10
24
24
24
24
Output Format
The first line of output prints: "Character Frequency:"
Then print each character and its frequency in the format: "<character>: <count>"
17
17
Sample Test Case
01
01
10
0
01
01
01
00
Input: 2
10
10
10
1
24
24
24
24
Hello World
Java
Output: Character Frequency:
H: 1
J: 1
W: 1
a: 2
d: 1
e: 1
l: 3
7
7
o: 2
01
01
01
01
r: 1
01
01
01
01
10
10
10
10
v: 1
24
24
24
24
Answer
import [Link];
import [Link];
import [Link];
class MessageAnalyzer{
public static void main(String args[]){
Scanner ip = new Scanner([Link]);
int n = [Link]();
7
[Link]();
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
17
7
01
01
01
for(int i=0; i<n; i++){
10
01
01
01
0
10
10
10
for(char c : [Link]()){
24
24
24
24
if([Link](c)){
[Link](c, [Link](c,0)+1);
}
}
}
[Link]("Character Frequency:");
17
17
01
01
}
10
0
01
01
01
00
10
10
10
1
[Link]();
24
24
24
24
}
}
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 10_Q4
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : COD
1. Problem Statement
7
7
01
01
01
01
In a ticket reservation system, you store the available seat numbers in a
01
01
01
01
10
10
10
TreeSet. Users input their desired seat number, and the program checks 10
24
24
24
24
whether the chosen seat is available.
Input Format
The first line of input contains a single integer n, representing the number of
available seats.
7
01
01
1
10
seat numbers.
01
01
01
0
10
10
10
10
24
24
24
24
The third line contains an integer m, representing the seat number that needs to
17
7
01
01
01
be searched.
10
01
01
01
0
10
10
10
10
Output Format
24
24
24
24
The output displays "[m] is present!" if the given seat is available. Otherwise, it
displays "[m] is not present!"
17
17
01
01
10
0
2456
01
01
01
00
5
10
10
10
1
24
24
24
24
Output: 5 is present!
Answer
import [Link];
import [Link];
import [Link];
class TicketReservation{
public static void main(String args[]){
Scanner ip = new Scanner([Link]);
7
7
int n = [Link]();
01
01
01
01
01
01
01
01
10
10
10
10
Set<Integer> availableSeats = new TreeSet<>();
24
24
24
24
for(int i=0; i<n; i++){
[Link]([Link]());
}
int m = [Link]();
if([Link](m)){
[Link](m + " is present!");
}
else{
7
01
01
1
10
}
01
01
01
0
10
10
10
10
24
24
24
24
24 24 24 24
10 10 10 10
}
01 01 01 0
}
01 01 01 10
7 7 7 17
[Link]();
Status : Correct
24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7
24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7
24 24 24 24
10 10 10 10
Marks : 10/10
0 10 01 01 01
1 01 0 01
7 7 17 7
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
REC_2028_OOPS using Java_Week 10_MCQ
Attempt : 1
Total Mark : 15
Marks Obtained : 15
Section 1 : MCQ
7
01
01
01
01
Answer
01
01
01
01
10
10
10
24
24
24
Answer
They are sorted in ascending order
Status : Correct Marks : 1/1
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
3. What will be the output of the following code?
17
7
01
01
01
10
01
01
01
0
import [Link].*;
10
10
10
10
24
24
24
24
class Main {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
[Link]("A", "Apple");
[Link]("B", "Banana");
[Link]("C", "Cherry");
[Link]("B", "Blueberry");
[Link](map);
}
}
7
17
17
01
01
10
0
01
01
01
Answer
00
10
10
10
1
24
24
24
24
{A=Apple, B=Blueberry, C=Cherry}
Status : Correct Marks : 1/1
Answer
It is not synchronized
Status : Correct Marks : 1/1
7
7
01
01
01
01
01
01
01
01
10
10
10
24
24
24
Answer
It maintains natural ordering
Status : Correct Marks : 1/1
6. What happens if two keys have the same hash code in a HashMap?
Answer
7
7
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
Status : Correct Marks : 1/1
17
7
01
01
01
10
01
01
01
0
10
10
10
10
7. Which method retrieves the lowest key in a TreeMap?
24
24
24
24
Answer
firstKey()
Status : Correct Marks : 1/1
Answer
7
17
17
01
01
10
0
01
01
01
O(1)
00
10
10
10
1
24
24
24
24
Status : Correct Marks : 1/1
Answer
TreeSet provides sorted elements
Status : Correct Marks : 1/1
7
7
01
01
01
01
10. What will be the output of the following code?
01
01
01
01
10
10
10
10
24
24
24
24
import [Link].*;
class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
[Link]("A", 1);
[Link]("B", 2);
[Link]("C", 3);
[Link]([Link]("B"));
}
}
7
7
01
01
01
1
10
01
01
01
Answer
10
10
10
10
24
24
24
24
true
17
7
01
01
01
10
01
01
01
Status : Correct Marks : 1/1
0
10
10
10
10
24
24
24
24
11. Which method removes all elements from a Set?
Answer
clear()
Status : Correct Marks : 1/1
17
17
01
01
10
0
01
01
01
import [Link].*;
00
10
10
10
1
class Main {
24
24
24
24
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
[Link]("X", 10);
[Link]("Y", 20);
[Link]("Z", 30);
[Link]("Y");
[Link](map);
}
}
7
7
01
01
01
01
Answer
01
01
01
01
10
10
10
{X=10, Z=30} 10
24
24
24
24
Answer
An exception occurs
Status : Correct Marks : 1/1
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
17
7
Answer
01
01
01
10
01
01
01
0
10
10
10
24
24
24
24
Status : Correct Marks : 1/1
Answer
HashMap
Status : Correct Marks : 1/1
7
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24