What is the result?

Given the code fragment:

What is the result?
A. A B C
B. A B C D E
C. A B D E
D. Compilation fails.

Download Printable PDF. VALID exam to help you PASS.

2 thoughts on “What is the result?

  1. The 2D-String array looks like this:
    A B C
    D E

    The outer loop runs and triggers the inner loop. i is currently 0. The inner loop runs and will run for length of arr[0], so 3 times. It prints arr[0][0] + space so “A “ and array[0][1] + space so “B “, and then breaks because of the break statement. Then the outer loop runs again, but now i = 1, so it will run for length or arr[1], which is two times. It will print arr[1][0] + space so “D “ and arr[1][1] + space so “E “. In short, it prints all the elements of the 2D-array in order, but not C because the break-statement prevents the inner loop from printing elements of the first row after it reaches B.

  2. C
    public static void main(String[] args) {
    String[][] StringArr = {{“A”,”B”,”C”},{“D”,”E”}};
    for(int i=0 ; i<StringArr.length;i++){
    for(int j=0;j<StringArr[i].length;j++){
    System.out.println(StringArr[i][j]+" ");
    break; //break the inner loop and go to other iteration i =1
    continue;// is unnecessary as the last statement in a loop
    so A B D E

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.