Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please I need the anwers as soon as possoble and thank you. QUESTION 1 Suppose you wish to implement the Comparable interface to allow your

please I need the anwers as soon as possoble and thank you.

QUESTION 1

Suppose you wish to implement the Comparable interface to allow your Vehicle class to compare Auto objects only. Which of the following is the correct way to do this?

public class Auto implements Comparable

public class Vehicle implements Comparable

public class Vehicle implements Comparable

public class Vehicle implements Comparable(Auto)

1 points

QUESTION 2

The analysis for the number of visits in a binary search begins with the equation,

T(n) = T(n / 2) + 1. What does the number 1 represent in this equation?

One element visit before a recursive call on one half of the array.

The total number of recursions required.

The fact that the number of elements is odd.

The fact that we search either the left half or the right half, but not both.

1 points

QUESTION 3

Consider an array with n elements. If we visit each element n times, how many total visits will there be?

n

2n

nn

n2

1 points

QUESTION 4

Complete the following code for an interface that classes who wish to compare Auto objects can implement.

public interface Comparator 
{ 
 _____________________ 
} 

boolean compare(Auto a, Auto b);

single compareTo(Auto a, Auto b);

int compare(Auto a, Auto b);

single compare(Auto a, Auto b);

1 points

QUESTION 5

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements?

It must return 1 if a comes before b, 0 if they are the same, and -1 if a comes after b.

It must return -1 if a comes before b, 0 if they are the same, and 1 if a comes after b.

It must return a positive value if a comes before b, 0 if they are the same, and a negative value if a comes after b.

It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b.

1 points

QUESTION 6

How many comparisons does selection sort make when sorting an array of length n?

n

log(2n)

n( n + 1) / 2

n

1 points

QUESTION 7

Which sort algorithm starts with an initial sequence of size 1, which is assumed to be sorted, and increases the size of the sorted sequence in the array in each iteration?

insertion sort

selection sort

merge sort

quicksort

1 points

QUESTION 8

A binary search is generally ____ a linear search.

slower than

equal to

less efficient than

faster than

1 points

QUESTION 9

Consider the sort method for selection sort shown below:

public static void sort (int[] a) 
{ 
 for (int i = 0; i < a.length  1; i++) 
 { 
 int minPos = minimumPosition(i); 
 swap(minPos, i); 
 } 
} 

Suppose we modify the loop control to read int i = 1; i < a.length 1; i++. What would be the result?

An exception would occur

The sort would not consider the last array element.

The sort would not consider the first array element.

The sort would still work correctly.

1 points

QUESTION 10

A version of which sort algorithm is used in the sort method in the Java Arrays class?

merge sort

quicksort

selection sort

insertion sort

1 points

QUESTION 11

Suppose you wish to sort an array list of objects, but the object class does not implement the Comparable interface. Because you are not allowed to modify this class, you decide to provide a comparator object that implements the Comparator interface. Which method must you implement from this interface to achieve your objective?

the sort method.

the compare method.

the compareTo method.

the compareObject method.

1 points

QUESTION 12

When the size of an array increases by a factor of 100, the time required by selection sort increases by a factor of ____.

2,000

5,000

10,000

12,000

1 points

QUESTION 13

If a call to the Arrays static method binarySearch returns a value of -10, what can be concluded?

I the element is not in the array

II the element is at index 10

III the element can be inserted at index 9

I

II

III

I and III

1 points

QUESTION 14

Suppose a developer gets class XYZ files and documentation from a subcontractor. This class does not implement the Comparable interface. What must be true in order for the developer to sort an array of XYZ objects without modifying the xyz class?

The XYZ class must implement the Sortable interface.

XYZ objects must be randomly distributed.

XYZ objects must be ordered.

The developer must supply a comparator object belonging to a class that implements the Comparator interface.

1 points

QUESTION 15

Consider the swap method shown below from the SelectionSorter class. If we modified it as shown in the swap2 method shown below, what would be the effect on the sort method?

private static void swap(int[] a, int i, int j) 
{ 
 int temp = a[i]; 
 a[i] = a[j]; 
 a[j] = temp; 
} 
 
private static void swap2(int[] a, int i, int j) 
{ 
 a[i] = a[j]; 
 a[j] = a[i]; 
} 

There would be no effect.

Some array elements would be overwritten.

It would sort the array in reverse order.

It would still be correct, but run a little faster.

1 points

QUESTION 16

Consider the following code snippet:

public static void sort(int[] a) 
{ 
 for (int i = 1; i < a.length; i++) 
 { 
 int next = a[i]; 
 int j = i; 
 while (j > 0 && a[j - 1] > next) 
 { 
 a[j] = a[j - 1]; 
 j--; 
 } 
 a[j] = next; 
 } 
} 

What sort algorithm is used in this code?

insertion sort

selection sort

merge sort

quicksort

1 points

QUESTION 17

Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search?

8

4

3

2

1 points

QUESTION 18

Which selection sort iteration guarantees the array is sorted for a 10-element array?

9th iteration

10th iteration

1st iteration

impossible to tell

1 points

QUESTION 19

What is the smallest value of n for which n2> 3n + 4?

6

5

4

3

1 points

QUESTION 20

How many times can an array with 729 elements be cut into three equal pieces?

4

5

6

7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions