0% found this document useful (0 votes)
6 views

Algorithms_Lab2_15M-2

The document outlines an assessment for an Algorithms & Data Structures lab, consisting of three questions focused on Java programming. Question 1 requires writing a stack implementation to push and pop elements, Question 2 involves a queue example with specific output questions, and Question 3 asks for a binary search program. Each question is allocated 5 marks, and submission guidelines emphasize the need for compilable and executable code along with output screenshots.

Uploaded by

التعزي
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)
6 views

Algorithms_Lab2_15M-2

The document outlines an assessment for an Algorithms & Data Structures lab, consisting of three questions focused on Java programming. Question 1 requires writing a stack implementation to push and pop elements, Question 2 involves a queue example with specific output questions, and Question 3 asks for a binary search program. Each question is allocated 5 marks, and submission guidelines emphasize the need for compilable and executable code along with output screenshots.

Uploaded by

التعزي
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
You are on page 1/ 3

Algorithms & Data Structures Lab Assessment-2

Total Marks: 15

Question 1: Stack (5 Marks)


Write a simple Java code that Push 20 elements then print it. Then pop all elements
on the stack after that print it again?

Best wishes 😊 Ahood Abdullah


Question 2: Queue (5 Marks)
Consider the following Java code that demonstrates basic operations on a Queue
implemented with a LinkedList:

import java.util.*;

public class QueueExample {

public static void main(String args[]){

Queue<Integer> qu = new LinkedList<>();

System.out.println("Is Queue empty ?: " + qu.isEmpty());

System.out.println("Adding elements to the Queue");

qu.add(1); qu.add(4); qu.add(6);

System.out.println("The Queue size is : " + qu.size());

System.out.println("The Queue elements are : " + qu);

System.out.println("The first element is : " + qu.peek());

System.out.println("Polling elements from the Queue");

qu.poll(); qu.poll(); qu.poll();

System.out.println("The Queue size is : " + qu.size());

System.out.println("The Queue elements are : " + qu); }}

Answer the following questions:

1. What does the program print when checking if the queue is empty before any
elements are added?

2. After adding the elements 1, 4, and 6, what are the printed outputs for the queue’s
size, its elements, and the first element (using peek())?

3. What will be the output after polling (removing) all elements from the queue, and
why?

4. Briefly explain the purpose of the peek() and poll() methods in the context of this
program.

Best wishes 😊 Ahood Abdullah


Question 3: Binary Search (5 Marks)
Write a simple Java program that performs the following tasks:

1. Prompt the user to enter 5 integers (ensure the numbers are in ascending
order for binary search to work correctly).

2. Ask the user to enter a number to search for in the array.

3. Implement a binary search method to search for the entered number in the
array.

4. If the number is found, print the index where it is located; otherwise, print
"Not Found".

Submission Guidelines:
- Ensure the code is compilable and executable.

- The assessment should be submitted clearly to show the code and output screenshot.

Best wishes 😊 Ahood Abdullah

You might also like