MCQ Questions

Question Bank › Technical › Data Structures › MCQ

1234

Other questions

Q301.Medium
Which method correctly stores an element in a sparse matrix represented as an array of sparse arrays of size N (valid indices 0 to N-1)?
a)
public void store(int row_index, int col_index, Object val)
{
if (row_index < 0 || row_index >= N)
{
System.out.println("row index out of bounds");
return;
}
if (col_index < 0 || col_index >= N)
{
System.out.println("column index out of bounds");
return;
}
sparse_array[row_index].store(col_index, val);
}
b)
public void store(int row_index, int col_index, Object val)
{
if (row_index < 0 || row_index >= N)
{
System.out.println("column index out of bounds");
return;
}
if (col_index < 0 || col_index >= N)
{
System.out.println("row index out of bounds");
return;
}
sparse_array[row_index].store(col_index, val);
}
c)
public void store(int row_index, int col_index, Object val)
{
if (row_index < 0 && row_index >= N)
{
System.out.println("row index out of bounds");
return;
}
if (col_index < 0 && col_index >= N)
{
System.out.println("column index out of bounds");
return;
}
sparse_array[row_index].store(col_index, val);
}
d)
public void store(int row_index, int col_index, Object val)
{
if (row_index < 0 && row_index >= N)
{
System.out.println("column index out of bounds");
return;
}
if (col_index < 0 && col_index >= N)
{
System.out.println("row index out of bounds");
return;
}
sparse_array[row_index].store(col_index, val);
}
  1. Option a
  2. Option b
  3. Option c
  4. Option d
Show answer & explanation
Answer: (A) Option a
Explanation: 1. Bounds check must use OR (||) so that either a negative or an over-large index is rejected. 2. The valid range is 0 to N-1, so the upper check must be >= N, not > N. 3. The row check should print 'row index out of bounds' and the column check should print 'column index out of bounds'. 4. Option a uses || with correct >= N checks and correct messages, then stores at sparse_array[row_index].store(col_index, val). 5. Options b, c, d use wrong operators or swapped messages.
Q302.Medium
A suffix array can be constructed in O(n log n) time.
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: 1. A naive approach sorts all suffixes in O(n^2 log n) time. 2. Using prefix doubling (or similar techniques), the suffix array can be built in O(n log n) time. 3. Therefore the statement is True.
Q303.HardAnswer under review
A doubly linked list class uses a Node with getNext(), setNext(), getPrev(), setPrev(), and fields head, tail and size. Which method correctly removes the node at a given position pos (0-based) from the list?

a)
public void remove(int pos)
{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<pos; i++)
temp = temp.getNext();
}
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
size--;
}

b)
public void remove(int pos)
{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<pos; i++)
temp = temp.getNext();
}
temp.getNext().setPrev(temp.getNext());
temp.getPrev().setNext(temp.getPrev());
size--;
}

c)
public void remove(int pos)
{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<pos; i++)
temp = temp.getNext().getNext();
}
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
size--;
}

d)
public void remove(int pos)
{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<pos; i++)
temp = temp.getNext().getNext();
}
temp.getNext().setPrev(temp.getNext());
temp.getPrev().setNext(temp.getPrev());
size--;
}
  1. Option a
  2. Option b
  3. Option c
  4. Option d
Show answer & explanation
Answer: (A) Option a
Explanation: 1. First the position is validated (pos<0 or pos>=size) and an empty list is handled. 2. If pos==0, head is moved to the next node and tail is reset if the list becomes empty. 3. Otherwise temp walks forward so that temp ends up as the node just before the target (loop runs i=1 to pos-1). 4. To unlink the target: temp.getNext().setPrev(temp.getPrev()) and temp.getPrev().setNext(temp.getNext()) — the target's neighbours are linked to each other. 5. Option b sets the neighbours' pointers to themselves (wrong). Options c and d skip two nodes at a time and corrupt the links. Only option a is correct.
Q304.Hard
A doubly-ended queue is implemented with a dummy head node and a dummy trail node, where head.getNext() gives the first element and trail is the sentinel after the last element. Which method correctly deletes an element from the rear end?
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)
public Object deleteRear() throws emptyDEQException {
  if(isEmpty()) throw new emptyDEQException("Empty");
  else {
    Node temp = head.getNext();
    Node cur = head;
    while(temp != trail) { temp = temp.getNext(); cur = cur.getNext(); }
    Object e = temp.getEle();
    cur.setNext(trail);
    size--;
    return e;
  }
}
c)
public Object deleteRear() throws emptyDEQException {
  if(isEmpty()) throw new emptyDEQException("Empty");
  else {
    Node temp = head.getNext();
    Node cur = head;
    while(temp.getNext() != trail) { temp = temp.getNext(); cur = cur.getNext(); }
    Object e = temp.getEle();
    cur.setNext(trail);
    size--;
    return e;
  }
}
d)
public Object deleteRear() throws emptyDEQException {
  if(isEmpty()) throw new emptyDEQException("Empty");
  else {
    Node temp = head.getNext();
    Node cur = head;
    while(temp.getNext() != trail) { temp = temp.getNext(); cur = cur.getNext(); }
    Object e = temp.getEle();
    temp.setNext(trail);
    size--;
    return e;
  }
}
  1. Option a
  2. Option b
  3. Option c
  4. Option d
Show answer & explanation
Answer: (C) Option c
Explanation: 1. To delete the rear element, we must reach the last real node (the one whose next is trail) while keeping a pointer cur to the node just before it. 2. Start temp = head.getNext() and cur = head, then loop while temp.getNext() != trail, advancing both. 3. When the loop ends, temp is the last element and cur is its predecessor. 4. Save temp's element, then set cur.setNext(trail) to unlink temp, decrement size and return the element. 5. Option a starts cur at temp (wrong predecessor); option b stops when temp == trail (goes one too far); option d sets temp.setNext(trail) instead of cur's. Only option c is correct.
Q305.Hard
A singly linked list contains the nodes 7 -> 8 -> 9 -> 10 -> 11 -> 12. What is the output of the following function when called with start pointing to the first node (7)?

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);
}
  1. 7 10 12 12 10 7
  2. 7 9 11 7 9 11
  3. 7 8 9 11
  4. 7 9 11 11 9 7
Show answer & explanation
Answer: (D) 7 9 11 11 9 7
Explanation: 1) fun(7): prints 7, recurses on node after next (9), then prints 7 at the end. 2) fun(9): prints 9, recurses on 11, then prints 9. 3) fun(11): prints 11, recurses on 13 which is NULL so returns, then prints 11. 4) Unwinding: 11, then 9, then 7. 5) Full output: 7 9 11 11 9 7 (alternate nodes forward, then back).
Q306.Hard
Assume that the operators +, –, * are left associative and ^ is right associative. The order of precedence (from highest to lowest) is ^, *, +, –. The postfix expression for the infix expression x + y * z – w ^ u ^ v is?
  1. xyz *+ wuv ^^ –
  2. xyz *+ wu^v^ –
  3. xy+z *w – u ^v^
  4. –+x*yz^ ^wuv
Show answer & explanation
Answer: (A) xyz *+ wuv ^^ –
Explanation: First handle y*z → yz*. Then x + (yz*) → xyz*+. For w ^ u ^ v, ^ is right associative, so it means w ^ (u ^ v) → wuv^^. Now combine: (xyz*+) – (wuv^^) → xyz*+ wuv^^ –. So the postfix expression is xyz*+ wuv^^ –.
1234

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank