About Question enthuware.ocajp.i.v8.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
andripos
Posts: 2
Joined: Wed May 08, 2019 2:27 am
Contact:

About Question enthuware.ocajp.i.v8.2.1089

Post by andripos »

Explanation

1. i = (int) k.shortValue();   --> You can use *= here but then you can't complete the 4th line.
2. str += b; -->  You can't use =, or *= here. Only += is valid.
3. b = !b; --> You can't use anything other than = here.
4. c *= i; --> You can only use *= or +=. = is not valid. Further, if you use += here, you can't complete line 2.
But it compile
1. i *= (int) k.shortValue();
4. c *= i;

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

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

Post by admin »

Yes, but if you use *= on 1st line, how will you complete the 4th?
If you like our products and services, please help us by posting your review here.

andripos
Posts: 2
Joined: Wed May 08, 2019 2:27 am
Contact:

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

Post by andripos »

Code: Select all

Short k = 9; Integer i = 9; Boolean b = false;
        char c = 'a'; String str = "123";

        i *= (int) k.shortValue();
        str += b;
        b = !b;
         c *= i;

        System.out.println(c);
will print '?'

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

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

Post by admin »

You can't use *= more than once in this question.
If you like our products and services, please help us by posting your review here.

timwaagh
Posts: 6
Joined: Mon Sep 16, 2019 4:11 am
Contact:

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

Post by timwaagh »

I can use some explanation as to why you cannot assign a boolean to a String, but can do += (concatenate and assign). And also why we cannot assign an int to a char, but can do += (add and assign).

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

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

Post by admin »

A String variable is a reference type and a boolean is a primitive, so, you cannot assign a boolean value to String variable. Thus, str = b; will not compile.

+= is a special operator in Java, which translates to concatenation and assignment. So, str += b becomes str = str + b;
Now, + operator is overloaded in Java. If one of the operands of + is a string, it converts the other operand into a string and concatenates the two string to produce a new string. In this case, b's value is converted to String. Therefore, it is ok to assign the string produced by str + b to be assigned back to str.

This is usually explained well in all certification books. Which book are you following? You may start with this book if you don't want to spend too much.
If you like our products and services, please help us by posting your review here.

lucasdclopes
Posts: 2
Joined: Mon May 02, 2022 12:53 am
Contact:

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

Post by lucasdclopes »

Hi.

Please, can someone enlighten me on why the item 4 works?
c *= i
afaik this is equivalent to:
c = c * i;
right?
And..an arithmetic operation should give a result at least as an int type. If so, "c * i" returns an int. How can that int be assigned to a char variable without casting?
What am I missing?

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

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

Post by admin »

c *= i is not equivalent to c = c * i;
It is to equivalent c = (char)( c * i);
If you like our products and services, please help us by posting your review here.

lucasdclopes
Posts: 2
Joined: Mon May 02, 2022 12:53 am
Contact:

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

Post by lucasdclopes »

admin wrote:
Mon May 02, 2022 1:13 am
c *= i is not equivalent to c = c * i;
It is to equivalent c = (char)( c * i);
ohh!
So compound assignments always have casts. I see

then...

Code: Select all

X1 op= X2
means

Code: Select all

X1 = (TypeofX1)(X1 op X2)
Is that right?
Thank you!!

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

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

Post by admin »

Correct. This is written in JLS Section 15.26.2 Compound Assignment Operators:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: marpiva and 32 guests