reset password
Author Message
rabbott
Posts: 1649
Posted 23:03 Mar 10, 2014 |
 
Grade

10

10

10

10

10

10

10

9

9

9

8

7

7

6

6

6

6

ladodgersfan
Posts: 13
Posted 15:36 Mar 11, 2014 |

what is the answer for question 5 i have b but it is marked wrong, i just want to make sure

rabbott
Posts: 1649
Posted 16:58 Mar 11, 2014 |

Question 5.

Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array, the  one with the 'X'?

_-_ _-_ _-_ _-_

_-_ _-_ _X_ _-_

_-_ _-_ _-_ _-_

 System.out.println(arr[1][2]);
 System.out.println(arr[2][3]);
 System.out.println(arr[2][1]);
 System.out.println(arr[3][2]);
 
The correct answer is (a). The wording of the question ("second row and third column") suggests (b). That's why a diagram is also included. This question tests whether you know that array indexing starts at 0. An easy way to test this is to create an array and print the contents of those cells.

public static void q5(){
        int [][] arr = 
            new int[][] 
               {{0, 1, 2, 3},
                {4, 5, 6, 7},
                {8, 9, 10, 11},
                {12, 13, 14, 15}};

        System.out.print (arr[1][2] + " " + arr[2][3] + " " + arr[2][1] + " " + arr[3][2]);

    }

The output is: 6  11  9  14

Last edited by rabbott at 17:08 Mar 11, 2014.
ytsai8
Posts: 47
Posted 19:59 Mar 11, 2014 |

@LADODGERSFAN

You probably forgot the indexes are starting with [0].. :\