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

Week-10 Assignment - July - 2024

Uploaded by

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

Week-10 Assignment - July - 2024

Uploaded by

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

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

DATA STRUCTURES AND ALGORITHMS USING JAVA


Assignment-10
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10×1 = 10
______________________________________________________________________________

QUESTION 1:
Which of the following statements is true for class randomAccessFile?

a. It allows us to handle files randomly.

b. We can move the file pointer to any position in the file and perform read, write, or read-
write operations at that position.
c. This class doesn’t provide flexibility to perform read and write operations simultaneously
d. It allows read or write or read-write simultaneously.

Correct Answer: a,b,c,d

Detailed Solution:
In Java, the RandomAccessFile class allows us to handle files randomly, meaning we can move the file
pointer to any position in the file and perform read, write, or read-write operations at that position. This
class provides the flexibility to simultaneously perform all of these operations on the file.

___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 2:
What does the following Java program do?

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileExample {


static final String FILEPATH = "NPTEL.txt";

public static void main(String[] args) {


try {
System.out.println(new String(readFromFile(FILEPATH, 0, 18)));
} catch (IOException e) {
e.printStackTrace();
}
}

private static byte[] readFromFile(String filePath, int position, int size)


throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
}
}
What is the main purpose of the provided Java program?
a. It writes data to a file named "NPTEL.txt".
b. It reads the entire content of a file named "NPTEL.txt".
c. It reads a specified portion of a file named "NPTEL.txt" and prints it to the console.
d. It creates a new file named "NPTEL.txt" and stores data in it.

Correct Answer: c

Detailed Solution:
The program reads a portion of the "NPTEL.txt" file (specified by the readFromFile method) and prints
that portion to the console.

___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 3:
Fill in the blanks in the context of Linear Searching.

Case Number of Key Comparisons Asymptotic Complexity Remark

Case 1 T(n) = 1 T(n) = O(1) ________

Case 2 T(n) = n T(n) = O(n) _________

Case 3 T(n) = (n+1)/2 T(n) = O(n) __________

Choose the best option for different cases:

a. Case1: Average case


Case2: Worst Case
Case3: Best Case

b. Case1: Best case


Case2: Average Case
Case3: Average Case

c. Case1: Best case


Case2: Worst Case
Case3: Average Case

d. Case1: Best case


Case2: Average Case
Case3: Worst Case

Correct Answer: c

Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

refer Slide 42

___________________________________________________________________________
QUESTION 4:
Linear searching with linked list in the best case will have an asymptotic complexity?
a. O(logn)
b. O(1)
c. O(n)
d. O(n/2)

Correct Answer: b

Detailed Solution:

___________________________________________________________________________
QUESTION 5:
Which of the following statements about interpolation search is/are Incorrect?
a. Interpolation search works efficiently on sorted arrays.
b. Interpolation search is a type of binary search.
c. Interpolation search always outperforms linear search.
d. Interpolation search uses a fixed mid-point for dividing the search space.
Correct Answer: b,c,d
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Interpolation search is most efficient when used on sorted arrays. It is not a type of binary search,
and its performance depends on the distribution of data, which allows it to potentially outperform
linear search. Interpolation search dynamically calculates the mid-point based on the values in
the array, so it doesn't use a fixed mid-point.
___________________________________________________________________________
QUESTION 6:
Which of the following is TRUE in case of Hashing?
a. Hashing is a mapping from the Key to its index(location).
b. Division Method
H(k) = k (mod h) if indices start from 0
H(k) = k (mod h)+1 if indices start from 1
c. Collision resolution techniques are Closed hashing and Open hashing
d. linear probing and chaining are not collision resolution techniques

Correct Answer: a,b,c

Detailed Solution:
● Hashing is indeed a mapping from the key to its index or location.
● The Division Method is a valid hashing technique.
● Collision resolution techniques include Closed hashing (also known as open addressing) and
Open hashing (also known as separate chaining).
● Linear probing and chaining are indeed collision resolution techniques used in closed hashing.
Therefore, statement d is false.

