Data Structure Multiple Choice Questions and Answers
Data Structure Multiple Choice Questions and Answers
main()
{
char str[]="san foundry";
int len = strlen(str);
int i;
for(i=0;i<len;i++)
push(str[i]); // pushes an element into stack
for(i=0;i<len;i++)
pop(); //pops an element from the stack
}
a) yrdnuof nas
b) foundry nas
c) sanfoundry
d) san foundry
View Answer
Answer: a
Explanation: First, the string ‘san foundry’ is pushed one by one into the stack.
When it is popped, the output will be as ‘yrdnuof nas’.
24. Which of the following data structure can provide efficient searching of the elements?
a) binary search tree
b) unordered lists
c) 2-3 tree
d) treap
View Answer
Answer: c
Explanation: The average case time for lookup in a binary search tree, treap and 2-3 tree is O(log n) and
in unordered lists it is O(n). But in the worst case, only the 2-3 trees perform lookup efficiently as it takes
O(log n), while others take O(n).
25. What is an AVL tree?
a) a tree which is unbalanced and is a height balanced tree
b) a tree which is balanced and is a height balanced tree
c) a tree with atmost 3 children
d) a tree with three children
View Answer
Answer: b
Explanation: It is a self balancing tree with height difference atmost 1.
26. What is the time complexity for searching a key or integer in Van Emde Boas data structure?
a) O (M!)
b) O (log M!)
c) O (log (log M))
d) O (M2)
View Answer
Answer: c
Explanation: In order to search a key or integer in the Van Emde Boas data structure, the operation can
be performed on an associative array. Hence, the time complexity for searching a key or integer in Van
Emde Boas data structure is O (log (log M)).
27. The optimal data structure used to solve Tower of Hanoi is _________
a) Tree
b) Heap
c) Priority queue
d) Stack
View Answer
Answer: d
Explanation: The Tower of Hanoi involves moving of disks ‘stacked’ at one peg to another peg with
respect to the size constraint. It is conveniently done using stacks and priority queues. Stack approach is
widely used to solve Tower of Hanoi.
28. What is the use of the bin data structure?
a) to have efficient traversal
b) to have efficient region query
c) to have efficient deletion
d) to have efficient insertion
View Answer
Answer: b
Explanation: Bin data structure allows us to have efficient region queries. A frequency of bin is increased
by one each time a data point falls into a bin.
29. Which is the most appropriate data structure for reversing a word?
a) stack
b) queue
c) graph
d) tree
View Answer
Answer: a
Explanation: Stack is the most appropriate data structure for reversing a word because stack follows LIFO
principle.
30. What is the functionality of the following piece of code?
a) 4 and 2
b) 2 and 4
c) 5 and 3
d) 3 and 5
View Answer
Answer: d
Explanation: Array indexing starts from 0.
37. In simple chaining, what data structure is appropriate?
a) Doubly linked list
b) Circular linked list
c) Singly linked list
d) Binary trees
View Answer
Answer: a
Explanation: Deletion becomes easier with doubly linked list, hence it is appropriate.
a) 3 and 5
b) 5 and 3
c) 2 and 4
d) 4 and 2
View Answer
Answer: a
Explanation: Array indexing starts from 0.
6. What is the output of the following Java code?
a) 4
b) 5
c) ArrayIndexOutOfBoundsException
d) InavlidInputException
View Answer
Answer: c
Explanation: Trying to access an element beyond the limits of an array gives
ArrayIndexOutOfBoundsException.
7. When does the ArrayIndexOutOfBoundsException occur?
a) Compile-time
b) Run-time
c) Not an error
d) Not an exception at all
View Answer
Answer: b
Explanation: ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free.
8. Which of the following concepts make extensive use of arrays?
a) Binary trees
b) Scheduling of processes
c) Caching
d) Spatial locality
View Answer
Answer: d
Explanation: Whenever a particular memory location is referred to, it is likely that the locations nearby are
also referred, arrays are stored as contiguous blocks in memory, so if you want to access array elements,
spatial locality makes it to access quickly.
9. What are the advantages of arrays?
a) Objects of mixed data types can be stored
b) Elements in an array cannot be sorted
c) Index of first element of an array is 1
d) Easier to store elements of same data type
View Answer
Answer: d
Explanation: Arrays store elements of the same data type and present in continuous memory locations.
10. What are the disadvantages of arrays?
a) Data structure like queue or stack cannot be implemented
b) There are chances of wastage of memory space if elements inserted in an array are lesser than the
allocated size
c) Index value of an array can be negative
d) Elements are sequentially accessed
View Answer
Answer: b
Explanation: Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied
positions can’t be used again. Wastage will occur in memory.
11. Assuming int is of 4bytes, what is the size of int arr[15];?
a) 15
b) 19
c) 11
d) 60
View Answer
Answer: d
Explanation: Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.
12. In general, the index of the first element in an array is __________
a) 0
b) -1
c) 2
d) 1
View Answer
Answer: a
Explanation: In general, Array Indexing starts from 0. Thus, the index of the first element in an array is 0.
13. Elements in an array are accessed _____________
a) randomly
b) sequentially
c) exponentially
d) logarithmically
View Answer
Answer: a
Explanation: Elements in an array are accessed randomly. In Linked lists, elements are accessed
sequentially.
Stack
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Stack”.
Stack Operations – 2
This set of Data Structure Interview Questions and Answers focuses on “Stack Operations – 2”.
Stack Operations – 3
This set of Data Structure Questions and Answers for Freshers focuses on “Stack Operations – 3”.
(A + B ⋀D)/(E – F)+G
2. Convert the following infix expressions into its equivalent postfix expressions.
a) (A B D ⋀ + E F – / G +)
c) (A B D ⋀ + E F/- G +)
b) (A B D +⋀ E F – / G +)
d) (A B D E F + ⋀ / – G +)
View Answer
Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
After the completion of all operation, the number of elements present in stack is?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: a
Explanation: Number of elements present in stack is equal to the difference between number of push
operations and number of pop operations. Number of elements is 5-4=1.
6. Which of the following is not an inherent application of stack?
a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling
View Answer
Answer: d
Explanation: Job Scheduling is not performed using stacks.
7. The type of expression in which operator succeeds its operands is?
a) Infix Expression
b) Prefix Expression
c) Postfix Expression
d) Both Prefix and Postfix Expressions
View Answer
Answer: c
Explanation: The expression in which operator succeeds its operands is called postfix expression. The
expression in which operator precedes the operands is called prefix expression. If an operator is present
between two operands, then it is called infix expressions.
8. Assume that the operators +,-, x are left associative and ^ is right associative. The order of precedence
(from highest to lowest) is ^, x, +, -. The postfix expression for the infix expression a + b x c – d ^ e ^ f is?
a) a b c x + d e f ^ ^ –
b) a b c x + d e ^ f ^ –
c) a b + c x d – e ^ f ^
d) – + a x b c ^ ^ d e f
View Answer
Answer: a
Explanation: Given Infix Expression is a + b x c – d ^ e ^ f. And ^ is right associative. Thus, the final
postfix expression is a b c x + d e f ^ ^ –
9. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what is the
order of removal?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
View Answer
Answer: b
Explanation: Stack follows LIFO(Last In First Out). So the removal order of elements are DCBA.
Queue Operations
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Queue Operations”.
1. A linear list of elements in which deletion can be done from one end (front) and insertion can take place
only at the other end (rear) is known as _____________
a) Queue
b) Stack
c) Tree
d) Linked list
View Answer
Answer: a
Explanation: Linear list of elements in which deletion is done at front side and insertion at rear side is
called Queue. In stack we will delete the last entered element first.
2. The data structure required for Breadth First Traversal on a graph is?
a) Stack
b) Array
c) Queue
d) Tree
View Answer
Answer: c
Explanation: In Breadth First Search Traversal, BFS, starting vertex is first taken and adjacent vertices
which are unvisited are also taken. Again, the first vertex which was added as an unvisited adjacent
vertex list will be considered to add further unvisited vertices of the graph. To get the first unvisited vertex
we need to follows First In First Out principle. Queue uses FIFO principle.
3. A queue follows __________
a) FIFO (First In First Out) principle
b) LIFO (Last In First Out) principle
c) Ordered array
d) Linear tree
View Answer
Answer: a
Explanation: Element first added in queue will be deleted first which is FIFO principle.
advertisement
4. Circular Queue is also known as ________
a) Ring Buffer
b) Square Buffer
c) Rectangle Buffer
d) Curve Buffer
View Answer
Answer: a
Explanation: Circular Queue is also called as Ring Buffer. Circular Queue is a linear data structure in
which last position is connected back to the first position to make a circle. It forms a ring structure.
5. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in what order
will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
View Answer
Answer: a
Explanation: Queue follows FIFO approach. i.e. First in First Out Approach. So, the order of removal
elements are ABCD.
Subscribe Now: Data Structure Newsletter | Important Subjects Newsletters
6. A data structure in which elements can be inserted or deleted at/from both ends but not in the middle
is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue
View Answer
Answer: c
Explanation: In dequeuer, we can insert or delete elements from both the ends. In queue, we will follow
first in first out principle for insertion and deletion of elements. Element with least priority will be deleted in
a priority queue.
7. A normal queue, if implemented using an array of size MAX_SIZE, gets full when?
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
View Answer
Answer: a
Explanation: When Rear = MAX_SIZE – 1, there will be no space left for the elements to be added in
queue. Thus queue becomes full.
8. Queues serve major role in ______________
a) Simulation of recursion
b) Simulation of arbitrary linked list
c) Simulation of limited resource allocation
d) Simulation of heap sort
View Answer
Answer: c
Explanation: Simulation of recursion uses stack data structure. Simulation of arbitrary linked lists uses
linked lists. Simulation of resource allocation uses queue as first entered data needs to be given first
priority during resource allocation. Simulation of heap sort uses heap data structure.
9. Which of the following is not the type of queue?
a) Ordinary queue
b) Single ended queue
c) Circular queue
d) Priority queue
View Answer
Answer: b
Explanation: Queue always has two ends. So, single ended queue is not the type of queue.
1. A linear collection of data elements where the linear node is given by means of pointer is called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list
View Answer
Answer: a
Explanation: In Linked list each node has its own data and the address of next node. These nodes are
linked by using pointers. Node list is an object that consists of a list of all nodes in a document with in a
particular selected set of nodes.
2. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head
pointer only. Given the representation, which of the following operation can be implemented in O(1) time?
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
View Answer
Answer: b
Explanation: We know the head node in the given linked list. Insertion and deletion of elements at the
front of the linked list completes in O (1) time whereas for insertion and deletion at the last node requires
to traverse through every node in the linked list. Suppose there are n elements in a linked list, we need to
traverse through each node. Hence time complexity becomes O(n).
advertisement
3. In linked list each node contains a minimum of two fields. One field is data field to store the data
second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
View Answer
Answer: c
Explanation: Each node in a linked list contains data and a pointer (reference) to the next node. Second
field contains pointer to node.
Note: Join free Sanfoundry classes at Telegram or Youtube
4. What would be the asymptotic time complexity to add a node at the end of singly linked list, if the
pointer is initially pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) Both O(n) and θ(n)
View Answer
Answer: d
Explanation: In case of a linked list having n elements, we need to travel through every node of the list to
add the element at the end of the list. Thus asymptotic time complexity is both θ(n) and O(n). Θ(n)
represents the tight bound of the algorithm’s time complexity, meaning it captures the best, average, and
worst-case scenarios that are all linear in this case. O(n) signifies the upper bound, indicating the worst-
case scenario is no worse than linear.
5. What would be the asymptotic time complexity to insert an element at the front of the linked list (head is
known)?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: a
Explanation: To add an element at the front of the linked list, we will create a new node which holds the
data to be added to the linked list and pointer which points to head position in the linked list. The entire
thing happens within O (1) time. Thus the asymptotic time complexity is O (1).
6. What would be the asymptotic time complexity to find an element in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n4)
View Answer
Answer: b
Explanation: If the required element is in the last position, we need to traverse the entire linked list. This
will take O (n) time to search the element.
7. What would be the asymptotic time complexity to insert an element at the second position in the linked
list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: a
Explanation: A new node is created with the required element. The pointer of the new node points the
node to which the head node of the linked list is also pointing. The head node pointer is changed and it
points to the new node which we created earlier. The entire process completes in O (1) time. Thus the
asymptotic time complexity to insert an element in the second position of the linked list is O (1).
8. The concatenation of two lists can be performed in O(1) time. Which of the following variation of the
linked list can be used?
a) Singly linked list
b) Doubly linked list
c) Circular doubly linked list
d) Array implementation of list
View Answer
Answer: c
Explanation: We can easily concatenate two lists in O (1) time using singly or doubly linked list, provided
that we have a pointer to the last node at least one of the lists. But in case of circular doubly linked lists,
we will break the link in both the lists and hook them together. Thus circular doubly linked list
concatenates two lists in O (1) time.
9. Consider the following definition in c programming language.
struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
1. What kind of linked list is best to answer questions like “What is the item at position n?”
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Array implementation of linked list
View Answer
Answer: d
Explanation: Arrays provide random access to elements by providing the index value within square
brackets. In the linked list, we need to traverse through each element until we reach the nth position. Time
taken to access an element represented in arrays is less than the singly, doubly and circular linked lists.
Thus, array implementation is used to access the item at the position n.
2. Linked lists are not suitable for the implementation of ___________
a) Insertion sort
b) Radix sort
c) Polynomial manipulation
d) Binary search
View Answer
Answer: d
Explanation: It cannot be implemented using linked lists.
3. Linked list is considered as an example of ___________ type of memory allocation.
a) Dynamic
b) Static
c) Compile time
d) Heap
View Answer
Answer: a
Explanation: As memory is allocated at the run time.
advertisement
4. In Linked List implementation, a node carries information regarding ___________
a) Data
b) Link
c) Data and Link
d) Node
View Answer
Answer: c
Explanation: A linked list is a collection of objects linked together by references from an object to another
object. By convention these objects are names as nodes. Linked list consists of nodes where each node
contains one or more data fields and a reference(link) to the next node.
5. Linked list data structure offers considerable saving in _____________
a) Computational Time
b) Space Utilization
c) Space Utilization and Computational Time
d) Speed Utilization
View Answer
Answer: c
Explanation: Linked lists saves both space and time.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
6. Which of the following points is/are not true about Linked List data structure when it is compared with
an 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
d) Access of elements in linked list takes less time than compared to arrays
View Answer
Answer: d
Explanation: To access an element in a linked list, we need to traverse every element until we reach the
desired element. This will take more time than arrays as arrays provide random access to its elements.
7. What does the following function do for a given Linked List with first node as head?
1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at
the end of the function.
What should be added in place of “/*ADD A STATEMENT HERE*/”, so that the function correctly reverses
a linked list.
a) *head_ref = prev;
b) *head_ref = current;
c) *head_ref = next;
d) *head_ref = NULL;
View Answer
Answer: a
Explanation: *head_ref = prev; At the end of while loop, the prev pointer points to the last node of original
linked list.
We need to change *head_ref so that the head pointer now starts pointing to the last node.
advertisement
2. What is the output of following function for start pointing to first node of following linked list?
1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
c) 1 2 3 5
d) 1 3 5 5 3 1
View Answer
Answer: d
Explanation: fun() prints alternate nodes of the given Linked List, first from head to end, and then from
end to head.
If Linked List has even number of nodes, then skips the last node.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
3. The following C function takes a simply-linked list as an 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.
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}
a) 1, 2, 3, 4, 5, 6, 7
b) 2, 1, 4, 3, 6, 5, 7
c) 1, 3, 2, 5, 4, 7, 6
d) 2, 3, 4, 5, 6, 7, 1
View Answer
Answer: b
Explanation: The function rearrange() exchanges data of every node with its next node. It starts
exchanging data from the first node itself.
5. In the worst case, the number of comparisons needed to search a singly linked list of length n for a
given element is?
a) log 2 n
b) n⁄2
c) log 2 n – 1
d) n
View Answer
Answer: d
Explanation: In the worst case, the element to be searched has to be compared with all elements of the
linked list.
6. Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not
given, can we delete the node X from given linked list?
a) Possible if X is not last node
b) Possible if size of linked list is even
c) Possible if size of linked list is odd
d) Possible if X is not first node
View Answer
Answer: a
Explanation: Following are simple steps.
struct node *temp = X->next;
X->data = temp->data;
X->next = temp->next;
free(temp);
7. You are given pointers to first and last nodes of a singly linked list, which of the following operations are
dependent on the length of the linked list?
a) Delete the first element
b) Insert a new element as a first element
c) Delete the last element of the list
d) Add a new element at the end of the list
View Answer
Answer: c
Explanation: Deletion of the first element of the list is done in O (1) time by deleting memory and changing
the first pointer.
Insertion of an element as a first element can be done in O (1) time. We will create a node that holds data
and points to the head of the given linked list. The head pointer was changed to a newly created node.
Deletion of the last element requires a pointer to the previous node of last, which can only be obtained by
traversing the list. This requires the length of the linked list.
Adding a new element at the end of the list can be done in O (1) by changing the pointer of the last node
to the newly created node and last is changed to a newly created node.
8. In the worst case, the number of comparisons needed to search a singly linked list of length n for a
given element is?
a) log2 n
b) n⁄2
c) log2 n – 1
d) n
View Answer
Answer: d
Explanation: The worst-case happens if the required element is at last or the element is absent in the list.
For this, we need to compare every element in the linked list. If n elements are there, n comparisons will
happen in the worst case.
Linked List
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Linked Lists”,
covering topics such as linked list operations, singly linked lists, doubly linked lists, and circular linked
lists.
class Node
{
protected Node next;
protected Object ele;
Node(Object e,Node n)
{
ele = e;
next = n;
}
public void setNext(Node n)
{
next = n;
}
public void setEle(Object e)
{
ele = e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class SLL
{
Node head;
int size;
SLL()
{
size = 0;
}
}
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Since you have to traverse to the end of the list and delete the last node, you need two
reference pointers. ‘cur’ to traverse all the way and find the last node, and ‘temp’ is a trailing pointer to
‘cur’. Once you reach the end of the list, setNext of ‘temp’ to null, ‘cur’ is not being pointed to by any node,
and hence it is available for garbage collection.
b)
c)
d)
View Answer
Answer: a
Explanation: Loop through the list to get into position one behind the actual position given.
temp.setNext(temp.getNext().getNext()) will delete the specified node.
b)
c)
d)
b)
c)
d)
View Answer
Answer: a
Explanation: Set the ‘next’ pointer point to the head of the list and then make this new node as the head.
a)
advertisement
public void insertRear(int data)
{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().setNext(node);
tail.setPrev(node);
length++;
}
b)
c)
d)
View Answer
Answer: a
Explanation: First create a new node whose ‘prev’ points to the node pointed to by the ‘prev’ of tail. The
‘next’ of the new node should point to tail. Set the ‘prev’ of tail to point to new node and the ‘prev’ of new
node to point to the new node.
b)
c)
d)
View Answer
Answer: a
Explanation: If the position to be deleted is not the head, advance to the given position and manipulate
the previous and next pointers of next and previous nodes respectively.
5. How do you calculate the pointer difference in a memory efficient double linked list?
a) head xor tail
b) pointer to previous node xor pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node
View Answer
Answer: b
Explanation: The pointer difference is calculated by taking XOR of pointer to previous node and pointer to
the next node.
6. What is the worst case time complexity of inserting a node in a doubly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
View Answer
Answer: c
Explanation: In the worst case, the position to be inserted maybe at the end of the list, hence you have to
traverse through the entire list to get to the correct position, hence O(n).
7. How do you insert a node at the beginning of the list?
a)
b)
c)
d)
View Answer
Answer: a
Explanation: The new node’s previous pointer will point to head and next pointer will point to the current
next of head.
8. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after performing the
given sequence of operations?
a) head-0-1-2-3-4-5-6-tail
b) head-1-2-3-4-5-6-tail
c) head-6-1-2-3-4-5-0-tail
d) head-0-1-2-3-4-5-tail
View Answer
Answer: c
Explanation: The given sequence of operations performs addition of nodes at the head and tail of the list.
9. What is the functionality of the following piece of code?
a) Return the element at the tail of the list but do not remove it
b) Return the element at the tail of the list and remove it from the list
c) Return the last but one element from the list but do not remove it
d) Return the last but one element at the tail of the list and remove it from the list
View Answer
Answer: d
Explanation: The previous of the tail/last-node and the next of the 3rd last node are manipulated, this
suggests that the second last node (last but one element) is being removed from the list.
10. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after performing the
given sequence of operations?
a) head-6-1-2-3-4-5-tail
b) head-6-1-2-3-4-tail
c) head-1-2-3-4-5-6-tail
d) head-1-2-3-4-5-tail
View Answer
Answer: b
Explanation: A new node is added to the head of the list and a node is deleted from the tail end of the list.
b)
advertisement
public int length(Node head)
{
int length = 0;
if( head == null)
return 0;
Node temp = head.getNext();
while(temp != null)
{
temp = temp.getNext();
length++;
}
return length;
}
c)
d)
View Answer
Answer: a
Explanation: If the head is null, it means that the list is empty. Otherwise, traverse the list until the head of
the list is reached.
3. What is the functionality of the following piece of code? Select the most appropriate.
b)
c)
d)
7. What is the functionality of the following code? Choose the most appropriate answer.
1. Which of the following real world scenarios would you associate with a stack data structure?
a) piling up of chairs one above the other
b) people standing in a line to be serviced at a counter
c) offer services based on the priority of the customer
d) tatkal Ticket Booking in IRCTC
View Answer
Answer: a
Explanation: Stack follows Last In First Out (LIFO) policy. Piling up of chairs one above the other is based
on LIFO, people standing in a line is a queue and if the service is based on priority, then it can be
associated with a priority queue. Tatkal Ticket Booking Follows First in First Out Policy. People who click
the book now first will enter the booking page first.
2. What does the following function check for? (all necessary headers to be included and function is
called from main)
#define MAX 10
a) full stack
b) invalid index
c) empty stack
d) infinite stack
View Answer
Answer: c
Explanation: An empty stack is represented with the top-of-the-stack(‘top’ in this case) to be equal to -1.
advertisement
3. What does ‘stack underflow’ refer to?
a) accessing item from an undefined stack
b) adding items to a full stack
c) removing items from an empty stack
d) index out of bounds exception
View Answer
Answer: c
Explanation: Removing items from an empty stack is termed as stack underflow.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
4. What is the output of the following program?
public Stack()
{
stk = new Object[CAPACITY];
}
a) stack is full
b) 20
c) 0
d) -999
View Answer
Answer: d
Explanation: The first call to pop() returns 10, whereas the second call to pop() would result in stack
underflow and the program returns -999.
5. What is the time complexity of pop() operation when the stack is implemented using an array?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)
View Answer
Answer: a
Explanation: pop() accesses only one end of the structure, and hence constant time.
6. Which of the following array position will be occupied by a new element being pushed for a stack of
size N elements(capacity of stack > N)?
a) S[N-1]
b) S[N]
c) S[1]
d) S[0]
View Answer
Answer: b
Explanation: Elements are pushed at the end, hence N.
7. What happens when you pop from an empty stack while implementing using the Stack ADT in Java?
a) Undefined error
b) Compiler displays a warning
c) EmptyStackException is thrown
d) NoStackException is thrown
View Answer
Answer: c
Explanation: The Stack ADT throws an EmptyStackException if the stack is empty and a pop() operation
is tried on it.
8. What is the functionality of the following piece of Java code?
Assume: ‘a’ is a non empty array of integers, the Stack class creates an array of specified size and
provides a top pointer indicating TOS(top of stack), push and pop have normal meaning.
1. What is the best case time complexity of deleting a node in a Singly Linked list?
a) O (n)
b) O (n2)
c) O (nlogn)
d) O (1)
View Answer
Answer: d
Explanation: Deletion of the head node in the linked list is taken as the best case. The successor of the
head node is changed to head and deletes the predecessor of the newly assigned head node. This
process completes in O(1) time.
2. Which of the following statements are not correct with respect to Singly Linked List(SLL) and Doubly
Linked List(DLL)?
a) Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1) in DLL
b) SLL uses lesser memory per node than DLL
c) DLL has more searching power than SLL
d) Number of node fields in SLL is more than DLL
View Answer
Answer: d
Explanation: To insert and delete at known positions requires complete traversal of the list in worst case
in SLL, SLL consists of an item and a node field, while DLL has an item and two node fields, hence SLL
occupies lesser memory, DLL can be traversed both ways(left and right), while SLL can traverse in only
one direction, hence more searching power of DLL. Node fields in SLL is 2 (data and address of next
node) whereas in DLL is 3(data, address to next node, address to previous node).
3. Given below is the Node class to perform basic list operations and a Stack class with a no arg
constructor.
Select from the options the appropriate pop() operation that can be included in the Stack class. Also ‘first’
is the top-of-the-stack.
advertisement
class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}
a)
b)
public Object pop()
{
if(size == 0)
System.out.println("underflow");
else
{
Object o = first.getEle();
first = first.getNext().getNext();
size--;
return o;
}
}
c)
d)
View Answer
Answer: a
Explanation: pop() should return the Object pointed to by the node ‘first’. The sequence of operations is,
first, get the element stored at node ‘first’ using getEle(), and second, make the node point to the next
node using getNext().
class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}
a)
b)
c)
View Answer
Answer: a
Explanation: To push an element into the stack, first create a new node with the next pointer point to the
current top-of-the-stack node, then make this node as top-of-the-stack by assigning it to ‘first’.
push(20);
push(4);
top();
pop();
pop();
push(5);
top();
a) 20
b) 4
c) stack underflow
d) 5
View Answer
Answer: d
Explanation: 20 and 4 which were pushed are popped by the two pop() statements, the recent push() is 5,
hence top() returns 5.
9. Which of the following data structures can be used for parentheses matching?
a) n-ary tree
b) queue
c) priority queue
d) stack
View Answer
Answer: d
Explanation: For every opening brace, push it into the stack, and for every closing brace, pop it off the
stack. Do not take action for any other character. In the end, if the stack is empty, then the input has
balanced parentheses.
10. Minimum number of queues to implement stack is ___________
a) 3
b) 4
c) 1
d) 2
View Answer
Answer: c
Explanation: Use one queue and one counter to count the number of elements in the queue.
a) Dequeue
b) Enqueue
c) Return the front element
d) Return the last element
View Answer
Answer: c
Explanation: q[front] gives the element at the front of the queue, since we are not moving the ‘front’ to the
next element,
it is not a dequeue operation.
6. What is the need for a circular queue?
a) effective usage of memory
b) easier computations
c) to delete elements based on priority
d) implement LIFO principle in queues
View Answer
Answer: a
Explanation: In a linear queue, dequeue operation causes the starting elements of the array to be empty,
and there is no way you can use that space, while in a circular queue, you can effectively use that space.
Priority queue is used to delete the elements based on their priority. Higher priority elements will be
deleted first whereas lower priority elements will be deleted next. Queue data structure always follows
FIFO principle.
7. Which of the following represents a dequeue operation? (count is the number of elements in the queue)
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Dequeue removes the first element from the queue, ‘front’ points to the front end of the
queue and returns the first element.
8. Which of the following best describes the growth of a linear queue at runtime? (Q is the original queue,
size() returns the number of elements in the queue)
a)
b)
c)
d)
View Answer
Answer: a
Explanation: A common technique to expand the size of array at run time is simply to double the size.
Create a new array of double the previous size and copy all the elements, after copying do not forget to
assign front = 0 and rear = size()-1, as these are necessary to maintain the decorum of the queue
operations.
public CircularQueue()
{
this(CAPACITY);
}
public CircularQueue (int n)
{
size = n;
front = 0;
rear = 0;
q = new Object[size];
}
a) 3 3
b) 3 6
c) 6 6
d) 10 6
View Answer
Answer: a
Explanation: First enqueue 10 and 3 into the queue, followed by a dequeue(removes 10), followed by an
enqueue(6), At this point, 3 is at the front end of the queue and 6 at the rear end, hence a call to
frontElement() will return 3 which is displayed twice.
1. In linked list implementation of queue, if only front pointer is maintained, which of the following
operation take worst case linear time?
a) Insertion
b) Deletion
c) To empty a queue
d) Both Insertion and To empty a queue
View Answer
Answer: d
Explanation: Since front pointer is used for deletion, so worst time for the other two cases.
2. In linked list implementation of a queue, where does a new element be inserted?
a) At the head of link list
b) At the centre position in the link list
c) At the tail of the link list
d) At any position in the linked list
View Answer
Answer: c
Explanation: Since queue follows FIFO so new element inserted at last.
3. In linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will
change during an insertion into a NONEMPTY queue?
a) Only front pointer
b) Only rear pointer
c) Both front and rear pointer
d) No pointer will be changed
View Answer
Answer: b
Explanation: Since queue follows FIFO so new element inserted at last.
advertisement
4. In linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will
change during an insertion into EMPTY queue?
a) Only front pointer
b) Only rear pointer
c) Both front and rear pointer
d) No pointer will be changed
View Answer
Answer: c
Explanation: Since its the starting of queue, so both values are changed.
5. In case of insertion into a linked queue, a node borrowed from the __________ list is inserted in the
queue.
a) AVAIL
b) FRONT
c) REAR
d) NULL
View Answer
Answer: a
Explanation: All the nodes are collected in AVAIL list.
Subscribe Now: Data Structure Newsletter | Important Subjects Newsletters
6. In linked list implementation of a queue, from where is the item deleted?
a) At the head of link list
b) At the centre position in the link list
c) At the tail of the link list
d) Node before the tail
View Answer
Answer: a
Explanation: Since queue follows FIFO so new element deleted from first.
7. In linked list implementation of a queue, the important condition for a queue to be empty is?
a) FRONT is null
b) REAR is null
c) LINK is empty
d) FRONT==REAR-1
View Answer
Answer: a
Explanation: Because front represents the deleted nodes.
8. The essential condition which is checked before insertion in a linked queue is?
a) Underflow
b) Overflow
c) Front value
d) Rear value
View Answer
Answer: b
Explanation: To check whether there is space in the queue or not.
9. The essential condition which is checked before deletion in a linked queue is?
a) Underflow
b) Overflow
c) Front value
d) Rear value
View Answer
Answer: a
Explanation: To check whether there is element in the list or not.
10. 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 beginning, then in pop operation, nodes must be
removed from the beginning
c) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed
from end
d) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed
from beginning
View Answer
Answer: a
Explanation: It can be done by both the methods.
Priority Queue
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Priority Queue”.
advertisement
public void insert_key(int key,Object item)
{
if(key<0)
{
Systerm.our.println("invalid");
System.exit(0);
}
else
{
Node temp = new Node(key,item,null);
if(count == 0)
{
head.setNext(temp);
temp.setNext(trail);
}
else
{
Node dup = head.getNext();
Node cur = head;
while((key>dup.getKey()) && (dup!=trail))
{
dup = dup.getNext();
cur = cur.getNext();
}
cur.setNext(temp);
temp.setNext(dup);
}
count++;
}
}
b)
c)
d)
View Answer
Answer: a
Explanation: Have two temporary pointers ‘dup’ and ‘cur’ with ‘cur’ trailing behind ‘dup’. Traverse through
the list until the given key is greater than some element with a lesser key, insert the new node ‘temp’ in
that position.
4. What is the time complexity to insert a node based on key in a priority queue?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explanation: In the worst case, you might have to traverse the entire list.
5. What is the functionality of the following piece of code?
1. What is a dequeue?
a) A queue with insert/delete defined for both front and rear ends of the queue
b) A queue implemented with a doubly linked list
c) A queue implemented with both singly and doubly linked lists
d) A queue with insert/delete defined for front side of the queue
View Answer
Answer: a
Explanation: A dequeue or a double ended queue is a queue with insert/delete defined for both front and
rear ends of the queue.
2. Select the function which performs insertion at the front end of the dequeue?
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Create a new node, if the current list is empty, the ‘head’ points to this node and this new
node points to ‘trail’. Otherwise, ‘head’ points to the new node and this in turn points to the current first
element(head.getNext()).
3. What is the functionality of the following piece of code?
public void function(Object item)
{
Node temp=new Node(item,trail);
if(isEmpty())
{
head.setNext(temp);
temp.setNext(trail);
}
else
{
Node cur=head.getNext();
while(cur.getNext()!=trail)
{
cur=cur.getNext();
}
cur.setNext(temp);
}
size++;
}
c)
d)
View Answer
Answer: b
Explanation: Have two pointers, one(temp) pointing to the first element and the other(cur) pointing to the
second element. Make the ‘head’ point to the second element, this removes all reference for ‘temp’.
6. Which of the following can be used to delete an element from the rear end of the queue?
a)
public Object deleteRear() throws emptyDEQException
{
if(isEmpty())
throw new emptyDEQException("Empty");
else
{
Node temp = head.getNext();
Node cur = temp;
while(temp.getNext() != trail)
{
temp = temp.getNext();
cur = cur.getNext();
}
Object e = temp.getEle();
cur.setNext(trail);
size--;
return e;
}
}
b)
c)
d)
View Answer
Answer: c
Explanation: Traverse till the end of the list with a pointer ‘temp’ and another ‘cur’ which is trailing behind
temp, make ‘cur’ point to trail, this removes all reference for ‘temp’.
7. What is the time complexity of deleting from the rear end of the dequeue implemented with a singly
linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explanation: Since a singly linked list is used, first you have to traverse till the end, so the complexity is
O(n).
8. After performing these set of operations, what does the final list look contain?
InsertFront(10);
InsertFront(20);
InsertRear(30);
DeleteFront();
InsertRear(40);
InsertRear(10);
DeleteRear();
InsertRear(15);
display();
a) 10 30 10 15
b) 20 30 40 15
c) 20 30 40 10
d) 10 30 40 15
View Answer
Answer: d
Explanation: A careful tracing of the given operation yields the result.
10
20 10
20 10 30
10 30
10 30 40
10 30 40 10
10 30 40
10 30 40 15
1. A Double-ended queue supports operations such as adding and removing items from both the sides of
the queue. They support four operations like addFront(adding item to top of the queue), addRear(adding
item to the bottom of the queue), removeFront(removing item from the top of the queue) and
removeRear(removing item from the bottom of the queue). You are given only stacks to implement this
data structure. You can implement only push and pop operations. What are the total number of stacks
required for this operation?(you can reuse the stack)
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: The addFront and removeFront operations can be performed using one stack itself as push
and pop are supported (adding and removing element from top of the stack) but to perform addRear and
removeRear you need to pop each element from the current stack and push it into another stack, push or
pop the element as per the asked operation from this stack and in the end pop elements from this stack to
the first stack.
2. You are asked to perform a queue operation using a stack. Assume the size of the stack is some value
‘n’ and there are ‘m’ number of variables in this stack. The time complexity of performing deQueue
operation is (Using only stack operations like push and pop)(Tightly bound).
a) O(m)
b) O(n)
c) O(m*n)
d) Data is insufficient
View Answer
Answer: a
Explanation: To perform deQueue operation you need to pop each element from the first stack and push it
into the second stack. In this case you need to pop ‘m’ times and need to perform push operations also
‘m’ times. Then you pop the first element from this second stack (constant time) and pass all the elements
to the first stack (as done in the beginning)(‘m-1’ times). Therfore the time complexity is O(m).
3. Consider you have an array of some random size. You need to perform dequeue operation. You can
perform it using stack operation (push and pop) or using queue operations itself (enQueue and Dequeue).
The output is guaranteed to be same. Find some differences?
a) They will have different time complexities
b) The memory used will not be different
c) There are chances that output might be different
d) No differences
View Answer
Answer: a
Explanation: To perform operations such as Dequeue using stack operation you need to empty all the
elements from the current stack and push it into the next stack, resulting in a O(number of elements)
complexity whereas the time complexity of dequeue operation itself is O(1). And there is a need of a extra
stack. Therefore more memory is needed.
advertisement
4. Consider you have a stack whose elements in it are as follows.
5 4 3 2 << top
Where the top element is 2.
You need to get the following stack
6 5 4 3 2 << top
The operations that needed to be performed are (You can perform only push and pop):
a) Push(pop()), push(6), push(pop())
b) Push(pop()), push(6)
c) Push(pop()), push(pop()), push(6)
d) Push(6)
View Answer
Answer: a
Explanation: By performing push(pop()) on all elements on the current stack to the next stack you get 2 3
4 5 << top.Push(6) and perform push(pop()) you’ll get back 6 5 4 3 2 << top. You have actually performed
enQueue operation using push and pop.
5. A double-ended queue supports operations like adding and removing items from both the sides of the
queue. They support four operations like addFront(adding item to top of the queue), addRear(adding item
to the bottom of the queue), removeFront(removing item from the top of the queue) and
removeRear(removing item from the bottom of the queue). You are given only stacks to implement this
data structure. You can implement only push and pop operations. What’s the time complexity of
performing addFront and addRear? (Assume ‘m’ to be the size of the stack and ‘n’ to be the number of
elements)
a) O(m) and O(n)
b) O(1) and O(n)
c) O(n) and O(1)
d) O(n) and O(m)
View Answer
Answer: b
Explanation: addFront is just a normal push operation. Push operation is of O(1). Whereas addRear is of
O(n) as it requires two push(pop()) operations of all elements of a stack.
Note: Join free Sanfoundry classes at Telegram or Youtube
6. Why is implementation of stack operations on queues not feasible for a large dataset (Asssume the
number of elements in the stack to be n)?
a) Because of its time complexity O(n)
b) Because of its time complexity O(log(n))
c) Extra memory is not required
d) There are no problems
View Answer
Answer: a
Explanation: To perform Queue operations such as enQueue and deQueue there is a need of emptying
all the elements of a current stack and pushing elements into the next stack and vice versa. Therfore it
has a time complexity of O(n) and the need of extra stack as well, may not be feasible for a large dataset.
7. Consider yourself to be in a planet where the computational power of chips to be slow. You have an
array of size 10.You want to perform enqueue some element into this array. But you can perform only
push and pop operations .Push and pop operation both take 1 sec respectively. The total time required to
perform enQueue operation is?
a) 20
b) 40
c) 42
d) 43
View Answer
Answer: d
Explanation: First you have to empty all the elements of the current stack into the temporary stack, push
the required element and empty the elements of the temporary stack into the original stack. Therfore
taking 10+10+1+11+11= 43 seconds.
8. You have two jars, one jar which has 10 rings and the other has none. They are placed one above the
other. You want to remove the last ring in the jar. And the second jar is weak and cannot be used to store
rings for a long time.
a) Empty the first jar by removing it one by one from the first jar and placing it into the second jar
b) Empty the first jar by removing it one by one from the first jar and placing it into the second jar and
empty the second jar by placing all the rings into the first jar one by one
c) There exists no possible way to do this
d) Break the jar and remove the last one
View Answer
Answer: b
Explanation: This is similar to performing dequeue operation using push and pop only. Elements in the
first jar are taken out and placed in the second jar. After removing the last element from the first jar,
remove all the elements in the second jar and place them in the first jar.
9. Given only a single array of size 10 and no other memory is available. Which of the following operation
is not feasible to implement (Given only push and pop operation)?
a) Push
b) Pop
c) Enqueue
d) Returntop
View Answer
Answer: c
Explanation: To perform Enqueue using just push and pop operations, there is a need of another array of
same size. But as there is no extra available memeory, the given operation is not feasible.
10. Given an array of size n, let’s assume an element is ‘touched’ if and only if some operation is
performed on it(for example, for performing a pop operation the top element is ‘touched’). Now you need
to perform Dequeue operation. Each element in the array is touched atleast?
a) Once
b) Twice
c) Thrice
d) Four times
View Answer
Answer: d
Explanation: First each element from the first stack is popped, then pushed into the second stack,
dequeue operation is done on the top of the stack and later the each element of second stack is popped
then pushed into the first stack. Therfore each element is touched four times.
1. To implement a stack using queue(with only enqueue and dequeue operations), how many queues will
you need?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: Either the push or the pop has to be a costly operation, and the costlier operation requires
two queues.
2. Making the push operation costly, select the code snippet which implements the same.(let q1 and q2
be two queues)
a)
b)
c)
advertisement
public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else
{
if(q1.size()>0)
{
q2.offer(x);
int size = q1.size();
while(size>0)
{
q1.offer(q2.poll());
size--;
}
}
else if(q2.size()>0)
{
q1.offer(x);
int size = q2.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
}
}
d)
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else
{
if(q1.size()>0)
{
q2.offer(x);
int size = q1.size();
while(size>0)
{
q2.offer(q2.poll());
size--;
}
}
else if(q2.size()>0)
{
q1.offer(x);
int size = q2.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
}
}
View Answer
Answer: a
Explanation: Stack follows LIFO principle, hence a new item added must be the first one to exit, but queue
follows FIFO principle, so when a new item is entered into the queue, it will be at the rear end of the
queue. If the queue is initially empty, then just add the new element, otherwise add the new element to
the second queue and dequeue all the elements from the second queue and enqueue it to the first one, in
this way, the new element added will be always in front of the queue. Since two queues are needed to
realize this push operation, it is considered to be costlier.
3. Making the push operation costly, select the code snippet which implements the pop operation.
a)
public void pop()
{
if(q1.size()>0)
{
q2.poll();
}
else if(q2.size()>0)
{
q1.poll();
}
}
b)
c)
d)
View Answer
Answer: b
Explanation: As the push operation is costly, it is evident that the required item is in the front of the queue,
so just dequeue the element from the queue.
4. Select the code snippet which returns the top of the stack.
a)
public int top()
{
if(q1.size()>0)
{
return q1.poll();
}
else if(q2.size()>0)
{
return q2.poll();
}
return 0;
}
b)
c)
d)
View Answer
Answer: c
Explanation: Assuming its a push costly implementation, the top of the stack will be in the front end of the
queue, note that peek() just returns the front element, while poll() removes the front element from the
queue.
5. Select the code snippet which return true if the stack is empty, false otherwise.
a)
public boolean empty()
{
return q2.isEmpty();
}
b)
c)
d)
View Answer
Answer: b
Explanation: If both the queues are empty, then the stack also is empty.
6. Making the pop operation costly, select the code snippet which implements the same.
a)
public int pop()
{
int res=-999,count=0;
if(q1.size()>0)
{
count = q1.size();
while(count>0)
q2.offer(q1.poll());
res = q1.poll();
}
if(q2.size()>0)
{
count = q2.size();
while(count>0)
q1.offer(q2.poll());
res = q2.poll();
}
return res;
}
b)
c)
d)
View Answer
Answer: c
Explanation: Here the pop operation is costly, hence we need two queues, other than the first element, all
the the elements from one queue are dequeued and enqueued to the second queue, hence only one
element remains in the first queue which is the item we want, so dequeue it and return the result.
1. What data structure is used when converting an infix notation to prefix notation?
a) Stack
b) Queue
c) B-Trees
d) Linked-list
View Answer
Answer: a
Explanation: First you reverse the given equation and carry out the algorithm of infix to postfix expression.
Here, the data structure used is stacks.
2. What would be the Prefix notation for the given equation?
A+(B*C)
a) +A*CB
b) *B+AC
c) +A*BC
d) *A+CB
View Answer
Answer: c
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-postfix algorithm.
The equation inside the bracket evaluates to CB* and outside the bracket evaluates to A+ therefore
getting CB*A+. Reversing this and we get +A*BC.
advertisement
3. What would be the Prefix notation for the given equation?
a) +*AB*CD
b) *+AB*CD
c) **AB+CD
d) +*BA*CD
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-postfix algorithm.
The equation inside the brackets evaluate to DC* and BA* respectively giving us DC*BA*+ in the end.
Reversing this we get the +*AB*CD.
4. What would be the Prefix notation for the given equation?
A+B*C^D
a) +A*B^CD
b) +A^B*CD
c) *A+B^CD
d) ^A*B+CD
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-prefix algorithm.
The preference order in ascending order are as follows +*^. Operators are pushed into the stack and
popped if its preference is greater than the one which is getting pushed. In the end all operators are
popped. The equation evaluates to DC^B*A+. Reversing this we get our following answer.
5. Out of the following operators (^, *, +, &, $), the one having highest priority is _________
a) +
b) $
c) ^
d) &
View Answer
Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the exponentiation will have the
highest priority.
6. Out of the following operators (|, *, +, &, $), the one having lowest priority is ________
a) +
b) $
c) |
d) &
View Answer
Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the logical OR will have the lowest
priority.
7. What would be the Prefix notation for the given equation?
A^B^C^D
a) ^^^ABCD
b) ^A^B^CD
c) ABCD^^^
d) AB^C^D
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-prefix algorithm.
Here we have to remember that the exponentiation has order of associativity from right to left. Therefore
the stack goes on pushing ^. Therefore resulting in ^^^ABCD.
8. What would be the Prefix notation for the given equation?
a+b-c/d&e|f
a) |&-+ab/cdef
b) &|-+ab/cdef
c) |&-ab+/cdef
d) |&-+/abcdef
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-prefix algorithm.
The preference order in ascending order are as follows |&+*/.
9. What would be the Prefix notation for the given equation?
(a+(b/c)*(d^e)-f)
a) -+a*/^bcdef
b) -+a*/bc^def
c) -+a*b/c^def
d) -a+*/bc^def
View Answer
Answer: b
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-prefix algorithm.
The preference order in ascending order are as follows +*/^. Brackets have the highest priority. The
equations inside the brackets are solved first.
10. What would be the Prefix notation and Postfix notation for the given equation?
A+B+C
a|b&c
a) a|&bc
b) &|abc
c) |a&bc
d) ab&|c
View Answer
Answer: c
Explanation: The order of preference of operators is as follows (descending): & |.
The equation a|b&c will be parenthesized as (a|(b&c)) for evaluation.
Therefore the equation for prefix notation evaluates to |a&bc.
a) abc*+de*+
b) abc+*de*+
c) a+bc*de+*
d) abc*+(de)*+
View Answer
Answer: a
Explanation: Using the infix to postfix expression conversion algorithm, the corresponding postfix
expression is found to be abc*+de*+.
6. Parentheses are simply ignored in the conversion of infix to postfix expression.
a) True
b) False
View Answer
Answer: b
Explanation: When a parenthesis is encountered, it is placed on the operator stack. When the
corresponding parenthesis is encountered, the stack is popped until the other parenthesis is reached and
they are discarded.
7. It is easier for a computer to process a postfix expression than an infix expression.
a) True
b) False
View Answer
Answer: a
Explanation: Computers can easily process a postfix expression because a postfix expression keeps
track of precedence of operators.
8. What is the postfix expression for the infix expression?
a-b-c
a) -ab-c
b) ab – c –
c) – -abc
d) -ab-c
View Answer
Answer: b
Explanation: The corresponding postfix expression for the given infix expression is found to be ab-c- and
not abc- -.
9. What is the postfix expression for the following infix expression?
a/b^c-d
a) abc^/d-
b) ab/cd^-
c) ab/^cd-
d) abcd^/-
View Answer
Answer: a
Explanation: Using the infix to postfix conversion algorithm, the corresponding postfix expression for the
infix expression is found to be abc^/d-.
10. Which of the following statement is incorrect with respect to infix to postfix conversion algorithm?
a) operand is always placed in the output
b) operator is placed in the stack when the stack operator has lower precedence
c) parenthesis are included in the output
d) higher and equal priority operators follow the same condition
View Answer
Answer: c
Explanation: Parentheses are not included in the output. They are placed in the operator stack and then
discarded.
11. In infix to postfix conversion algorithm, the operators are associated from?
a) right to left
b) left to right
c) centre to left
d) centre to right
View Answer
Answer: b
Explanation: In infix, prefix and postfix expressions, the operators are associated from left to right and not
right to left.
12. What is the corresponding postfix expression for the given infix expression?
a*(b+c)/d
a) ab*+cd/
b) ab+*cd/
c) abc*+/d
d) abc+*d/
View Answer
Answer: d
Explanation: Using the infix to postfix conversion algorithm, the corresponding postfix expression is
obtained as abc+*d/.
13. What is the corresponding postfix expression for the given infix expression?
a+(b*c(d/e^f)*g)*h)
a) ab*cdef/^*g-h+
b) abcdef^/*g*h*+
c) abcd*^ed/g*-h*+
d) abc*de^fg/*-*h+
View Answer
Answer: b
Explanation: Using the infix to postfix expression conversion algorithm using stack, the corresponding
postfix expression is found to be abcdef^/*g*h*+.
14. What is the correct postfix expression for the following expression?
a+b*(c^d-e)^(f+g*h)-i
a) abc^de-fg+*^*+i-
b) abcde^-fg*+*^h*+i-
c) abcd^e-fgh*+^*+i-
d) ab^-dc*+ef^gh*+i-
View Answer
Answer: c
Explanation: The postfix expression for the given infix expression is found to be abcd^e-fgh*+^*+i- when
we use infix to postfix conversion algorithm.
15. From the given Expression tree, identify the correct postfix expression from the list of options.
a) ab*cd*+
b) ab*cd-+
c) abcd-*+
d) ab*+cd-
View Answer
Answer: b
Explanation: From the given expression tree, the infix expression is found to be (a*b)+(c-d). Converting it
to postfix, we get, ab*cd-+.
- + 5 / 10 5 5
a) 2
b) 5
c) 10
d) 7
View Answer
Answer: a
Explanation: The infix notation of the given prefix notation is 5+10/5-5 which gives us 2 as our answer.
advertisement
2. What would be the solution to the given prefix notation?
/ / / 16 4 2 1
a) 1
b) 4
c) 2
d) 8
View Answer
Answer: c
Explanation: The infix notation to the given prefix notation is 16/4/2/1 which gives us 2 as our answer. The
infix notation is got from the prefix notation by traversing the equation from the right.
= 16/4/2/1
= 4/2/1 (16/4=4)
= 2/1 (4/2=2)
= 2 (2/1=2).
Subscribe Now: Data Structure Newsletter | Important Subjects Newsletters
3. What would be the solution to the given prefix notation?
+ 9 * 3 / 8 4
a) 14
b) 15
c) 18
d) 12
View Answer
Answer: b
Explanation: The infix notation for the given prefix notation is (9+(3*(8/4))) which solves to 15. So 15 is
correct answer.
4. What would be the solution to the given prefix notation?
- + 1 2 * 3 / 6 2
a) 6
b) -6
c) 3
d) -3
View Answer
Answer: b
Explanation: The infix notation for the given prefix notation is (1+2)-3*(6/2). The result of the given
equation is -6.
5. What would be the solution to the given prefix notation?
- * 1 5 / * / 6 3 6 2
a) 1
b) 0
c) -1
d) -2
View Answer
Answer: c
Explanation: The infix notation for the given prefix notation is (1*5)-(6/3)*6/2. The result of the equation is -
1.
6. What would be the solution to the given prefix notation?
* / + 1 2 / 4 2 + 3 5
a) 12
b) 7.5
c) 9
d) 13.5
View Answer
Answer: a
Explanation: The infix notation of the given prefix notation is ((1+2)/(4/2))*(3+5) which solves to (3/2)*8
which by solving gives us 12.
7. Given a prefix and a postfix notation what are the difference between them?
a) The postfix equation is solved starting from the left whereas the prefix notation is solved from the right
b) The postfix equation is solved starting from the right whereas the prefix notation is solved from the left
c) Both equations are solved starting from the same side(right)
d) Both equations are solved starting from the same side(left)
View Answer
Answer: a
Explanation: The postfix notation is solved starting from left but whereas the prefix notation is reversed
after creating them, therefore it’s solved starting from right.
8. When converting the prefix notation into an infix notation, the first step to be followed is ________
a) Reverse the equation
b) Push the equation to the stack
c) Push the equation onto the queue
d) Push the equation to the stack or queue
View Answer
Answer: a
Explanation: The steps that are followed are: the equation is reversed, pushed onto a stack, popped one
by one and solved. Therefore the first step is reversing the equation.
9. The time complexity of converting a prefix notation to infix notation is _________
a) O(n) where n is the length of the equation
b) O(n) where n is number of operands
c) O(1)
d) O(logn) where n is length of the equation
View Answer
Answer: a
Explanation: The processes that are involved are reversing the equation (O(n)), pushing them all onto the
stack(O(n)), and popping them one by one and solving them (O(n)). Hence the answer is O(n) where n is
the length of the equation.
10. Given two processes (conversion of postfix equation to infix notation and conversion of prefix notation
to infix notation), which of the following is easier to implement?
a) Both are easy to implement
b) Conversion of postfix equation to infix equation is harder than converting a prefix notation to infix
notation
c) Conversion of postfix equation to infix equation is easier than converting a prefix notation to infix
notation
d) Insufficient data
View Answer
Answer: c
Explanation: As the conversion of prefix notation to infix notation involves reversing the equation, the
latter is harder to implement than postfix to infix process.
1. Which of the following data structure is used to convert postfix expression to infix expression?
a) Stack
b) Queue
c) Linked List
d) Heap
View Answer
Answer: a
Explanation: To convert the postfix expression into infix expression we need stack. We need stack to
maintain the intermediate infix expressions. We use stack to hold operands.
2. The postfix expression abc+de/*- is equivalent to which of the following infix expression?
a) abc+-de*/
b) (a+b)-d/e*c
c) a-(b+c)*(d/e)
d) abc+*-(d/e)
View Answer
Answer: c
infix ⇒ a(b+c)(d/e)*-
Explanation: Given postfix expression : abc+de/*-
⇒ a(b+c)*(d/e)-
⇒ a-(b+c)*(d/e)
Hence, correct choice is a-(b+c)*(d/e).
3. The equivalent infix expression and value for the postfix form 1 2 + 3 * 4 5 * – will be ___________
a) 1 + 2 * 3 – 4 * 5 and -13
b) (2 + 1) * (3 – 4) * 5 and 13
c) 1 + 2 * (3 – 4) * 5 and -11
d) (1 + 2) * 3 – (4 * 5) and -11
View Answer
Answer: d
⇒ (1 + 2) 3 * 4 5 * –
Explanation: Given postfix expression : 1 2 + 3 * 4 5 * –
⇒ ((1 + 2) * 3) 4 5 * –
⇒ ((1 + 2) * 3) (4 * 5) –
⇒ ((1 + 2) * 3) – (4 * 5)
So, the equivalent infix expression is (1 + 2) * 3 – (4 * 5) and it’s value is -11.
advertisement
4. What is the value of the postfix expression 2 3 + 4 5 6 – – *
a) 19
b) 21
c) -4
d) 25
View Answer
Answer: d
infix ⇒ (2 + 3)4 (5 – 6) – *
Explanation: Given postfix expression : 2 3 + 4 5 6 – – *
⇒ (2 + 3)*4 – (5 – 6)
Hence, value = (2 + 3) * (4 – (5 – 6)) = 5 *(4 – (-1)) = 5*5 = 25.
5. The prefix expression of the postfix expression AB+CD-* is __________
a) (A+B)*(C-D)
b) +AB*-CD
c) A+*BCD-
d) *+AB-CD
View Answer
Answer: d
Explanation: To convert from postfix to prefix, we first convert it to infix and then to prefix.
infix ⇒ 4 (5 a 6) b (7 a 8) c
Explanation: Given postfix expression: 4 5 6 a b 7 8 a c
⇒ (4 b (5 a 6)) (7 a 8) c
⇒ (4 b (5 a 6)) c (7 a 8)
So, the required infix expression is 4 b 5 a 6 c 7 a 8.
7. To convert the postfix expression into the infix expression we use stack and scan the postfix expression
from left to right.
a) True
b) False
View Answer
Answer: a
Explanation: Stack is used to postfix expression to infix expression. And to convert we follow the following
steps: (i) Scan the expression from left to right. (ii) If operand is found, push it on stack.(iii) If operator is
found, the two operands are popped and the combined infix expression is formed and pushed onto the
stack.
8. Which of the following is valid reverse polish expression?
a) a op b
b) op a b
c) a b op
d) both op a b and a b op
View Answer
Answer: c
Explanation: The postfix expression is also known as the reverse polish expression. In postfix
expressions, the operators come after the operands. So, the correct expression is a b op and hence a b
op is correct.
9. The result of the postfix expression 5 3 * 9 + 6 / 8 4 / + is _____________
a) 8
b) 6
c) 10
d) 9
View Answer
Answer: b
Explanation: Given postfix expression: 5 3 * 9 + 6 / 8 4 / +
Result = 5 3 * 9 + 6 / 8 4 / +
= (5 * 3) 9 + 6 / (8 / 4) +
= ((5 * 3) + 9) / 6 + ( 8 / 4) = ( 24 / 6) + 2 = 4 + 2 = 6.