Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 8: 2D Array
Start Practice TestPractice Test
About Exam
easy

2D Array Main Diagonal

Mark
< Prev
Next >

The setDiagonal method below is intended to modify its 2D array parameter, matrix, by setting each element on the main diagonal to 1. Which line of code should replace /* missing code */ for the method to work as intended?

public class Diagonal {
    public static void setDiagonal(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            /* missing code */
        }
    }
    public static void main(String[] args) {
        int[][] square = new int[3][3];
        setDiagonal(square);
        System.out.println(square[1][1]);
    }
}
A

matrix[i][matrix.length - 1 - i] = 1;

B

matrix[i][i] = 1;

C

matrix[i][0] = 1;

D

matrix[0][i] = 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.

AI Tutor

How can I help?

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