___________________________________________________________________________
QUESTION 7:
Which of the following is/are a condition for Closed Hashing to stop from searching?
a. The key value is found.
b. An unoccupied (or empty) location is encountered.
c. It reaches the location where the search was started.
d. The hash table is resized.
Correct Answer: a,b,c
Detailed Solution:
In closed hashing (open addressing), the search stops under the following conditions:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

1.The key value is found.


2.An unoccupied (or empty) location is encountered.
3. It reaches the location where the search was started.
Resizing the hash table is not a condition for stopping the search; it's an operation that might be
performed to maintain the load factor or reduce collisions. Therefore, option d is the correct answer.

___________________________________________________________________________\

QUESTION 8:
Which sorting technique is shown in the illustration below?

a. Merge
b. Bubble
c. Insertion
d. Selection

Correct Answer: b

Detailed Solution:
The illustration shows bubble sort.
Refer slide #156

___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:
What does the following code represent?

1. for i = 1 to (n-1) do // (n-1) iterations


2. j = SelectMin(i, n) // Select the smallest from the remaining part
of the list
3. if (i ≠ j) then // Interchange when the minimum is
in remote
4. swap(A[i], A[j])
5. endif
6. endfor
7. stop
Choose the correct option.
a. A binary search algorithm
b. A bubble sort algorithm
c. A selection sort algorithm
d. A quicksort algorithm
Correct Answer: c
Detailed Solution:

The provided code represents a **selection sort algorithm**. Here's a breakdown of how it
works:

1. The loop runs from `i = 1` to `(n-1)` for a total of `(n-1)` iterations. In each iteration, it selects
the `i`-th element as the current minimum.

2. The `SelectMin(i, n)` function is called to find the index `j` of the smallest element in the
remaining part of the list from `i` to `n`. This function helps identify the minimum element in the
unsorted portion of the list.

3. If `i` is not equal to `j`, it means that the minimum element is not at index `i`, so a swap
operation is performed between elements at indices `i` and `j`. This step ensures that the
minimum element is moved to its correct position in the sorted portion of the list.

4. The loop continues to the next iteration, and the process repeats until all elements are sorted.

5. Once the loop completes all iterations, the sorting process is finished, and the algorithm stops.
So, the correct answer is **C. A selection sort algorithm**, as it accurately describes the sorting
process outlined in the code.
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 10:
What does the following pseudocode represent, and what is its purpose?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

1. B[1]=A[1] // Initially first element in the input list is the first element in the output list
2. For j = 2 to N do // Continue taking one key at a time from the input list
/* Select the key K from the input list */
3. K = A[j] // K is the key under insertion
/* Search the final location i in the output list */
4. flag = TRUE // To control the number of scan in the output list
5. i = j-1 // Points the rightmost element in the output list
6. While (flag = TRUE) do // Scan until we get the place for K
7. If (K < B[i]) then
8. i = i-1
9. If (i = 0) then // Stop if the list is exhausted
10. flag = FALSE
11. EndIf
12. Else
13. flag = FALSE // Stop here
14. EndIf
15. EndWhile
/* Move to the right one place all the keys from and after i+1 */
16. p = j
17. While (p>i+1) do
18. B[p] = B[p-1]
19. p = p-1
20. EndWhile
/* Insert the keys at i+1 th place */
21. B[i+1] = K // Insert the key at the (i+1)-th place
22. EndFor
23. Stop

Choose the correct answer.

a. This pseudocode represents a sorting algorithm called Bubble Sort, used to sort elements
in ascending order.
b. This pseudocode represents an algorithm for inserting a new element into an existing list
and maintaining the list's sorted order.
c. This pseudocode represents a stack-based data structure used for storing and retrieving
elements in a last-in, first-out (LIFO) manner.
d. This pseudocode represents a binary search algorithm for finding a specific element in a
sorted list.

Correct Answer: b
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Detailed Solution:
The provided pseudocode describes an insertion sort algorithm, which is used to insert elements into a list
in a sorted order. It iteratively selects a key (K) from an input list (A) and searches for its final location (i)
in the output list (B) while maintaining the sorted order of elements. It then inserts the key at the correct
position in the output list.

___________________________________________________________________________

************END************

You might also like