0% found this document useful (0 votes)
13 views

Week-04 Assignment - 2023-1

This document contains a 10 question multiple choice quiz about data structures and algorithms using Java. The questions cover topics like time complexity of searching a linked list, printing a linked list in reverse order using recursion, inserting a node at the end of a linked list, advantages of doubly linked lists over singly linked lists, and runtime of deletion operations on different data structures. For each question, the correct answer and a detailed explanation is provided.

Uploaded by

Prabha K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Week-04 Assignment - 2023-1

This document contains a 10 question multiple choice quiz about data structures and algorithms using Java. The questions cover topics like time complexity of searching a linked list, printing a linked list in reverse order using recursion, inserting a node at the end of a linked list, advantages of doubly linked lists over singly linked lists, and runtime of deletion operations on different data structures. For each question, the correct answer and a detailed explanation is provided.

Uploaded by

Prabha K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA STRUCTURES AND ALGORITHMS USING JAVA

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?

int method(struct node* head)


{
if(head == NULL)
return 0;

method(head.next);
System.out.print(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

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;

public Node(int data) {


this.data = data;
this.next = null;
}
}

class LinkedList {
Node head;

public LinkedList() {
head = null;
}

public void performOperation(int value) {


Node newNode = new Node(value);

if (head == null) {
head = newNode;
return;
}

Node current = head;


while (current.next != null) {
current = current.next;
}

current.next = newNode;
}

public void display() {


Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.performOperation(10);
list.performOperation(20);
list.performOperation(30);

System.out.println("Linked List elements:");


list.display();
}
}

What performOperation method does in this program?


a. To execute various operations on the linked list.
b. To perform an operation in the middle of the linked list.
c. To insert a new element at the end of the linked list.
d. To remove the last element from the linked list.

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?

a. Deletion after a node


b. Insertion after a node
c. Searching
d. Traversal

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?

a. Java LinkedList class cannot contain duplicate elements.


b. Java LinkedList class maintains insertion order.
c. Java LinkedList class is non synchronized.
d. Java LinkedList class cannot be used as a list, stack or queue.

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

b. Random Access Requirements

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?

a. Arrays are always faster than linked lists.


b. Linked lists are always faster than arrays.
c. Arrays are better for random access, while linked lists are better for sequential access.
d. Arrays and linked lists have the same performance for both random and sequential access.

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:

Feature Array Linked List

Memory Allocated in contiguous Allocated in non-contiguous


allocation memory memory

Access time Fast for random access, slow Slow for random access, fast
for sequential access for sequential access

Insertion and Slow Fast


deletion

Size Fixed Dynamic

___________________________________________________________________________

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?

a. The time complexity of the deletion operation is O(1).


b. The time complexity of the deletion operation is O(n).
c. The time complexity of the deletion operation depends on the size of the integer to be deleted.
d. The time complexity of the deletion operation depends on the location of the deletion point.

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?

a. LinkedList<String> a = new LinkedList<String>();


b. LinkedList<String> b = new LinkedList<>();
c. LinkedList<int> c = new LinkedList<String>();
d. LinkedList<Integer> d = new LinkedList<>(1, 2, 3, 4, 5);

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************

You might also like