wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

DSA quiz 3 set 1

Total questions: 10

Worksheet time: 30mins

Name
Class
Date
1.

Consider a directed, weighted graph G = (V, E) with weight function w: E→R. For some function f: V→R, for each edge (u, v) ∈ E, define w'(u, v) as w(u, v) + af(u) - bf(v), where a and b are two constants.

Which one of the options completes the following sentence so that it is TRUE?

“The shortest paths in G under w are the shortest paths under w’ too, ______”.

a)

If a = b

b)

If a<b

c)

If a>b

d)

Any value of a and b

2.

Let G be a graph with 100! vertices, with each vertex labeled by a distinct permutation of the numbers 1, 2, …, 100. There is an edge between vertices u and v if and only if the label of u can be obtained by swapping two adjacent numbers in the label of v. Let y denote the degree of a vertex in G, and z denote the number of connected components in G. Then y + z = _______ .

a)

100

b)

110

c)

109

d)

None of these

3.

Consider BFS on a binary tree starting from the root node. There is a vertex t at a distance 6 from the root. If t is the nth vertex in this BFS traversal, then the minimum and maximum possible values of n are ________

a)

32,64

b)

31,63

c)

64,127

d)

127,128

4.

Consider following statements written in C

i) sizeof(int)

ii) sizeof(int*)

iii) sizeof(int**)

Consider the size of int as 4 bytes and size of pointer as 4 bytes. Pick the correct option -

a)

Only i) would compile successfully, and it would return size as 4.

b)

i), ii) and iii) would compile successfully and the size of each would be same i.e. 4

c)

i), ii) and iii) would compile successfully, but the size of each would be different and would be decided at run time

d)

ii) and iii) would result in compile error but i) would compile and result in size as 4.

5.

Consider the following code snippet.

#include <stdlib.h>

int main()

{

 int *ptr;

 int **ptr1;

 int **ptr2;

 ptr = (int*)malloc(sizeof(int));

 ptr1 = (int**)malloc(10*sizeof(int*));

 ptr2 = (int**)malloc(10*sizeof(int*));

 free(ptr);

 free(ptr1);

 free(*ptr2);

 return 0;

}

Pick the Correct option -

a)

malloc() for ptr1 and ptr2 isn’t correct. It’ll give a compile time error.

b)

free(*ptr2) is not correct. It’ll give a run time error.

c)

free(*ptr2) is not correct. It’ll give a compile time error.

d)

No issue with any of the malloc() and free() i.e. no compile/run time error.

6.

Which data structure is best suited to print the documents in the printer?

a)

Queue

b)

Stack

c)

Linked List

d)

Array

7.

Consider the following code snippet.

#include <stdio.h>

int func(int *a, int data)

{

    *a = data;

    return a;

}

int main()

{

    int *x = malloc(sizeof(int));

    if (NULL == x) return;

    x = func(x, 0);

    if(x)

    {

        x = (int*) malloc(sizeof (int));

        if (NULL == x) return;

        x = func(x, 10);

    }

    printf("%d\n", *x);

    free(x);

}

a)

compiler error because the comparison should be made as x==NULL and not as shown.

b)

compiler error as the return of malloc is not typecast appropriately.

c)

compiles successfully but execution may result in memory leak.

d)

compiles successfully but execution may result in dangling pointer.

8.

Consider C code.

int abc(int val)

{

    int x = 0;

    while (val > 0)

    {

        x = x + abc(val--);

    }

    return val;

}

Calling of abc(3) will result in -

a)

Return 6

b)

Infinite loop

c)

Abnormal termination

d)

Both B & C

9.

 Let A be an array of 16 numbers consisting of a sequence of 0’s followed by a sequence of 1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst case number of probes performed by an optimal algorithm is________.

a)

2

b)

3

c)

4

d)

5

10.

Let’s say you have been given 2 GB of data with only 200 MB of available Main Memory. You need to sort this data. Which sorting technique will be most appropriate?

a)

Heap sort

b)

Merge sort

c)

Quick sort

d)

Insertion sort