You are on page 1of 8

How would you implement a method in Java that checks if a given string containing parentheses is

balanced using a Queue?


----Use a loop to iterate through each character of the string, enqueueing opening parentheses
and dequeuing when encountering closing parentheses.

What happens if you try to pop() from an empty stack in Java?


----It throws a NoSuchElementException

In what scenario would you prefer using a Stack instead of an ArrayList or LinkedList in Java?
----When recursion needs to be implemented

In what scenario would you prefer using a Queue instead of an ArrayList or LinkedList in Java?
---- When elements need to be processed in the order they were added

If the user inputs 99 and 66. What will be the output of the sample program?
---- Enter 2 numbers: 99 66
Priority queue elements before deletion:
[66, 99]
Removing two numbers from the priority queue:
Priority queue elements after deletion:
[99]
Priority queue elements after deletion:
[1, 99]
Priority queue elements after peek():
1

What is the significance of the PriorityQueue class in Java compared to other data structures?
---- It orders elements based on their natural ordering or according to a comparator

Which of the following represents the final arrangement of the numbers [45, 12, 22, 44, 32] when
inserted consecutively using the PriorityQueue algorithm?
----[12, 32, 22, 45, 44]

Analyze the code below.


public class ArrayClass {
public static void main(String[] args) {
int i = 25;
int [] arr = new int [i];
i = 50;
System.out.println("I is "+ i);
System.out.println("The size of i is " + arr.length);} }

What is the value of arr.length?


----25

How does choosing an appropriate data structure affect the performance of algorithms?
---- It can significantly improve or degrade algorithm performance

Design an algorithm to check if a given number is prime. Describe the steps involved in your
algorithm and explain why it is efficient.
---- Start by dividing the number by 2 and continue checking divisibility up to its square root. If
no divisors are found, the number is prime. This algorithm is efficient because it only checks
up to the square root of the number, reducing the number of iterations needed.

Which of the following statements best describes an abstract data type (ADT)?
---- An ADT is a conceptual model that defines the operations that can be performed on a data
structure and the properties of those operations.

Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.

void fun(struct node **head_ref)


{
struct node *temp = NULL;
struct node *current = *head_ref;
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}

if(temp != NULL )
*head_ref = temp->prev;
}

Assume that reference of head of following doubly linked list is passed to above function 1 <--> 2 <--
> 3 <--> 4 <--> 5 <-->6. What should be the modified linked list after the function call?
Answer: 6 < - - > 5 < - - > 4 < - - > 3 < - - > 2 < - - > 1

The following function reverse() is supposed to reverse a singly linked list. There is one line missing
at the end of the function.
/* Link list node */
struct node
{
int data;
struct node* next;
};

/* head_ref is a double pointer which points to head (or start) pointer


of linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
/*ADD A STATEMENT HERE*/
}

What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly
reverses a linked list.
----*head_ref = prev;
The following C function takes a simply-linked list as input argument. It modifies the list by moving
the last element to the front of the list and returns the modified list. Some part of the code is left
blank. Choose the correct alternative to replace the blank line.

typedef struct node


{
int value;
struct node *next;
}Node;

Node *move_to_front(Node *head)


{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
return head;
}
Answer: q->next = NULL; p->next = head; head = p;
A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue.
To which node should p point such that both the operations enQueue and deQueue can be
performed in constant time?
--- Rear node

What are the time complexities of finding 8th element from beginning and 8th element from end in a
singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8.
--- O(1) and O(n)

Is it possible to create a doubly linked list using only one pointer with every node?
--- Yes, possible by storing XOR of addresses of previous and next nodes.

Which of the following is true about linked list implementation of queue?


A) ---- In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must
be removed from the beginning.
C) Both of the above
D) None of the above

Linked lists in Data Structures and Algorithms have the following characteristics, EXCEPT for one:
----- Deletion is O (1)

Consider the function f defined below.


struct item
{
int data;
struct item * next;
};

int f(struct item *p)


{
return (
(p == NULL) ||
(p->next == NULL) ||
(( p->data <= p->next->data) && f(p->next))
);
}

For a given linked list p, the function f returns 1 if and only if _____
-- The elements in the list are sorted in non-decreasing order of data value

It is the most useful for tasks that can be defined in terms of similar subtasks.
---Recursion

What happens if you try to pop() from an empty stack in Java?


A) It throws a NoSuchElementException
B) It returns null
C) It returns the default value of the element type
D) It removes the last element of the stack

In what scenario would you prefer using a Stack instead of an ArrayList or LinkedList in Java?
A) When elements need to be accessed randomly
B) When elements need to be inserted at the beginning
C) When recursion needs to be implemented
D) When elements need to be sorted in ascending order

Which of the following Java code snippets correctly creates a Queue object using
java.util.LinkedList?
A) Queue<Integer> queue = new Queue<>();
B) Queue<Integer> queue = new LinkedList<>();
C) Queue<Integer> queue = new ArrayList<>();
D) Queue<Integer> queue = new LinkedList<Integer>();

