1. What differentiates a circular linked list from a normal linked list?
A. Fixed size
B. There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
C. Insertion based on position
D. Accessing elements at specified positions
Answer: d
Explanation:
Array elements can be accessed in two steps. First, multiply the size of the data type with the specified position, second, add this value to the base address. Both of these operations can be done in constant time, hence accessing elements at a given index/position is faster.
2. What is the time complexity of inserting at the end in dynamic arrays?
A. O(1)
B. O(n)
C. O(logn)
D. Either O(1) or O(n)
Answer: D
Explanation:
Depending on whether the array is full or not, the complexity in dynamic array varies. If you try to insert into an array that is not full, then the element is simply stored at the end, this takes O(1) time. If you try to insert into an array which is full, first you will have to allocate an array with double the size of the current array and then copy all the elements into it and finally insert the new element, this takes O(n) time.
3. What is the time complexity to count the number of elements in the linked list?
A. O(1)
B. O(n)
C. O(logn)
D. O(n2)
Answer: B
Explanation:
To count the number of elements, you have to traverse through the entire list, hence complexity is O(n).
4. What is the functionality of the following code?
public void function(Node node)
{
if(size == 0)
head = node;
else
{
Node temp,cur;
for(cur = head; (temp = cur.getNext())!=null; cur = temp);
cur.setNext(node);
}
size++;
}
A. Inserting a node at the beginning of the list
B. Deleting a node at the beginning of the list
C. Inserting a node at the end of the list
D. Deleting a node at the end of the list
Answer: C
Explanation:
The for loop traverses through the list and then inserts a new node as cur.setNext(node);
5. What is the space complexity for deleting a linked list?
A. O(1)
B. O(n)
C. Either O(1) or O(n)
D. O(logn)
Answer: A
Explanation:
You need a temp variable to keep track of current node, hence the space complexity is O(1).
6. What would be the asymptotic time complexity to find an element in the linked list?
A. O(1)
B. O(n)
C. O(n2)
D. O(n4)
Answer: B
Explanation:
If the required element is in the last position, we need to traverse the entire linked list. This will take O (n) time to search the element.
7. What would be the asymptotic time complexity to insert an element at the second position in the linked list?
A. O(1)
B. O(n)
C. O(n2)
D. O(n3)
Answer: A
Explanation:
A new node is created with the required element. The pointer of the new node points the node to which the head node of the linked list is also pointing. The head node pointer is changed and it points to the new node which we created earlier. The entire process completes in O (1) time. Thus the asymptotic time complexity to insert an element in the second position of the linked list is O (1).
8. The concatenation of two lists can be performed in O(1) time. Which of the following variation of the linked list can be used?
A. Singly linked list
B. Doubly linked list
C. Circular doubly linked list
D. Array implementation of list
Answer: C
Explanation:
We can easily concatenate two lists in O (1) time using singly or doubly linked list, provided that we have a pointer to the last node at least one of the lists. But in case of circular doubly linked lists, we will break the link in both the lists and hook them together. Thus circular doubly linked list concatenates two lists in O (1) time.
9. Consider the following definition in c programming language.
struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
Which of the following c code is used to create new node?
A. ptr = (NODE*)malloc(sizeof(NODE));
B. ptr = (NODE*)malloc(NODE);
C. ptr = (NODE*)malloc(sizeof(NODE*));
D. ptr = (NODE)malloc(sizeof(NODE));
Answer: A
Explanation:
As it represents the right way to create a node.
10. What kind of linked list is best to answer questions like “What is the item at position n?”
A. Singly linked list
B. Doubly linked list
C. Circular linked list
D. Array implementation of linked list
Answer: D
Explanation:
Arrays provide random access to elements by providing the index value within square brackets. In the linked list, we need to traverse through each element until we reach the nth position. Time taken to access an element represented in arrays is less than the singly, doubly and circular linked lists. Thus, array implementation is used to access the item at the position n.
11. Linked lists are not suitable for the implementation of ___________
A. Insertion sort
B. Radix sort
C. Polynomial manipulation
D. Binary search
Answer: D
Explanation:
It cannot be implemented using linked lists.
12. Linked list is considered as an example of ___________ type of memory allocation.
A. Dynamic
B. Static
C. Compile time
D. Heap
Answer: A
Explanation:
As memory is allocated at the run time.
13. In Linked List implementation, a node carries information regarding ___________
A. Data
B. Link
C. Data and Link
D. Node
Answer: C
Explanation:
A linked list is a collection of objects linked together by references from an object to another object. By convention these objects are names as nodes. Linked list consists of nodes where each node contains one or more data fields and a reference(link) to the next node.
14. Linked list data structure offers considerable saving in _____________
A. Computational Time
B. Space Utilization
C. Space Utilization and Computational Time
D. Speed Utilization
Answer: C
Explanation:
Linked lists saves both space and time.
15. Which of the following points is/are not true about Linked List data structure when it is compared with an array?
A. Arrays have better cache locality that can make them better in terms of performance
B. It is easy to insert and delete elements in Linked List
C. Random access is not allowed in a typical implementation of Linked Lists
D. Access of elements in linked list takes less time than compared to arrays
Answer: D
Explanation:
To access an element in a linked list, we need to traverse every element until we reach the desired element. This will take more time than arrays as arrays provide random access to its elements.
16. 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.;
}
A. Prints all nodes of linked lists
B. Prints all nodes of linked list in reverse order
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverse order
Answer: B
Explanation:
fun1() prints the given Linked List in reverse manner.
For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.
17. Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity?
A. Insertion Sort
B. Quick Sort
C. Heap Sort
D. Merge Sort
Answer: D
Explanation:
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.
18. 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.
A. *head_ref = prev;
B. *head_ref = current;
C. *head_ref = next;
D. *head_ref = NULL;
Answer: A
Explanation:
*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.
19. What is the output of following function for start pointing to first node of following linked list?
1->2->3->4->5->6
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.;
}
A. 1 4 6 6 4 1
B. 1 3 5 1 3 5
C. 1 2 3 5
D. 1 3 5 5 3 1
Answer: D
Explanation:
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.
20. The following C function takes a simply-linked list as an 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. Choose the correct alternative to replace the blank line.
typedef struct node
{
int value;
struct node *next;
}Node;
Node *move_to_front(Node *heaD.
{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
return head;
}
A. q = NULL; p->next = head; head = p;
B. q->next = NULL; head = p; p->next = head;
C. head = p; p->next = q; q->next = NULL;
D. q->next = NULL; p->next = head; head = p;
Answer: D
Explanation:
When while loop completes its execution, node ‘p’ refers to the last node whereas the ‘q’ node refers to the node before ‘p’ in the linked list. q->next=NULL makes q as the last node. p->next=head places p as the first node. the head must be modified to ‘p’ as ‘p’ is the starting node of the list (head=p). Thus the sequence of steps are q->next=NULL, p->next=head, head=p.
21. 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;
}
}
A. 1, 2, 3, 4, 5, 6, 7
B. 2, 1, 4, 3, 6, 5, 7
C. 1, 3, 2, 5, 4, 7, 6
D. 2, 3, 4, 5, 6, 7, 1
Answer: B
Explanation:
The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.
22. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is?
A. log 2 n
B. n⁄2
C. log 2 n – 1
D. n
Answer: D
Explanation:
In the worst case, the element to be searched has to be compared with all elements of the linked list.
23. 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?
A. Possible if X is not last node
B. Possible if size of linked list is even
C. Possible if size of linked list is odd
D. Possible if X is not first node
Answer: A
Explanation:
Following are simple steps.
struct node *temp = X->next;
X->data = temp->data;
X->next = temp->next;
free(temp);
24. 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?
A. Delete the first element
B. Insert a new element as a first element
C. Delete the last element of the list
D. Add a new element at the end of the list
Answer: C
25. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is?
A. log2 n
B. n⁄2
C. log2 n – 1
D. n
Answer: D
Explanation:
The worst-case happens if the required element is at last or the element is absent in the list. For this, we need to compare every element in the linked list. If n elements are there, n comparisons will happen in the worst case.
- 10+ TOP C++ MCQs on OOPs Concept – 4
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on Sizes
- 10+ TOP C++ MCQs on References – 3
- 10+ TOP C++ MCQs on Operator Overloading – 1
- 10+ TOP C++ MCQs on Dereferencing
- 10+ TOP C++ MCQs on Increment and Decrement
- 10+ TOP C++ MCQs on Design of Class Hierarchies