1. Which package contains the Java Collection Framework?
C) [Link]
2. Collection is the root interface of the Collection Framework?
3.A shopping application stores a list of items in the order they were added and allows
duplicate items.
Which collection should be used?
A) HashSet
B) TreeSet
C) ArrayList____
D) HashMap
[Link] is theoutput of the below programme
import [Link].*;
public class CollectionExample {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]("Java"); // duplicates allowed
[Link](c);
Output:
[Java, Python, Java]
5.A banking application stores account number and balance as key–value pairs.
Map collection should be used?(True? False)
6.A music app stores a playlist where songs can repeat and the order matters.
Which interface should be used?
A) Set
B) Map
C) List______
D) Collection
[Link] the error in the below programme:
import [Link].*;
public class ArrayListDemo {
public static void main(String[] args) {
List<String> students = new ArrayList<>();
[Link]("Kiran");
[Link]("Ravi");
[Link]("Kiran");
[Link](students);
8.A program needs a general container to store objects without caring about order or
uniqueness.
Which is the root interface used?
A) List
B) Set
C) Collection____
D) Map
[Link] e-commerce application frequently retrieves products by index position.
LinkedList class gives better performance?(True?False)
10.A chat application frequently adds and removes messages from the middle of a list.
LinkedList class should be used?
11.A student attendance system stores ordered names with duplicates allowed.
ArrayList class is best?
3 marks:
[Link]:
Suppose you are designing a shopping cart for an online store. Write the program
demonstrates a simple shopping cart system.
Programme:
import [Link].*;
public class ShoppingCart {
public static void main(String[] args) {
// Create a shopping cart (Collection)
Collection<String> cart = new ArrayList<>();
// Add items to the cart
[Link]("Mobile");
[Link]("Laptop");
[Link]("Headphones");
[Link]("Mobile"); // duplicate allowed
// Display all items in the cart
[Link]("Shopping Cart Items: " + cart);
// Total items in the cart
[Link]("Total Items: " + [Link]());
Output:
Shopping Cart Items: [Mobile, Laptop, Headphones, Mobile]
Total Items: 4
[Link]:
import [Link].*;
public class HashMapExample {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
[Link](101, "Java");
[Link](102, "Python");
[Link](103, "C");
[Link](map);
Question 1
What will be the output of the following Java program?
Question 2
Which interface and class are used in the above program to store key–value pairs?
Question 3
Does HashMap maintain insertion order? Explain with reference to the above program.
Ans:
The program inserts three key–value pairs into a HashMap and prints it.
Possible Output:
{101=Java, 102=Python, 103=C}
Important:
HashMap does not guarantee order, so output order may vary.
For example, it may also print:
{103=C, 101=Java, 102=Python}
So the order is not fixed.
Question 2: Which interface and class are used?
Interface: Map (from [Link] package)
Class: HashMap (from [Link] package)
In the program:
Map<Integer, String> map = new HashMap<>();
✔ Map → Interface
✔ HashMap → Implementation class
Question 3: Does HashMap maintain insertion order?
No, HashMap does NOT maintain insertion order.
Explanation:
In the program, you inserted:
o 101 → Java
o 102 → Python
o 103 → C
But when printing, order is not guaranteed.
This is because HashMap stores elements using hashing mechanism, not insertion
sequence.
[Link]:
a. Difference between List and Set
b. Difference between Collection and Map
c. Set Examples and Map Examples
[Link]:
In a class attendance register, a student name may appear more than once (by mistake or
multiple sessions)(must use LinkedList)
programme:
import [Link].*;
public class AttendanceRegister {
public static void main(String[] args) {
LinkedList<String> attendance = new LinkedList<>();
[Link]("Ravi");
[Link]("Anil");
[Link]("Ravi"); // duplicate allowed
[Link](attendance);
[Link]:
College Student Registration System
List (ArrayList) → Store all registered students
(duplicates allowed – mistake entries)
Set (HashSet) → Store unique roll numbers
(no duplicates allowed)
Programme:
import [Link].*;
public class CollegeStudentRegistration {
public static void main(String[] args) {
// List using ArrayList (duplicates allowed)
ArrayList<String> students = new ArrayList<>();
[Link]("Kiran");
[Link]("Ravi");
[Link]("Anil");
[Link]("Kiran"); // duplicate allowed
[Link]("Registered Students (List):");
for (String s : students) {
[Link](s);
[Link]();
// Set using HashSet (duplicates NOT allowed)
HashSet<Integer> rollNumbers = new HashSet<>();
[Link](101);
[Link](102);
[Link](103);
[Link](101); // duplicate NOT allowed
[Link]("Unique Roll Numbers (Set):");
for (int r : rollNumbers) {
[Link](r);
Output
Registered Students (List):
Kiran
Ravi
Anil
Kiran
Unique Roll Numbers (Set):
101
102
103
6. Aim:
Write a Java program using ArrayList to store student marks in a class and display them.
Explain why ArrayList is suitable for this scenario.
Programme:
import [Link];
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>();
[Link](85);
[Link](90);
[Link](78);
[Link](90); // duplicate allowed
[Link]("Student Marks:");
for (int m : marks) {
[Link](m);
Output
Student Marks:
85
90
78
90
6 marks:
[Link]:
Write a Java program to store student roll number and student details (name, marks) using
the Map interface.
Demonstrate how to insert and retrieve data from a HashMap.
Programme:
import [Link].*;
class Student {
String name;
int marks;
Student(String name, int marks) {
[Link] = name;
[Link] = marks;
public class StudentMapExample {
public static void main(String[] args) {
// Map interface reference
Map<Integer, Student> studentMap = new HashMap<>();
// Adding student details (Key = Roll No)
[Link](101, new Student("Kiran", 85));
[Link](102, new Student("Ravi", 78));
[Link](103, new Student("Sita", 85)); // duplicate value allowed
// Display student details
[Link]("Student Marks Details:");
for ([Link]<Integer, Student> entry : [Link]()) {
int rollNo = [Link]();
Student s = [Link]();
[Link]("Roll No: " + rollNo +
", Name: " + [Link] +
", Marks: " + [Link]);
Output:
Student Marks Details:
Roll No: 101, Name: Kiran, Marks: 85
Roll No: 102, Name: Ravi, Marks: 78
Roll No: 103, Name: Sita, Marks: 85
[Link]:
Write a Java program using Collection interfaces List and Set to implement a real-time
student registration system.
Demonstrate how List allows duplicate student entries and Set stores unique course names.
Programme:
import [Link].*;
// Student class
class Student {
int id;
String name;
Student(int id, String name) {
[Link] = id;
[Link] = name;
void display() {
[Link]("ID: " + id + ", Name: " + name);
public class ListSetExample {
public static void main(String[] args) {
// List interface - allows duplicates
List<Student> studentList = new ArrayList<>();
[Link](new Student(101, "Kiran"));
[Link](new Student(102, "Ravi"));
[Link](new Student(101, "Kiran")); // duplicate allowed
[Link]("Student Registration List:");
for (Student s : studentList) {
[Link]();
// Set interface - does not allow duplicates
Set<String> courseSet = new HashSet<>();
[Link]("JAVA");
[Link]("PYTHON");
[Link]("JAVA"); // duplicate ignored
[Link]("\nAvailable Courses:");
for (String course : courseSet) {
[Link](course);
Output:
Student Registration List:
ID: 101, Name: Kiran
ID: 102, Name: Ravi
ID: 101, Name: Kiran
Available Courses:
JAVA
PYTHON
(Order may vary for Set)