wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

DS QUIZ-1 (CSE-42)

Total questions: 16

Worksheet time: 19mins

Name
Class
Date
1.

What is the time complexity of the following code:

int a = 0, i = N;

while (i > 0) {

a += i;

i /= 2;

}

a)
O(N)
b)
O(log N)
c)
O(N^2)
d)

O(Nlog N)

2.

Consider the following C program :

#include < stdio.h >

int main () {

int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5};

int *p = arr+4;

printf ("%d\n", p[1]);

return 0;

}

The number that will be displayed on execution of the program is?

a)
5
b)

2

c)
7
d)
6
3.

What would be output ?

a)

55

b)
20
c)
15
d)
35
4.

Which of the following is/are true

a)

calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.

b)

calloc() takes two arguments, but malloc takes only 1 argument.

c)

Both malloc() and calloc() return \'void *\' pointer.

d)

All of the above

5.

A 2-D array A[4.....7, -1.....3] requires 2 bytes of storage space for each element. if the array is stored in row major form having base address 100, then address of A[6,2] will be...?

a)

126

b)

116

c)
120
d)
114
6.

What are the elements present in the array of the following C code?

int array[5] = {5};

a)
[5, 0, 0, 0, 0]
b)

5, (garbage), (garbage), (garbage), (garbage)

c)

(garbage), (garbage), (garbage), (garbage), 5

d)
[5, 5, 5, 5, 5]
7.

An array of similar data types which themselves are a collection of dissimilar data type are ___________

a)
array
b)

Linked list

c)
tuple
d)

Array of Structure

8.

Which of the following statements is false?

a)

In static allocation, memory cannot be resized at runtime

b)

In dynamic allocation, unused memory must be explicitly freed by programmer

c)

Static allocation uses stack memory

d)

Dynamic allocation uses stack memory

9.

If you allocate memory using malloc() but forget to call free(), it may cause:

a)
Automatic garbage collection of unused memory.
b)
Increased speed of program execution.
c)
Memory leaks, which can lead to increased memory usage and potential exhaustion of available memory.
d)
Improved performance due to better memory management.
10.

Consider a 1000 × 1000 matrix with only 1000 non-zero elements. Which storage is more efficient?

a)

Normal 2D array representation

b)

Sparse matrix representation (triplet)

c)

Both are equally efficient

d)

Depends on the compiler

11.

In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is

a)
n/2
b)
log(n)
c)
n
d)
1
12.

The following C function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank.


Node* move_to_front(Node head) {

if ((head == NULL) || (head->next == NULL)) {

return head;

}

Node* prev = NULL;

Node* temp = head;

while (temp->next != NULL) {

prev = temp;

temp = temp->next;

}

// Fill in the missing code here

return head;

}

Choose the correct alternative to replace the blank line.

a)
prev->next = head; head = temp; temp->next = NULL;
b)
temp->next = head; prev->next = NULL; head = temp;
c)
head->next = temp; temp->next = NULL;
d)
temp->next = NULL; head = prev;
13.

The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?

void rearrange(struct node *head) {

if (head == NULL || head->next == NULL)

return;

struct node* temp = head;

struct node* temp1 = head->next;

while (temp1 != NULL) {

int swap = temp->value;

temp->value = temp1->value;

temp1->value = swap;

temp = temp1->next;

if (temp != NULL){

temp1 = temp->next;

else

temp1 = NULL;

}

}

a)
2,1,4,3,6,5,7
b)
7,6,5,4,3,2,1
c)
1,2,3,4,5,6,7
d)
3,2,1,6,5,4,7
14.

To Insert a node after a given node ptr. Fill in the blanks.

struct node* newNode = (struct node*)malloc(sizeof(struct node));

newNode->data = x;

newNode->next = ptr->next;

newNode->prev = ______; // Blank 1

if (ptr->next != NULL)

ptr->next->prev = ______; // Blank 2

ptr->next = newNode;

a)

Blank 1: ptr,

Blank 2: newNode

b)

Blank 1: prev,

Blank 2: newNode

c)

Blank 1: NULL,

Blank 2: newNode

d)

Blank 1: newNode

Blank 2: prev

15.

Let SLLdel be a function that deletes a node in a singly-linked list given:

  • a pointer to the node to be deleted, and

  • a pointer to the head of the list.

Similarly, let DLLdel be another function that deletes a node in a doubly-linked list, given:

  • a pointer to the node to be deleted, and

  • a pointer to the head of the list.

Let n denote the number of nodes in each of the linked lists.
Which one of the following statements is TRUE about the worst-case time complexity of SLLdel and DLLdel?

a)
SLLdel has O(1) and DLLdel has O(1) time complexity.
b)
SLLdel has O(n) and DLLdel has O(1) time complexity.
c)
SLLdel has O(n) and DLLdel has O(n) time complexity.
d)
SLLdel has O(1) and DLLdel has O(n) time complexity.
16.

On a scale of 1 to 5, how would you rate your Data Structures professor?

a)
3
b)

1

c)
4
d)
2