0% found this document useful (0 votes)
11 views4 pages

Java Coding Problems and Solutions

The document presents a series of coding problems in Java, focusing on various operations with lists and collections. It includes tasks such as adding and retrieving elements from an ArrayList, removing elements, checking for specific elements, and implementing a student management system using collections. Each problem provides hints and expected outputs to guide the implementation.

Uploaded by

Barbie Koushal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Java Coding Problems and Solutions

The document presents a series of coding problems in Java, focusing on various operations with lists and collections. It includes tasks such as adding and retrieving elements from an ArrayList, removing elements, checking for specific elements, and implementing a student management system using collections. Each problem provides hints and expected outputs to guide the implementation.

Uploaded by

Barbie Koushal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding Problems in Java Programming by Tawqeer ul Islam

Coding Problems in Java – Set 4

1. Add and Retrieve Elements from an ArrayList


Problem:
• Create an ArrayList<String>, add five names to it, and print each name using a
loop.
Hint: Use add() and get() methods of ArrayList.

2. Remove an Element from a List


Problem:
• Given a List<Integer>, remove all occurrences of a specific number.
List<Integer> numbers = [Link](1, 2, 3, 2, 4, 2, 5);
int target = 2;
[Link](removeElement(numbers, target));
// Output: [1, 3, 4, 5]
Hint: Use removeIf() method.

3. Check if a List Contains a Specific Element


Problem:
• Given a List<String>, check if a specific string is present.
Hint: Use the contains() method of List.

4. Convert an Array to a List and Vice Versa


Problem:
• Convert an Integer[] array into a List<Integer> and back to an array.
Hint: Use [Link]() and toArray().

5. Find the Maximum and Minimum Elements in a List


Problem:
• Given a List<Integer>, find and print the maximum and minimum values.
Hint: Use [Link]() and [Link]().
Coding Problems in Java Programming by Tawqeer ul Islam

6. Reverse a List
Problem:
• Given a List<String>, reverse its elements.
Hint: Use [Link]().

7. Sort a List in Ascending and Descending Order


Problem:
• Given a List<Integer>, sort it in both ascending and descending order.
Hint: Use [Link]() and [Link]().

8. Count the Frequency of Elements in a List


Problem:
• Given a List<String>, count how many times each string appears.
List<String> words = [Link]("apple", "banana", "apple", "orange", "banana",
"apple");
[Link](countFrequencies(words));
// Output: {apple=3, banana=2, orange=1}
Hint: Use a HashMap<String, Integer>.

9. Find Common Elements Between Two Lists


Problem:
• Given two List<Integer>, find the common elements.
List<Integer> list1 = [Link](1, 2, 3, 4, 5);
List<Integer> list2 = [Link](3, 4, 5, 6, 7);
[Link](findCommonElements(list1, list2));
// Output: [3, 4, 5]
Hint: Use retainAll() method.

10. Implement a Stack Using Deque


Problem:
• Use Deque<Integer> to implement a stack with:
o push(int x)
Coding Problems in Java Programming by Tawqeer ul Islam

o pop()
o peek()
o isEmpty()
Hint: Use ArrayDeque<Integer> with addFirst() and removeFirst().

Thodi Badi Problem


Problem: Student Management System Using Collections
Problem Statement: You need to create a simple Student Management System in Java
using the Collections Framework. The system should allow users to perform the following
operations:
1. Add a student with details:
o id (Integer)
o name (String)
o age (Integer)
o marks (Double)
2. Remove a student by their id.
3. Search for a student by their id and display their details.
4. Display all students, sorted by their marks in descending order.
5. Find the student with the highest marks.

Implementation Hints:
• Use a HashMap<Integer, Student> where the key is the student's id and the value
is the Student object.
• Use [Link]() with a Comparator to sort students by marks.
• Implement the operations inside a menu-driven program using a Scanner for user
input.

Expected Output Example:


1. Add Student
2. Remove Student
3. Search Student
4. Display All Students
5. Find Top Student
Coding Problems in Java Programming by Tawqeer ul Islam

6. Exit
Enter your choice: 1

Enter ID: 101


Enter Name: John
Enter Age: 20
Enter Marks: 85.5
Student added successfully!

Enter your choice: 4


Students List (Sorted by Marks):
ID: 101, Name: John, Age: 20, Marks: 85.5

Enter your choice: 5


Top Student:
ID: 101, Name: John, Age: 20, Marks: 85.5

Enter your choice: 6


Exiting...

Common questions

Powered by AI

Collections.sort() can be combined with a custom Comparator to sort complex objects like students by particular fields, such as marks. The Comparator defines a compare() method that establishes ordering by comparing object properties. This approach not only orders objects based on specific criteria but also allows for flexible sorting strategies.

The removeIf() method enhances code efficiency by providing an inline way to remove elements based on a specified condition. When used in List<Integer> to remove specific elements, it eliminates the need for manual iteration and index tracking, thus simplifying the code and reducing potential errors, particularly in larger datasets.

To find common elements between two List<Integer> instances, use the retainAll() method on one of the lists with the other list as the parameter. This method modifies the list to include only elements that are present in both lists, effectively identifying the intersection. This process is straightforward and optimizes list comparison.

Implement a student management system using a HashMap<Integer, Student>, where student operations are streamlined via collection methods. Adding involves putting a new Student object into the map; removal utilizes the remove(id) method; searching leverages get(id) for quick student retrieval by ID. Collections facilitate these operations efficiently, enhancing data handling capabilities.

To find the maximum and minimum values in a List<Integer>, you can use the Collections.max() and Collections.min() methods, respectively. These methods allow you to find the largest and smallest elements efficiently in a list.

To reverse elements of a List<String> in Java, you can use the Collections.reverse() method. First, create the list and populate it with the desired elements. Then, apply Collections.reverse(list) to reverse the order of elements in-place.

Converting arrays to lists and vice versa in Java is important for utilizing the rich API of lists, which offers easy data manipulation, iteration, and storage. Arrays.asList() is used to convert an array to a list, while the toArray() method converts a list back to an array, allowing for seamless data interchangeability between the two structures.

To implement a stack using Deque<Integer> in Java, use an ArrayDeque. Key operations include push(int x) using addFirst(x), pop() using removeFirst(), peek() using getFirst(), and isEmpty() using isEmpty() method. This makes stack operations efficient as the Deque interface provides first-in-last-out (FILO) functionality similar to a stack.

To count the frequency of elements in a List<String>, a HashMap<String, Integer> is effective. Iterate through the list, using each element as a key in the HashMap and increment its corresponding value to count occurrences. This approach efficiently tracks how many times each string appears in the list.

A HashMap<Integer, Student> can be used to manage student records efficiently by storing the student ID as the key and the Student object as the value. This allows for quick retrieval, addition, and deletion of students based on their IDs, thanks to the constant time complexity of hash-based indexing, enhancing performance in large datasets.

You might also like