Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
medium Solved by 7 students

Insertion Sort Loop Condition Error

Mark
< Prev
Next >

The following insertionSort method is intended to sort an array of integers but contains a logical error. Which statement best describes the error?

public class InsertionSortError {
    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            while(j > 0 && arr[j] > key) {
                arr[j+1] = arr[j];
                j--; 
            }
            arr[j+1] = key;
        }
    }
}
A

The key should be stored as a float for better precision.

B

The for-loop should start at i = 0 instead of i = 1.

C

Elements should be shifted to the left rather than to the right.

D

The while loop condition should be ‘j >= 0’ instead of ‘j > 0’, to ensure the first element is compared.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1ngvandangthanh11 - 100
#2sfsdfdsfsd11 0m 01s 99
#3Potter1611 0m 31s 69
#4bommasam00012 1m 13s 17
#5sssemihunal01 0m 01s -11
#6lsj0803092212 2m 43s -73
#7parth.taur22711 7m 44s -364
#8jeonsaw172314 24m 16s -1,386
Items per page:
10
1 – 8 of 8
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|