| 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
< Prev
Next >

Given the following method call, rightRotate(new int[]{1, 2, 3, 4, 5}, 3);, what will be the state of the array after the function execution?

Consider the following 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;
            }
        }
    }

Given the following method call, rightRotate(new int[]{1, 2, 3, 4, 5}, 3);, what will be the state of the array after the function execution?

A

4, 5, 3, 2, 1

B

3, 4, 5, 1, 2

C

5, 1, 2, 3, 4

D

5, 4, 3, 2, 1

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy