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

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

Moderator: admin

Post Reply
dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post by dannysantos1985 »

What is a constant expression?
Is it like 20 or 13434 that is assigned to a final variable. Give me examples, please

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

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

Post by admin »

Any expression that doesn't change its value at run time is a constant expression. For example,
2+4

final int a = 1; //if a is final
a+2 //then this is constant.

20 //this is obviously constant
If you like our products and services, please help us by posting your review here.

goncaloncpinto
Posts: 5
Joined: Mon Feb 15, 2016 5:43 am
Contact:

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

Post by goncaloncpinto »

Short version:
Any variable that has an assigned value at compile time is a constant.
a = 5 + 4 <- Constant
a = b + 3 <- If the value of b is not known at compile time, than 'a' is a constant

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

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

Post by admin »

goncaloncpinto wrote:Short version:
Any variable that has an assigned value at compile time is a constant.
a = 5 + 4 <- Constant
a = b + 3 <- If the value of b is not known at compile time, than 'a' is a constant
No, that is not correct. In your example, a cannot be considered a constant if it is not defined as final. If it is not final, it is a variable because its value can be changed at run time.
If you like our products and services, please help us by posting your review here.

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan »

I find it strange that option 4 is the only correct answer as line 3: i++; typically looks like it would not compile when i is not declared with/assigned a value? (int i;) How comes?

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

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

Post by admin »

It is ok to have a uninitialized variable. The problem occurs only when you try to use an uninitialized variable. So int i; is ok as long as you don't try to use i, for example, in a statement System.out.println(i);
If you like our products and services, please help us by posting your review here.

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan »

so what is then uninitialized i++ ?

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

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

Post by admin »

Not sure I understand your doubt. In this particular question, i is being initialized at line //2 i =c; before it is being used at line //3 i++;
So there is no compilation issue for i++; in this situation.
-Paul.
If you like our products and services, please help us by posting your review here.

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan »

no its fine, i misunderstood. its all good now. sorry for the bother

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

Post by vidyats »

narrow conversion always explicit right?
In this problem, it is mentioned that narrowing conversion does not apply to long or double.
I am not clear about this point.

char c = (char)30L; // This is narrow conversion right ?

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

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

Post by admin »

Yes, it is a narrowing primitive conversion but also a casting conversion. The explanation is actually about the implicit narrowing conversions that happen in case of assignment. These conversions are not applicable for float, double and long even if the value is small enough to fit into a target variable.

For example, int i = 20L; This will not compile even though 20 can easily fit into an int.

Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests