0% found this document useful (0 votes)
49 views14 pages

Question

The document contains 15 multiple choice questions related to algorithms and data structures. It covers topics like sorting algorithms, binary search trees, graphs, heaps, stacks, queues, linked lists, and more. The questions test understanding of time complexity, implementation of various algorithms, minimum paths in graphs, and properties of different data structures.

Uploaded by

NIKITH ACHANTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views14 pages

Question

The document contains 15 multiple choice questions related to algorithms and data structures. It covers topics like sorting algorithms, binary search trees, graphs, heaps, stacks, queues, linked lists, and more. The questions test understanding of time complexity, implementation of various algorithms, minimum paths in graphs, and properties of different data structures.

Uploaded by

NIKITH ACHANTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Consider the following statements:


int x = 10, y = 15;
x = ((x < y) ? (y + x) : (y - x);

What will be the value of x after executing these statements?


a) 25
b) 15
c) 5
d) Error

Correct Answer : A

2. Which of the following stable sorting algorithm takes the least time when applied to an
almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort

Answer: d

3. Choose the correct code for merge sort.


a)

void merge_sort(int arr[], int left, int right)


{
if (left > right)
{

int mid = (right-left)/2;


merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

merge(arr, left, mid, right); //function to merge sorted


arrays
}
}

b)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = left+(right-left)/2;


merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

merge(arr, left, mid, right); //function to merge sorted


arrays
}
}

c)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = left+(right-left)/2;


merge(arr, left, mid, right); //function to merge sorted arrays
merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

}
}

d)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = (right-left)/2;


merge(arr, left, mid, right); //function to merge sorted arrays
merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);
}
}

Answer: b

4. Consider the original array 17 8 12 4 26. How many comparisons are needed to
construct the BST on the original array?
a) 5
b) 4
c) 7
d) 10

Answer: d

5. Consider the following statements related to the binary tree sort.


I. Element can be added gradually as they become available
II. It needs extra memory space
a) Statement I is true but Statement II is false
b) Both Statement I and Statement II are false
c) Both Statement I and Statement II are true
d) Statement II is true but Statement I is false

Answer: c
Explanation: Binary tree sort is dynamic sorting, that is it gets more efficient as more
the elements are added. So, we can add elements gradually as they become
available. Binary tree sort requires extra memory space, its worst case space
complexity is Θ(n).

6. Fractional knapsack problem is solved most efficiently by which of the following


algorithm?
a) Divide and conquer
b) Dynamic programming
c) Greedy algorithm
d) Backtracking

Answer: c

Explanation: Greedy algorithm is used to solve this problem. We first sort items
according to their value/weight ratio and then add item with highest ratio until we
cannot add the next item as a whole. At the end, we add the next item as much as
we can.

7.Consider the following heap after buildheap phase. What will be its
corresponding array?

a) 26,53,41,97,58,59,31
b) 26,31,41,53,58,59,97
c) 26,41,53,97,31,58,59
d) 97,53,59,26,41,58,3

Answer: d

Explanation: Constructing a max heap using the elements 97,53,59,26,41,58,31 will


cause the heap to look like that.

8. What is the auxiliary space complexity of tree sort?

a) O(1)

b) O(n)
c) O(log n)

d) O(n log n)

Answer: b

Explanation: Tree sort requires auxiliary space for maintaining a binary search
tree. So the auxiliary space complexity of tree sort is O(n).

9. Dijkstra’s Algorithm run on a weighted, directed graph G={V,E} with non-negative


weight function w and source s, terminates with d[u]=delta(s,u) for all vertices u in V.

a) True

b) False

Answer: a

Explanation: The equality d[u]=delta(s,u) holds good when vertex u is added to set
S and this equality is maintained thereafter by the upper bound property.

10. Consider the following graph.


If b is the source vertex, what is the minimum cost to reach f vertex?
a) 8
b) 9
c) 4
d) 6

Answer: d

Explanation: The minimum cost to reach f vertex from b vertex is 6 by having


vertices g and e as intermediates.

b to g, cost is 1

g to e, cost is 4

e to f, cost is 1

hence total cost 1+4+1=6.

11.. In the given graph, identify the shortest path having minimum cost to reach
vertex E if A is the source vertex.
a) a-b-e
b) a-c-e
c) a-c-d-e
d) a-c-d-b-e

Answer: b

Explanation: The minimum cost required to travel from vertex A to E is via vertex C

A to C, cost= 3

C to E, cost= 2

Hence the total cost is 5.

12.. Minimum number of queues to implement stack is ___________

a) 3

b) 4

c) 1
d) 2

Answer: c

Explanation: Use one queue and one counter to count the number of elements in
the queue.

13.What is the functionality of the following piece of code?

public void display()

if(size == 0)

System.out.println("underflow");

else

Node current = first;

while(current != null)

System.out.println(current.getEle());

current = current.getNext();

}
}

a) reverse the list

b) display the list

c) display the list excluding top-of-the-stack-element

d) reverse the list excluding top-of-the-stack-element

Answer: b

Explanation: An alias of the node ‘first’ is created which traverses through the list
and displays the elements.

14, 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 push()
operation that can be included in the Stack class. Also ‘first’ is the top-of-the-stack.

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)

public void push(Object item)

{
Node temp = new Node(item,first);

first = temp;

size++;

b)

public void push(Object item)

Node temp = new Node(item,first);

first = temp.getNext();

size++;

c)

public void push(Object item)

Node temp = new Node();

first = temp.getNext();

first.setItem(item);

size++;
}

d)

public void push(Object item)

Node temp = new Node();

first = temp.getNext.getNext();

first.setItem(item);

size++;

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’.

15. 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


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).

You might also like