Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Tests  >  Test: Linked List - Computer Science Engineering (CSE) MCQ

Test: Linked List - Computer Science Engineering (CSE) MCQ


Test Description

15 Questions MCQ Test - Test: Linked List

Test: Linked List for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Test: Linked List questions and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus.The Test: Linked List MCQs are made for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Linked List below.
Solutions of Test: Linked List questions in English are available as part of our course for Computer Science Engineering (CSE) & Test: Linked List solutions in Hindi for Computer Science Engineering (CSE) course. Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free. Attempt Test: Linked List | 15 questions in 45 minutes | Mock test for Computer Science Engineering (CSE) preparation | Free important questions MCQ to study for Computer Science Engineering (CSE) Exam | Download free PDF with solutions
Test: Linked List - Question 1

What does the following function do for a given Linked List with first node as head?

void fun1(struct node* head)
{
  if(head == NULL)
    return;
  
  fun1(head->next);
  printf("%d  ", head->data);
}

Detailed Solution for Test: Linked List - Question 1

fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1. 

Test: Linked List - Question 2

Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity?

Detailed Solution for Test: Linked List - Question 2

Both Merge sort and Insertion sort can be used for linked lists.

The slow random-access performance of a linked list makes other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible.

Since worst case time complexity of Merge Sort is O(nLogn) and Insertion sort is O(n2), merge sort is preferred.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Linked List - Question 3

The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function.

/* Link list node */
struct node
{
    int data;
    struct node* next;
};
  
/* head_ref is a double pointer which points to head (or start) pointer 
  of linked list */
static void reverse(struct node** head_ref)
{
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    /*ADD A STATEMENT HERE*/
}
  
What should be added in place of “/*ADD A STATEMENT HERE*/”, so that the function correctly reverses a linked list.

Detailed Solution for Test: Linked List - Question 3

*head_ref = prev;

At the end of while loop, the prev pointer points to the last node of original linked list. We need to change *head_ref so that the head pointer now starts pointing to the last node.

See the following complete running program.

#include<stdio.h>
#include<stdlib.h>
   
/* Link list node */
struct node
{
    int data;
    struct node* next;
};
   
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    *head_ref = prev;
}
   
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
    /* allocate node */
    struct node* new_node =
            (struct node*) malloc(sizeof(struct node));
              
    /* put in the data  */
    new_node->data  = new_data;
                  
    /* link the old list off the new node */
    new_node->next = (*head_ref);    
          
    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}
   
/* Function to print linked list */
void printList(struct node *head)
{
    struct node *temp = head;
    while(temp != NULL)
    {
        printf("%d  ", temp->data);    
        temp = temp->next;  
    }
}    
   
/* Drier program to test above function*/
int main()
{
    /* Start with the empty list */
    struct node* head = NULL;
     
     push(&head, 20);
     push(&head, 4);
     push(&head, 15); 
     push(&head, 85);      
       
     printList(head);    
     reverse(&head);                      
     printf("\n Reversed Linked list \n");
     printList(head);    
     return 0;
}

Test: Linked List - Question 4

What is the output of following function for start pointing to first node of following linked list?

1->2->3->4->5->6

Detailed Solution for Test: Linked List - Question 4

fun() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then skips the last node.

Test: Linked List - Question 5

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?

struct node 
{
  int value;
  struct node *next;
};
void rearrange(struct node *list)
{
  struct node *p, * q;
  int temp;
  if ((!list) || !list->next) 
      return;
  p = list;
  q = list->next;
  while(q) 
  {
     temp = p->value;
     p->value = q->value;
     q->value = temp;
     p = q->next;
     q = p?p->next:0;
  }
}

Detailed Solution for Test: Linked List - Question 5

The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.

Test: Linked List - Question 6

Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among union, intersection, membership, cardinality will be the slowest?

Detailed Solution for Test: Linked List - Question 6

1. Union:

To compute the union of two sets represented as linked lists:

  • Traverse each linked list and insert elements into a new list, ensuring no duplicates.
  • This requires O(n2)O(n^2) in the worst case if we need to check for duplicates by comparing each new element to all existing elements in the result list.

2. Intersection:

To compute the intersection of two sets:

  • For each element in the first linked list, check if it exists in the second linked list.
  • Since checking for membership in a linked list takes O(n)O(n), the total time complexity is O(nm)O(n \cdot m), where nn and mm are the sizes of the two lists.
  • This is slower than union because it involves repeated membership checks.

3. Membership:

To check if an element exists in the set:

  • Traverse the linked list to search for the element.
  • This is a linear operation, O(n)O(n), where nn is the size of the list.

4. Cardinality:

To determine the number of elements in the set:

  • Traverse the linked list and count the elements.
  • This is a single traversal operation, O(n)O(n).

Comparison:

  • Union and Intersection involve operations over two lists.
  • Membership and Cardinality work with a single list.
  • Among these, Intersection is the slowest because it requires repeated membership checks, making it O(nm)O(n \cdot m), which is typically more computationally expensive than union's O(n2)O(n^2) in most implementations.

Conclusion:

The intersection operation and union is the slowest when sets are represented as linked lists.

Test: Linked List - Question 7

Consider the function f defined below.

struct item 

  int data; 
  struct item * next; 
}; 
  
int f(struct item *p) 

  return (
          (p == NULL) || 
          (p->next == NULL) || 
          (( P->data <= p->next->data) && f(p->next))
         ); 

For a given linked list p, the function f returns 1 if and only if

Detailed Solution for Test: Linked List - Question 7

