About Question enthuware.ocajp.i.v7.2.1268 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1268 :

Post by ETS User »

In the answer it says that option 3 is wrong because 'i' is not initialized, however aren't all primitives initialized to 0?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by admin »

No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.

HTH,
Paul.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by Ambiorix »

admin wrote:No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.

HTH,
Paul.
How do we know that this is in a method given that we can only see 2 lines of code?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by admin »

Ambiorix wrote:
admin wrote:No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.

HTH,
Paul.
How do we know that this is in a method given that we can only see 2 lines of code?
No matter where you put these 2 lines, they are invalid.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by flex567 »

From the explenation:
No 2.
uses 'j +=5'. Now, this statement is preceded by 'int i=0,' and that means we are trying to declare variable j.

1) What does this sentence mean? Everyhing after

Code: Select all

int i=0
is decleration?

2) j = j + 5: This to me is not a declaration statement but initialization.(or just assignment)

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by admin »

It is talking about the initialization section of the if statement, which contains: int i=0, j+=5;

Since this statement starts with the type specification i.e. int, it is a declaration statement. This declaration will apply to i as well as j. i is ok, but j has already been declared before the if statement, that is why complier will complain.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by flex567 »

Aha I understand now, thanx

unranked
Posts: 3
Joined: Thu Jan 14, 2021 9:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by unranked »

5 statement little confusing me. On the other statements there's declarations for variables. But on 5's, there's no declaration for j.

int j, var j = 0;

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by admin »

I see that option 5 has int i = 0, j = 0; and not int j, var j = 0;

int i = 0, j = 0; is valid because the Java language allows it. It is called "compound declaration". i and j are considered to be of the same type i.e. int.

int i, var j = 0; is invalid because var declaration is not allowed in a compound declaration.

unranked
Posts: 3
Joined: Thu Jan 14, 2021 9:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1268 :

Post by unranked »

Yeah, sorry, sorry)
I didn't see that: int i = 0, j = 0;

thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests