02 DS Quiz Set
02 DS Quiz Set
A) union
B) intersection
C) membership
D) cardinality
Answer A)
Finding membership and cardinality will
require a linear scan. Between union and
intersection, union will require more
operations than intersection as union will have
to scan both Lists and include only the unique
elements in the union.
4.Interpret what the following function computes given head nodes of two list.
<pre>
struct LinkedList{
int value;
LinkedList* next;
}
LinkedList* DoSomething(LinkedList* first, LinkedList* second){
int len1 = length(first);
int len2 = length(second);
int diff; LinkedList* tmp1,*tmp2;
if (len1 > len2){
diff = len1len2;
tmp1 = first; tmp2 = second;
}else{
diff = len2len1;
tmp1 = second; tmp2 = first;
}
while(diff>0){
tmp1=tmp1>
next;
diff;
}
while(tmp1!=NULL &&
tmp2!=NULL &&
tmp1!=tmp2){
tmp1=tmp1>
next;
tmp2=tmp2>
next;
}
if (tmp1==NULL)
return NULL
else
return tmp1;
}
</pre>
length() computes the length of linkedlist.
A) Find the node where two list have same value and
return NULL if there is no such element.
B) Find the node where two list intersects and return
NULL if there is no intersection.
C) Find the node in the longest list, at length of smallest
list and return NULL if length of two list are same.
D) None of the above
Answer B)
Explanation:
diff computes the difference between length of
two list. Then move the head of longer by diff.
Follow it by moving the pointers of both list till
they match
Which of the following is true about stack ADT implemented using
linked list ?
A. For push( ) operation, new nodes are inserted at the head of linked list. For
pop( ) operation,
nodes are removed from head.
B. push( ) adds new node at head of list and pop( ) removes nodes from rear
end of list
C. Push() adds new node at rear end of list and Pop() removes nodes from
head of list.
D. None of above
Answer: A
Convert the following infix expression to
postfix expression. ( ^ is the exponentiation
operator ) (a/b^c)*b*cd*e
A.ab/c^b*c*de*
B.abc^/bc**de*
C.abc/^b*c*de
D.abc^/bc*de**
Answer B)
Explanation
(a/b^c)*b*cd*e = (a/(b^c))*(b*c)(d*e)
Suppose a circular queue is implemented with
an array of n elements. What is the condition
for a queue being full?
A. REAR == FRONT
B. (REAR+1) mod n == FRONT
C. (FRONT+1) mod n == REAR
D. None of Above
Answer B)
Given two structures tmp1, tmp2 as defined
below, how will you compare the two
structures?
struct A { int a; };
struct A tmp1,tmp2
A. tmp1==tmp2
B. tmp1.a==tmp2.a
C. Both A & B
D. tmp1 and tmp2 cannot be compared.
Answer B)
9. A queue is a __________
A. FILO
B. LIFO
C. FIFO
D. None of the above.
Answer C)
10. In general, List ADT allows:
A. Insertions and deletions anywhere.
B. Insertions and deletions only at one end.
C. Insertions at back and deletions at the front.
D. Insertions at the front and deletions at the back
Answer A)
11. A dynamic array is to be created in C++.
Which of the statements will achieve this?
A. int *A = new int [5];
B. int *A= new int*[5];
C. int *A = new int(5);
D. int A = new int[5];
Answer A)
12. What does the following function DoSomething( ) do on the linked list head (passed as
argument to function? Assume that Node is a structure that has an integer member called
value and a pointer to another.
struct Node {
int value;
Struct Node * next;
};
void DoSomething (struct Node *head)
{
if (head == NULL) return;
else {
DoSomething (head>
A. print values in linked list in order
next);
cout << head> B. print alternate values in linked list
value; C. print values in linked list in reverse
}
order
}
D. Error.
Answer C)
13.Suppose C supported a hypothetical datatype called digit which could only hold a
single digit (0 9).
You are needed to write a program that adds two digits. You soon
realize that sum of 2 digits can exceed 9 and hence one single digit
variable cannot hold the number. To rectify this problem you define a
structure digit2 (2 digits) as follows.
struct digit2 {
digit unit; /* digit variable that holds the units place digit */
digit tens; /* digit variable that holds the tens place digit */
};