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

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

Moderator: admin

Post Reply
icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

I don't understand the answer for the last question (i.e., c *= i; ). My understanding is it means c = c * i;, of which the result may be beyond of the max value of a char.

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

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

Post by admin »

c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

Yeah, you are right. But one thing I want to point out is it will lose precision if the actual result of c*i is greater than the max value of a char. Am I correct?

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

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

Post by admin »

You are absolutely correct :)
If you like our products and services, please help us by posting your review here.

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

Post by kecker »

Why is there an explicit cast for the last one?

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

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

Post by admin »

Where do you see an explicit cast in the options?
If you like our products and services, please help us by posting your review here.

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

Post by kecker »

admin wrote:Where do you see an explicit cast in the options?
I saw it in your explanation above in explaining why the correct options are correct.
c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

HTH,
Paul.

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

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

Post by admin »

That is because JLS defines a compound operator that way. Please see this: http://docs.oracle.com/javase/specs/jls ... ls-15.26.2

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

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post by alkour »

It is not correct the last sentence of Explanations: 4. c *=i; .... if you use += here, you can't complete line2.

I run c +=i; it is OK and give 'j' char.

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

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

Post by admin »

I am not sure what you mean. += is correct but you can't use it for option 4 because then you will not be able to complete option 2.
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Type mismatch: cannot convert from int to Integer
Type mismatch: cannot convert from int to Integer
Cannot invoke shortValue() on the primitive type short
The operator *= is undefined for the argument type(s) char, Integer

at Enthuware.P9.main(P9.java:10)

for,
c*=i;
i =(int)k.shortValue();
Integer i=9;

Code: Select all

public class P9 
{

	public static void main(String[] args)
	{
		short k=9;
		Integer i=9;
		boolean b = false;
		char c='a';
		String str="123";
		
i =(int)k.shortValue();  // You can use *= here but then you can't complete the 4th line.
str += b; //  You can't use =, or *= here. Only += is valid.
b = !b; // You can't use anything other than = here.
c*=i; // You can only use *= or +=. = is not valid. Further, if you use += here, you can't complete line 2. 


	}

}
Is it eclipse problem or something else?

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

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

Post by admin »

Difficult to say. Please use command line.
If you like our products and services, please help us by posting your review here.

phusztek
Posts: 6
Joined: Tue Nov 22, 2016 11:04 am
Contact:

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

Post by phusztek »

admin wrote:c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

HTH,
Paul.

Actually it still doesn't compile because the precedence of the explicit cast is higher than the * operator. So I think the c *= i; transletes to c = (char) (c*i);

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

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

Post by admin »

Yes, you are right. It translates to c = (char) (c*i);
Paul.
If you like our products and services, please help us by posting your review here.

Neib133
Posts: 1
Joined: Tue May 01, 2018 3:16 am
Contact:

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

Post by Neib133 »

Feedback;
I have to admit that I did not understand this question. It took me a couple of minutes of picking my brain until I managed to re-read the text on the top. I somehow missed that I am supposed to put the pieces together that would work! :S

Post Reply

Who is online

Users browsing this forum: No registered users and 73 guests