What is the explanation for the following question

Asked

Viewed 159 times

0

This is a matter of the simulated java certification of the Whizlabs site.

public class Whizlabs {
    public static void main(String[] args) {
        int[] testData = {1, 2, 3};
        for(abc){
        }
    }
}

The question is as follows: "Choose the option can replace the text "abc" in the above code." (Select 2 options)

To. int i : testData

B. int i = 0; i < 1; i++

C. i++

D. ;i++; 1 < 1;

And. ;i < 1; 0

The correct answer is the letter "A" and "B". Why? I didn’t understand the answer.

  • You know how one works for? What is a for? Do you understand his syntax? You doubt what?

  • In case the question is just asking to go through the arrays using for, is that it? Because the letter "B" should not be i <= 1 or i < 2?

  • 2

    That’s what it looks like, even though I didn’t do anything useful, I would compile, the other three options.

  • Now I understand, so there is no need to go through the whole array, so the part of the letter "B" i<1 is correct, because I would compile normally. In the end the only mistake was my lack of attention. Thank you

  • 3

    In fact, it seems to me that they just want to know which ones have the right syntax, since they don’t have a definite goal, just something that can be put that will work in some way. There’s a lot of question is that even more understand it than the knowledge itself.

1 answer

1

The answer is the letter A. What the question is to iterate the array.

int[] testData = {1, 2, 3};
for(int i : testData){
     System.out.println(i);
}

It’s the same as doing:

int[] testData = {1, 2, 3};
for(int i = 0; i < 3; i++){
     System.out.println(i);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.