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
hard

2D Array Spiral Traversal

Mark
< Prev
Next >

Consider the following method:

public static int[][] spiral(int n) {
    int[][] result = new int[n][n];
    int value = 1;
    int top = 0, bottom = n - 1, left = 0, right = n - 1;
    
    while (top <= bottom && left <= right) {
        for (int c = left; c <= right; c++) {
            result[top][c] = value++;
        }
        top++;
        
        for (int r = top; r <= bottom; r++) {
            result[r][right] = value++;
        }
        right--;
        
        if (top <= bottom) {
            for (int c = right; c >= left; c--) {
                result[bottom][c] = value++;
            }
            bottom--;
        }
        
        if (left <= right) {
            for (int r = bottom; r >= top; r--) {
                result[r][left] = value++;
            }
            left++;
        }
    }
    return result;
}

What value is stored at index [1][1] in the 2D array returned by the method call spiral(4)?

A

11

B

6

C

10

D

13

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: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|