NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Chapter 8 Multidimensional Arrays

Section 8.2 Two-Dimensional Array Basics
1. Which of the following statements are correct?
a. char[][] charArray = {'a', 'b'};
b. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
c. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}};
d. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};
Key:d

#
2. Assume double[][] x = new double[4][5], what are x.length and x[2].length?
a. 4 and 4
b. 4 and 5
c. 5 and 4
d. 5 and 5
Key:b

#
3. What is the index variable for the element at the first row and first column in array a?
a. a[0][0]
b. a[1][1]
c. a[0][1]
d. a[1][0]
Key:a

#
4. When you create an array using the following statement, the element values are automatically initialized to 0.

int[][] matrix = new int[5][5];

a. True
b. False
Key:a

#
5. How many elements are array matrix (int[][] matrix = new int[5][5])?

a. 14
b. 20
c. 25
d. 30
Key:c

#
6. Analyze the following code:

public class Test {
public static void main(String[] args) {
boolean[][] x = new boolean[3][];
x[0] = new boolean[1]; x[1] = new boolean[2];
x[2] = new boolean[3];

System.out.println("x[2][2] is " + x[2][2]);
}
}
a. The program has a compile error because new boolean[3][] is wrong.
b. The program has a runtime error because x[2][2] is null.
c. The program runs and displays x[2][2] is null.
d. The program runs and displays x[2][2] is true.
e. The program runs and displays x[2][2] is false.
Key:e x is a ragged array. (See the section on Ragged Array) x[2] has three elements with default value false.

#
7. Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?
a. 2 and 1
b. 2 and 2
c. 3 and 2
d. 2 and 3
e. 3 and 3
Key:c

#
8. Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?
a. 2, 3, and 3
b. 2, 3, and 4
c. 3, 3, and 3
d. 3, 3, and 4
e. 2, 2, and 2
Key:b

#
Section 8.3 Processing Two-Dimensional Arrays
9. What is the output of the following program?

public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

int v = values[0][0];
for (int row = 0; row < values.length; row++)
for (int column = 0; column < values[row].length; column++)
if (v < values[row][column])
v = values[row][column];

System.out.print(v);
}
}

a. 1
b. 3
c. 5
d. 6
e. 33
Key:e

#
10. What is the output of the following program?

public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

int v = values[0][0];
for (int[] list : values)
for (int element : list)
if (v > element)
v = element;

System.out.print(v);
}
}


a. 1
b. 3
c. 5
d. 6
e. 33
Key:a

#
11. What is the output of the following program?

public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}};

for (int row = 0; row < values.length; row++) {
java.util.Arrays.sort(values[row]);
for (int column = 0; column < values[row].length; column++)
System.out.print(values[row][column] + " ");
System.out.println();
}
}
}

a. The program prints two rows 3 4 5 1 followed by 33 6 1 2
b. The program prints on row 3 4 5 1 33 6 1 2
c. The program prints two rows 3 4 5 1 followed by 2 1 6 33
d. The program prints two rows 1 3 4 5 followed by 1 2 6 33
e. The program prints one row 1 3 4 5 1 2 6 33
Key:d

#
12. What is the output of the following code?

public class Test {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15}};

for (int i = 0; i < 4; i++)
System.out.print(matrix[i][1] + " ");
}
}

a. 1 2 3 4
b. 4 5 6 7
c. 1 3 8 12
d. 2 5 9 13
e. 3 6 10 14
Key:d

#
13. What is the output of the following code?
public class Test5 {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15}};

for (int i = 0; i < 4; i++)
System.out.print(matrix[1][i] + " ");
}
}
a. 1 2 3 4
b. 4 5 6 7
c. 1 3 8 12
d. 2 5 9 13
e. 3 6 10 14
Key:b

#
Section 8.4 Passing Two-Dimensional Arrays to Methods
14. Suppose a method p has the following heading:

public static int[][] p()

What return statement may be used in p()?

a. return 1;
b. return {1, 2, 3};
c. return int[]{1, 2, 3};
d. return new int[]{1, 2, 3};
e. return new int[][]{{1, 2, 3}, {2, 4, 5}};
Key:e

#
15. What is the output of the following program?

public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

for (int row = 0; row < values.length; row++) {
System.out.print(m(values[row]) + " ");
}
}

public static int m(int[] list) {
int v = list[0];
for (int i = 1; i < list.length; i++)
if (v < list[i])
v = list[i];
return v;
}
}

a. 3 33
b. 1 1
c. 5 6
d. 5 33
e. 33 5
Key:d

#
Section 8.8 Multidimensional Arrays
16. Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?
a. 4, 5, and 6
b. 6, 5, and 4
c. 5, 5, and 5
d. 4, 5, and 4
Key:a

#
17. Which of the following statements are correct?
a. char[][][] charArray = new char[2][2][];
b. char[2][2][] charArray = {'a', 'b'};
c. char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}};
d. char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};
Key:ad

#
18. What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[][][] data = {{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}};

System.out.print(data[1][0][0]);
}
}
a. 1
b. 2
c. 4
d. 5
e. 6
Key:d

#
19. What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[][][] data = {{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}};

System.out.print(ttt(data[0]));
}

public static int ttt(int[][] m) {
int v = m[0][0];

for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
if (v < m[i][j])
v = m[i][j];

return v;
}
}
a. 1
b. 2
c. 4
d. 5
e. 6
Key:c
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.