0% found this document useful (0 votes)
83 views7 pages

Solutions

The document contains Java code examples demonstrating custom exceptions for age eligibility in an army job, stack operations, and priority queue usage. It illustrates how to handle exceptions, iterate through collections, and perform operations like adding and removing elements. Additionally, it includes outputs and notes on the behavior of iterators and collections in Java.

Uploaded by

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

Solutions

The document contains Java code examples demonstrating custom exceptions for age eligibility in an army job, stack operations, and priority queue usage. It illustrates how to handle exceptions, iterate through collections, and perform operations like adding and removing elements. Additionally, it includes outputs and notes on the behavior of iterators and collections in Java.

Uploaded by

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

1.

Output:

x=0

Divide by zero error

Inside the finally block

7.

// Custom exception for "Too Old" condition

class TooOldException extends Exception {

public TooOldException(String message) {

super(message);

// Custom exception for "Too Young" condition

class TooYoungException extends Exception {

public TooYoungException(String message) {

super(message);

// Main class

public class ArmyJobAgeCriteria {

private int age;

// Constructor to set age

public ArmyJobAgeCriteria(int age) {

[Link] = age;

}
// Method to check age eligibility

public void checkAge() throws TooOldException, TooYoungException {

if (age > 28) {

throw new TooOldException("You are too old to apply for the job.");

} else if (age < 18) {

throw new TooYoungException("You are too young to apply for the job.");

} else {

[Link]("Congratulations! You are eligible for the job.");

// Main method to test the program

public static void main(String[] args) {

int[] testAges = {15, 22, 30}; // Example ages for testing

for (int age : testAges) {

[Link]("Checking eligibility for age: " + age);

ArmyJobAgeCriteria criteria = new ArmyJobAgeCriteria(age);

try {

[Link]();

} catch (TooOldException | TooYoungException e) {

[Link]([Link]());

}
10.

Output:

Exception caught [Link]: 2

20. An Iterator in Java is an interface that provides a way to iterate (or traverse) through the elements of
a collection, such as a list or set, one by one. It allows you to access each element without needing to
know the underlying structure of the collection.

27.

import [Link].*;

public class StringStackExample {

public static void main(String[] args) {

// Create a Stack of Strings

Stack<String> stack = new Stack<>();

// Insert 5 string elements into the stack

[Link]("Apple");

[Link]("Banana");

[Link]("Cherry");

[Link]("Date");

[Link]("Elderberry");

// Traversing the stack using Iterator

[Link]("Stack elements:");

Iterator<String> iterator = [Link]();

while ([Link]()) {

[Link]([Link]());

// Perform 2 deletion operations (pop)

[Link](); // Removes "Elderberry"


[Link](); // Removes "Date"

// Display the final stack after deletion

[Link]("\nFinal stack after 2 deletions:");

iterator = [Link](); // Re-iterate the stack

while ([Link]()) {

[Link]([Link]());

28. In Java, the Set interface in the Collections Framework is responsible for storing unique items in the
collection, meaning no duplicate elements are allowed. The HashSet and TreeSet are common
implementations of the Set interface, but List itself can store duplicates.

import [Link].*;

public class PriorityQueueExample {

public static void main(String[] args) {

// Create a PriorityQueue of Strings

PriorityQueue<String> pq = new PriorityQueue<>();

// Add five string elements to the priority queue

[Link]("Banana");

[Link]("Apple");

[Link]("Cherry");

[Link]("Date");

[Link]("Elderberry");

// Display the head element of the queue (element with the highest priority)

[Link]("Head element of the queue: " + [Link]());


// Traverse the queue using Iterator

[Link]("\nTraversing the queue using Iterator:");

Iterator<String> iterator = [Link]();

while ([Link]()) {

[Link]([Link]());

29.

Output:

SIT

30.

Output:

31.

Output:

10

20

30

Note: The Iterator used here traverses the elements in the stack from the bottom to the top, which is
why it prints the elements in the order they were pushed.

32.

Output:

Silicon

Bhubaneswar
35.

Output:

[1, 3, 10, 20]

Note: The actual output may vary due to the internal structure of the heap. However, the smallest
element (1) will always be at the front, and the other elements will be in a partial order satisfying the
heap property.

38.

Output:

The strings are unequal.

The strings are equal.

39.

Output:

HelloY7

40.

Output:

Hello96

Note: In this case, ch1 + ch2 is evaluated first:

 'Y' (ASCII value 89) + 7 = 96.


 The result will be Hello96

You might also like