The function f() works as follows
1) If linked list is empty return 1
2) Else If linked list has only one element return 1
3) Else if node->data is smaller than equal to node->next->data and same thing holds for rest of the list then return 1
4) Else return 0

Test: Linked List - Question 8

A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time?

Detailed Solution for Test: Linked List - Question 8

Answer is not “(b) front node”, as we can not get rear from front in O(1), but if p is rear we can implement both enQueue and deQueue in O(1) because from rear we can get front in O(1). Below are sample functions. Note that these functions are just sample are not working. Code to handle base cases is missing.

/* p is pointer to address of rear (double pointer).  This function adds new 

   node after rear and updates rear which is *p to point to new node  */

void  enQueue(struct node **p, struct node *new_node)

{

    /* Missing code to handle base cases like *p is NULL */

       

     new_node->next = (*p)->next;

     (*p)->next = new_node;

     (*p) = new_node /* new is now rear */

     /* Note that p->next is again front and  p is rear */

 }

/* p is pointer to rear. This function removes the front element and 

    returns the dequeued element from the queue */

struct node *deQueue(struct node *p)

{

    /* Missing code to handle base cases like p is NULL,

        p->next is NULL,...  etc */

  

    struct node *temp = p->next;

    p->next = p->next->next;

    return temp;

    /* Note that p->next is again front and  p is rear */

}

Test: Linked List - Question 9

Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?

Detailed Solution for Test: Linked List - Question 9

Following are simple steps.

    struct node *temp  = X->next;
    X->data  = temp->data;
    X->next  = temp->next;
    free(temp); 

Test: Linked List - Question 10

You are given pointers to first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list?

Detailed Solution for Test: Linked List - Question 10

a) Can be done in O(1) time by deleting memory and changing the first pointer.

b) Can be done in O(1) time, see push()

c) Delete the last element requires pointer to previous of last, which can only be obtained by traversing the list.

d) Can be done in O(1) by changing next of last and then last.

Test: Linked List - Question 11

Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-case time complexity of the best known algorithm to delete the node x from the list?

Detailed Solution for Test: Linked List - Question 11

To delete node x, we should find out the previous node to the x. To find the previous nodes to the x will take O(n).
Therefore it takes O(n).

Test: Linked List - Question 12

N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed. An algorithm performs the following operations on the list in this order:
Θ(N) delete, O(log N) insert, O(log N) find, and Θ(N) decrease-key

What is the time complexity of all these operations put together

Detailed Solution for Test: Linked List - Question 12

The time complexity of decrease-key operation is Θ(1) since we have the pointer to the record where we have to perform the operation. However, we must keep the doubly linked list sorted and after the decrease-key operation we need to find the new location of the key. This step will take Θ(N) time and since there are Θ(N) decrease-key operations, the time complexity becomes O(N²).

Test: Linked List - Question 13

The concatenation of two lists is to be performed in O(1) time. Which of the following implementations of a list should be used?

Detailed Solution for Test: Linked List - Question 13

Singly linked list cannot be answer because we cannot find last element of a singly linked list in O(1) time.

Doubly linked list cannot also not be answer because of the same reason as singly linked list.

Test: Linked List - Question 14

Consider the following statements:
i.   First-in-first out types of computations are efficiently supported by STACKS.
ii.  Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for almost all the basic LIST operations.
iii. Implementing QUEUES on a circular array is more efficient than implementing QUEUES on a linear array with two indices.
iv.  Last-in-first-out type of computations are efficiently supported by QUEUES.

Which of the following is correct?

Detailed Solution for Test: Linked List - Question 14

i -STACK is the data structure that follows Last In First Out (LIFO) or First In Last Out (FILO) order, in which the element which is inserted at last is removed out first.

ii – Implementing LISTS on linked lists is more efficient than implementing it on an array for almost all the basic LIST operations because the insertion and deletion of elements can be done in O(1) in Linked List but it takes O(N) time in Arrays.

iii- Implementing QUEUES on a circular array is more efficient than implementing it on a linear array with two indices because using circular arrays, it takes less space and can reuse it again.

iv – QUEUE is the data structure that follows First In First Out (FIFO) or Last In Last Out (LILO) order, in which the element which is inserted first is removed first.

only (ii) and (iii) are TRUE.
Option (A) is correct.

Test: Linked List - Question 15

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let ‘enqueue’ be implemented by inserting a new node at the head, and ‘dequeue’ be implemented by deletion of a node from the tail.


Which one of the following is the time complexity of the most time-efficient implementation of ‘enqueue’ and ‘dequeue, respectively, for this data structure?

Detailed Solution for Test: Linked List - Question 15

For Enqueue operation, performs in constant amount of time (i.e., Θ(1)), because it modifies only two pointers, i.e.,

Create a Node P.
P-->Data = Data
P-->Next = Head
Head = P
For Dequeue operation, we need address of second last node of single linked list to make NULL of its next pointer. Since we can not access its previous node in singly linked list, so need to traverse entire linked list to get second last node of linked list, i.e.,

temp = head;
 While( temp-Next-->Next != NULL){
        temp = temp-Next;
        }
temp-->next = NULL;
Tail = temp;
Since, we are traversing entire linked for each Dequeue, so time complexity will be Θ(n).

Option (B) is correct.

Information about Test: Linked List Page
In this test you can find the Exam questions for Test: Linked List solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Linked List, EduRev gives you an ample number of Online tests for practice

Top Courses for Computer Science Engineering (CSE)

Download as PDF

Top Courses for Computer Science Engineering (CSE)