Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Fri Sep 21, 2018 11:55 pm
by rdheepan
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,

Code: Select all

String[] sA = new String[1] ; sA[0] = "aaa";
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?

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Sat Sep 22, 2018 12:45 am
by admin
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.

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Sat Sep 22, 2018 1:37 am
by rdheepan
@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".

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Sat Sep 22, 2018 1:45 am
by admin
>@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 :)

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Sat Sep 22, 2018 1:47 am
by rdheepan
Okay. May by i am confused a bit. Will read through it again. Thanks for your help.

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Thu Aug 06, 2020 3:58 pm
by niiels93
Hello admin,



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?

Code: Select all

String[] sA = new String[] { "aaa"};

Code: Select all

String[] sA = {new String( "aaa")};
Thnx!

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Thu Aug 06, 2020 10:28 pm
by admin
niiels93 wrote:
Thu Aug 06, 2020 3:58 pm
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?

Code: Select all

String[] sA = new String[] { "aaa"};

Code: Select all

String[] sA = {new String( "aaa")};
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.

Arrays are an important topic for the exam and I suggest you to go through a good book before attempting mock exams.
I recommend Chapter 4 of OCA Java 8 Fundamentals (if you are studying for 1z0-808) or Chapter 9 of OCP Java 11 Part 1 Fundamentals (if you are studying for 1z0-815).

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1316 :

Posted: Fri Oct 09, 2020 4:17 pm
by niiels93
thanks!