Question Bank › Technical › Data Structures › MCQ
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);
}A suffix array can be constructed in O(n log n) time.
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--;
}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;
}
}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);
}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?
INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India
contact@intmatedtech.com