In what scenario would you prefer using a Queue instead of an ArrayList or LinkedList in Java?
A) When elements need to be accessed randomly
B) When elements need to be inserted at the beginning
C) When elements need to be processed in the order they were added
D) When elements need to be sorted in ascending order

What happens when you add elements to a PriorityQueue in Java?


A) Elements are added to the end of the queue
B) Elements are added randomly within the queue
C) Elements are added based on their natural ordering or according to a specified comparator
D) Elements are added in reverse order

What is the purpose of the loop in the given Java program?


A) To display the stack elements after deletion
B) To remove two numbers from the stack
C) To add 5 numbers entered by the user to the stack
D) To initialize the stack with 5 numbers

What does the stack.peek() method do in the context of the given Java program?
A) Adds an element to the top of the stack
B) Removes the top element of the stack
C) Returns the top element of the stack without removing it
D) Checks if the stack is empty

What is the significance of using a Stack data structure in the context of algorithms or applications?
A) It guarantees constant-time access to its elements
B) It ensures elements are processed in the order they are added
C) It supports concurrent access by multiple threads
D) It provides efficient searching and sorting operations

Which of the following represents the final arrangement of the numbers [3, 1, 2, 5, 4] when inserted
consecutively using the PriorityQueue algorithm?
a. [1, 2, 3, 4, 5] b. [5, 4, 3, 2, 1]
c. [1, 3, 2, 5, 4] d. [4, 5, 2, 1, 3]

How does choosing an appropriate data structure affect the performance of algorithms?
A) It has no impact on algorithm performance
B) It can significantly improve or degrade algorithm performance
C) It only affects the readability of the algorithm
D) It determines the algorithm's compatibility with different programming languages

How does the choice of data structure impact the efficiency of algorithms?
A) The data structure has no impact on algorithm efficiency.
B) The data structure can significantly affect the time and space complexity of algorithms.
C) Algorithms are independent of data structures and operate at a constant efficiency.
D) The choice of data structure affects only the readability of the algorithm, not its efficiency.

Which of the following statements best describes an algorithm?


A) An algorithm is a computer program written in high-level programming languages.
B) An algorithm is a set of instructions or rules for calculations or other problem-solving
operations.
C) An algorithm is a physical device used to input data into a computer system.
D) An algorithm is a data structure that stores and organizes information in memory.

How does an algorithm differ from a simple sequence of steps?


A) An algorithm always involves mathematical operations, while a sequence of steps does not.
B) An algorithm is always written in a specific programming language, while a sequence of steps
can be written in any language.
C) An algorithm is a precise and unambiguous set of instructions for solving a problem, while a
sequence of steps may lack such clarity and specificity.
D) An algorithm is typically longer and more complex than a sequence of steps.

Suppose you must design an algorithm to find the maximum element in an array. Describe the
steps you would take to create this algorithm.
A) Identify the problem, break it into smaller subproblems, design a step-by-step solution, and
analyze its efficiency.
B) Start writing code immediately, without planning or considering different approaches.
C) Use trial and error to develop the algorithm, testing different strategies until one works.
D) Consult existing algorithms and modify them slightly to suit your specific requirements.

Design an algorithm to check if a given number is prime. Describe the steps involved in your
algorithm and explain why it is efficient.
A) Start by dividing the number by 2 and continue checking divisibility up to its square root. If
no divisors are found, the number is prime. This algorithm is efficient because it only checks
up to the square root of the number, reducing the number of iterations needed.
B) Generate all possible divisors of the number and check each one individually. If no divisors other
than 1 and the number itself are found, the number is prime. This algorithm is efficient because it
exhaustively searches for divisors, ensuring accuracy.
C) Multiply the number by itself repeatedly until it exceeds the given number. If the result matches
the given number, the number is prime. This algorithm is efficient because it relies on simple
arithmetic operations.
D) Use a brute-force approach to check divisibility by every integer from 2 to the given number
minus one. If no divisors are found, the number is prime. This algorithm is efficient because it
covers all possible cases without skipping any potential divisors.

In a queue data structure, which operation removes an element from the front end?
A) push() B) enqueue()
D) isEmpty() C) dequeue()

Why might a doubly linked list be preferred over a singly linked list for certain applications?
A) Doubly linked lists have a simpler implementation than singly linked lists.
B) Doubly linked lists require less memory compared to singly linked lists.
C) Doubly linked lists allow for bidirectional traversal and easier removal of elements.
D) Doubly linked lists have better performance for search operations than singly linked lists.

Which of the following points is/are true about Linked List data structure when it is compared with
array?
A) Arrays have better cache locality that can make them better in terms of performance.
B) It is easy to insert and delete elements in Linked List
C) Random access is not allowed in a typical implementation of Linked Lists and the size of
array has to be pre-decided, linked lists can change their size any time.
D) All of the above

Is it possible to create a doubly linked list using only one pointer with every node?
A) Not possible
B) Yes, possible by storing XOR of addresses of previous and next nodes.
C) Yes, possible by storing XOR of current node and next node
D) Yes, possible by storing XOR of current node and previous node

You might also like