Week-04 Assignment - 2023-1
Week-04 Assignment - 2023-1
Assignment 4
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10×1 = 10
______________________________________________________________________________
QUESTION 1:
In the worst case, the number of comparisons needed to search an item stored in a single
linked list structure of length 100 is
a. 98
b. 101
c. 99
d. 100
Correct Answer: d
Detailed Solution:
In the worst case, the item to be searched is not in the linked list. In this case, the search algorithm will
have to compare the item with all 100 nodes in the linked list before it gives up.
___________________________________________________________________________
QUESTION 2:
What does the following function do given a linked list with the first node as head?
method(head.next);
System.out.print(head.data);
}
Correct Answer: b
Detailed Solution:
Method prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5,
method() prints 5->4->3->2->1.
___________________________________________________________________________
QUESTION 3:
Consider the given Java code snippet that implements a singly linked list:
class Node {
int data;
Node next;
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
if (head == null) {
head = newNode;
return;
}
current.next = newNode;
}
Correct Answer: C
Detailed Solution:
The given code insert an element at the end of the list. So option c is the correct choice.
___________________________________________________________________________
QUESTION 4:
Which of the following operations is performed more efficiently by a doubly linked list than
by linear linked list?
Correct Answer: a
Detailed Solution:
In a singly linked list, two iterations are required to perform deletion which can be done more
efficiently by a doubly linked list.
___________________________________________________________________________
QUESTION 5:
What is the time complexity of deletion from the end in a circular doubly linked list?
a. O(1)
b. O(n)
c. O(log n)
d. O(log log n)
Correct Answer: a
Detailed Solution:
O(1) is the time complexity because in a circular doubly linked list, the last element can be
directly accessed.
___________________________________________________________________________
QUESTION 6:
Which of the following statement(s) is(are) true about Java LinkedList?
Correct Answer: b, c
Detailed Solution:
Java LinkedList class can contain duplicate elements. It maintains insertion order.
Java LinkedList class is non synchronized and can be used as a list, stack or queue.
___________________________________________________________________________
QUESTION 7:
For which of the following applications, Linkedlist may not be the good choice?
a. Sparse matrix manipulation
c. Polynomial manipulation
d. memory management
Correct Answer: b
Detailed Solution:
The correct answer is b. Random Access Requirements.
Linked lists are not good for applications that require random access to the elements. This is because
linked lists are sequential data structures, and you have to traverse the list from the beginning to find the
element you want. This can be inefficient if the list is large.
Sparse matrix manipulation, polynomial manipulation, and memory management are all applications
that do not require random access to the elements. In fact, they can benefit from the linked list's ability
to insert and delete elements quickly.
Here is a more detailed explanation of why linked lists are not good for random access:
● In a linked list, each element is a node that contains two pointers: one to the data and one to the
next node. This means that to access an element in a linked list, you have to follow the pointers
starting from the beginning of the list.
● If the list is large, this can be very inefficient. For example, if you want to access the 100th
element in a list of 1000 elements, you have to follow 99 pointers.
● In contrast, arrays allow you to access any element directly by its index. This makes arrays much
faster for random access.
Therefore, linked lists are not a good choice for applications that require random access to the elements.
However, they can be a good choice for applications that do not require random access, such as sparse
matrix manipulation, polynomial manipulation, and memory management.
___________________________________________________________________________
QUESTION 8:
Which of the following statements is true about arrays and linked lists in Java?
Correct Answer: C
Detailed Solution:
The correct answer is (c) Arrays are better for random access, while linked lists are better for sequential
access.
Arrays are better for random access because you can access any element in the array directly by its
index. This makes arrays much faster for accessing elements that are not near the beginning or end of
the array.
Linked lists are better for sequential access because you can traverse the list from the beginning to the
end without having to jump around. This makes linked lists much faster for accessing elements that are
near the beginning or end of the list.
Here is a table that summarizes the differences between arrays and linked lists in Java:
Access time Fast for random access, slow Slow for random access, fast
for sequential access for sequential access
___________________________________________________________________________
QUESTION 9:
Suppose you have a linked list of integers and you want to delete the first node in the list. Which of
the following statements is true?
Correct Answer: a
Detailed Solution:
The correct answer is (a) The time complexity of the deletion operation is O(1).
In a linked list, each element is a node that contains two pointers: one to the data and one to the next
node. This means that to delete the first node in the list, you simply need to change the next pointer of
the second node to point to the third node. The time complexity of this operation is constant, regardless
of the size of the list.
In contrast, if the list were an array, the deletion operation would be O(n). This is because you would
have to shift all of the elements in the array to the left to fill in the gap left by the deleted node. The time
complexity of this operation depends on the size of the array, which is why it is O(n).
___________________________________________________________________________
QUESTION 10:
Which of the following is not a valid declaration of a collection of type List?
Correct Answer: c
Detailed Solution:
A type should be a class where int is a primitive type. Others are correct as per the syntax
___________________________________________________________________________
************END************