Week-10 Assignment - July - 2024
Week-10 Assignment - July - 2024
QUESTION 1:
Which of the following statements is true for class randomAccessFile?
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.
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;
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.
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
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
___________________________________________________________________________\
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?
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
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************