Java OCA OCP Practice Question 770

Question

Which of the following are true? (Choose two.)

20:  int[] v [] = new int[10][20]; 
21:  for (int i = 0; i < v.length; i++) 
22:     for (int j = 0; j < v.length; j++) 
23:        v[i][j] = 'x';  
24:  System.out.println(v.size()); 
  • A. One line needs to be changed for this code to compile.
  • B. Two lines need to be changed for this code to compile.
  • C. Three lines need to be changed for this code to compile.
  • D. If the code is fixed to compile, none of the cells in the 2D array have a value of 0.
  • E. If the code is fixed to compile, half of the cells in the 2D array have a value of 0.
  • F. If the code is fixed to compile, all of the cells in the 2D array have a value of 0.


A, E.

Note

Line 24 does not compile because arrays use length.

It is ArrayList that uses size().

All of the other lines compile, making Option A correct.

It is allowed to split up the braces in the 2D array declaration on line 20.

The code is also allowed to use v.length as the loop condition on line 22.

The array starts out with all 200 of the cells initialized to the default value for an int of 0.

Both loops iterate starting at 0 and stopping before 10, which causes only half of the array to be set to 'x'.

The other half still has the initial default value of 0, making Option E correct.




PreviousNext

Related