Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
hard

Tracing an Array Rotation Method

Mark
< Prev
Next >

Consider the following rightRotate method.

public static void rightRotate(int[] data, int shift) {
        int len = data.length;
        shift = shift % len;

        for (int start = 0; start < 1; start++) {
            int current = start;
            int prevVal = data[current];

            boolean first = true;
            while (first || current != start) {
                first = false;
                int next = (current + shift) % len;
                int temp = data[next];
                data[next] = prevVal;
                prevVal = temp;
                current = next;
            }
        }
    }

What are the contents of the array after the method call rightRotate(new int[]{1, 2, 3, 4, 5}, 3) is executed?

A

3, 4, 5, 1, 2

B

4, 5, 3, 2, 1

C

5, 4, 3, 2, 1

D

5, 1, 2, 3, 4

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

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

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