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

Week-11 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)
24 views

Week-11 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/ 8

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

DATA STRUCTURES AND ALGORITHMS USING JAVA


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

QUESTION 1:
Which sorting algorithm is implemented in the following code:

public class Sort <T extends Comparable<T>> {


public static void Sort(T[] arr, int len) {
for (int i = 0; i < (len - 1); i++) {
int minIndex = i; // Assume the current index has the minimum value

// Find the index of the minimum element in the unsorted part of the array
for (int j = i + 1; j < len; j++) {
// Compare current minIndex value with the next element
if (arr[minIndex].compareTo(arr[j]) > 0) {
minIndex = j; // Update minIndex if a smaller element is found
}
}

// Swap the found minimum element with the first element of the unsorted part
T temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Method to print the array elements
public static <T> void printArray(T[] arr, int len) {
for (int i = 0; i < len; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}

a. Heap Sort
b. Merge Sort
c. Insertion Sort
d. Selection Sort
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Correct Answer: d. Selection Sort

Detailed Solution:
Selection sort is implemented in the provided code. For a detailed explanation, please refer to slides 90-92
from Week 11, Lec 51-55.

___________________________________________________________________________
QUESTION 2:
Given the following code snippet:

public static void main(String[] args) {

System.out.println("The Min Heap is ");


Integer[] a = new Integer[15];
Integer[] x = new Integer[15];
MinHeap minHeap = new MinHeap(a , 5 );
minHeap.insert(5);
minHeap.insert(3);
minHeap.insert(17);
minHeap.insert(10);
minHeap.insert(84);
minHeap.insert(19);
minHeap.insert(6);
minHeap.insert(22);
minHeap.insert(9);
minHeap.minHeap();
minHeap.print();
minHeap.sort(x);
}

Assume the MinHeap class has a standard implementation of a MinHeap. What will be the correct
output of the program?
a.

The Min Heap is


3 5 6 9 84 19 17 22 10
3 5 6 9 10 17 19 22 84

b.

The Min Heap is


5 3 17 10 84 19 6 22 9
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

3 5 6 9 10 17 19 22 84

c.

The Min Heap is


3 5 17 6 9 19 10 22 84
3 5 6 9 17 19 22 84 10

d.

The Min Heap is


84 22 19 17 10 9 6 5 3
84 22 19 17 10 9 6 5 3

Correct Answer: a

Detailed Solution:

The Min Heap is


3 5 6 9 84 19 17 22 10
3 5 6 9 10 17 19 22 84
___________________________________________________________________________
QUESTION 3:
Which sorting algorithm is not comparison-based?
a. Quick Sort
b. Merge Sort
c. Counting Sort
d. Bubble Sort

Correct Answer: c. Counting Sort

Detailed Solution:
Counting Sort is a non-comparison-based sorting algorithm that works well for integers or small-range
inputs.

___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 4:
What is the time complexity of Quick Sort in its average case?
a. O(n)
b. O(n log n)
c. O(n^2)
d. O(log n)
Correct Answer: b. O(n log n)

Detailed Solution:
Quick Sort has an average-case time complexity of O(n log n).

___________________________________________________________________________
QUESTION 5:
Choose the best option to fill in blank spaces given in the BubbleSort algorithm below

For i = 1 to _____(1)_____ do
For j = 1 to _____(2)_____ do
If A[j] _____(3)_____ A[j+1] then
Swap(A[j], A[j+1])
EndIf
j=j+1
EndFor
EndFor
Stop

a.
1. n-1
2. n-1
3. >
b.
1. n-1
2. n-1
3. <
c.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

1. n-1
2. n-i
3. <
d.
1. n-1
2. n-i
3. >

Correct Answer: d

Detailed Solution:
Blank (1): n-1

○ The outer loop runs from 1 to n-1 because each pass through the array sorts the
largest unsorted element into its final position.
2. Blank (2): n-i
○ The inner loop runs from 1 to n-i to avoid comparing already sorted elements.
3. Blank (3): >
○ Bubble Sort swaps elements if the current element is greater than the next
element, so A[j] > A[j+1] triggers the swap.

So, the correct options are:

1. Blank (1): n-1


2. Blank (2): n-i
3. Blank (3): >

The correct code is:

1. For i = 1 to n-1 do
2. For j = 1 to n-i do
3. If (A[j] > A[j+1]) then
4. Swap(A[j], A[j+1])
5. EndIf
6. j=j+1
7. EndFor
8. EndFor
9. Stop
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

___________________________________________________________________________
QUESTION 6:
Which library in Java contains the sort() function?
a. java.io
b. java.util
c. java.applet
d. java.swing
Correct Answer: b. java.util

Detailed Solution:
sort() function was present in java.util library.

___________________________________________________________________________
QUESTION 7:
Consider the code snippet given below.

import java.util.Arrays;
public class SortingExample {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
System.out.println("Original Array:");
printArray(arr);
________________// Fill in the blank

System.out.println("\nSorted Array (Ascending Order):");


printArray(arr);
}
// Utility function to print an array
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}

Fill in the blank to sort the array.


a. Arrays.sort(arr)
b. Arrays.sorted(arr)
c. arr.sort(Arrays)
d. sort.arr()
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Correct Answer: a Arrays.sort(arr)

Detailed Solution:
Arrays.sort(arr) is the right syntax.

___________________________________________________________________________
QUESTION 8:
Consider the code given below.

1. for (i=0;i<10;i++)
2. {
3. for(j=0;j<10;j++)
4. {
5. if(arr[i] < arr[j])
6. {
7. temp = arr[i];
8. arr[i] = arr[j];
9. arr[j] =arr[i];
10. }
11. }
12. }

Which line of code has a logical error?


a. 7
b. 10
c. 5
d. 9

Correct Answer: d. 9

Detailed Solution:
the correct line of code is arr[j] = temp.

___________________________________________________________________________

QUESTION 9:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

When implementing the Bubble Sort algorithm, what is the time complexity in the worst-
case scenario?
a. O(n)
b. O(n log n)
c. O(n^2)
d. O(log n)
Correct Answer: c. O(n^2)

Detailed Solution:
The worst-case time complexity of the Bubble Sort algorithm is O(n^2), as it involves nested loops to
compare and swap elements.

___________________________________________________________________________
QUESTION 10:
Given an array of 1 million integers with values ranging from -10,000 to 10,000, which
sorting algorithm is most suitable for sorting this array efficiently with the least amount of
memory usage?
a. Quick Sort
b. Merge Sort
c. Bubble Sort
d. Radix Sort

Correct Answer: d. Radix Sort

Detailed Solution:
Radix Sort is the most suitable option for sorting a large array of integers when the range of values is
known and relatively small, as it uses a counting sort-based approach for each digit, minimizing memory
usage. In this case, the values range from -10,000 to 10,000, which is a range of 20,001 integers, making
Radix Sort an efficient and memory-efficient choice. Quick Sort and Merge Sort have a higher memory
overhead, and Bubble Sort is not suitable for large datasets due to its time complexity.

___________________________________________________________________________
************END************

You might also like