385,745 views
31 votes
31 votes
Consider the following code segment, where twoD is a two-dimensional (2D) String array. The code segment is intended to display "JAVA".

System.out.print(twoD[2][1]);

System.out.print(twoD[3][2]);

System.out.print(twoD[1][1]);

Which of the following code segments properly declares and initializes twoD so that the code segment works as intended?

A) String[][] twoD = {{"V", "AV", "J"}, {"JA", "VA", "A"},

{"JA", "J", "JAV"}, {"AV", "V", "A"}};

B) String[][] twoD = {{"VA", "J", "A", "V"}, {"J", "A", "V", "A"},

{"AV", "A", "JA", "V"}};

C) String[][] twoD = {{"VA", "J", "V", "JA"}, {"J", "JA", "A", "VA"},

{"J", "VA", "A", "V"}};

D) String[][] twoD = {{"A", "VA", "J", "V"}, {"VA", "A", "JA", "V"},

{"VA", "J", "A", "V"}};

E) String[][] twoD = {{"A", "V"}, {"VA", "J"}, {"J", "A"}, {"A", "AV"}};

User Catalandres
by
2.8k points

1 Answer

21 votes
21 votes

Answer:

A) String[][] twoD = {{"V", "AV", "J"}, {"JA", "VA", "A"}, {"JA", "J", "JAV"}, {"AV", "V", "A"}};

Step-by-step explanation:

The code segment that would make this snippet of code work as intended would be A)

String[][] twoD = {{"V", "AV", "J"}, {"JA", "VA", "A"}, {"JA", "J", "JAV"}, {"AV", "V", "A"}};

This initializiation of array twoD would spell out JAVA with the code in the question because each of the following calls the following strings

twoD[2][1] = "J"

twoD[3][2] = "A"

twoD[1][1] = "VA"

The other options either don't have an index of 3 available or output the wrong letter sequences.

User Violin Yanev
by
3.2k points