The question asks for the options that will correctly create and initializean array of Strings.
The evaluation results shows that the option 3 is a valid answer,
As I agree that this statement correctly creates a string array, but it never initialize that arrray. It creates an array first and then assigns value later. How it could be the valid answer then?
1. The question doesn't mention that it has to be in a single statement. So, you have to consider the whole option (which means both the statements together) as one.
2. Even if you consider just the first statement i.e. String[] sA = new String[1]; sA is actually being declared as well as initialized. It is being initialized to new String[1] and its element sa[0] is initialized to null.
So both ways, it is a correct option.
If you like our products and services, please help us by posting your review here.
@The question doesn't mention that it has to be in a single statement... Initialization cannot happen in multiple statements. If we assign values at the same time when we declare is called initialization right? So even though the question does not explicitly mention that it has to be a single statement, the word initialization mandates it to be single statement.
@Even if you consider just the first statement ... It initialize the array with null string. but the question explicitly says, "initialize with non null elements".
>@The question doesn't mention that it has to be in a single statement... Initialization cannot happen in multiple statements. If we assign values at the same time when we declare is called initialization right?
No! Where did you read that? Initialization can happen anywhere anytime. It just means assigning a value for the first time before the variable/reference is used.
>@Even if you consider just the first statement ... It initialize the array with null string. but the question explicitly says, "initialize with non null elements".
Yes, you are right here. The question does say non-null. But the option is still correct
If you like our products and services, please help us by posting your review here.
The question is: Which of the following statements will correctly create and initialize an array of Strings to non null elements?
I've always learned that an array has a fixed size and that you have specify the length with the declaration. When im looking at the answers i dont see the length specification in the declaration. Could you explain why the following answers dont have that and what the use is of the curly brackets?
I've always learned that an array has a fixed size and that you have specify the length with the declaration.
It looks like the source of your information is not good because the second part of your statement is incorrect. In fact, you can't specify the size in array declaration. You specify the size while instantiating the array. The size can be specified directly or indirectly.
When im looking at the answers i dont see the length specification in the declaration. Could you explain why the following answers dont have that and what the use is of the curly brackets?
The part within curly braces is one of the ways of instantiating and initializing an array. Since the compiler can easily count the number of elements that are present in that array, it creates an array of that length. That is why, we don't specify the length explicitly in this style.