About Question enthuware.ocpjp.v7.2.1379 :

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

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1379 :

Post by The_Nick »

Hi everyone,
The assignment of the string "gone" to s occurs after the first argument to print has been evaluated. If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated.
Does it mean that if it was:

 

Code: Select all

public static void main(String[] args)    {       String s = "going";       print(s="gone",  this.getString());    }    static void print(String a, String b)    {       System.out.println(a +", "+ b );    } String getString(){return "on the run" + (1/0) ;}
Getting an ArithmeticException on the second argument of print would erase the assignment "s="gone" of the first argument right?

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by admin »

Did you try it out?
-Paul.

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

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by admin »

You might want to go through http://docs.oracle.com/javase/specs/jls ... l#jls-15.6 for understanding the details.
HTH,
Paul.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by The_Nick »

Yep I got what you mean, I have tried it out sorry for asking I could have avoided it.

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by alkour »

It tried this code.

Code: Select all

public class StringEvaluation {

    public static void main(String[] args) {
        String s = "going";
        print(s, s = "gone");
    }
    static void print(String a, String b) {
        System.out.println(a + " , " + b);
    }    
}
It compiles and run without errors with output

Code: Select all

going , gone
Why the correct answer is "It will not compile"?

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

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by admin »

The correct option is option 1, "going, gone".

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by fariz.siracli »

hello. I could not understand explanation :( . The link also did not give me idea.
so please explain why does it return "going, gone" instead of "gone, gone". The value of variable s becomes "gone" after argument evaluation and in body of method it should print that value.

Etruskas
Posts: 2
Joined: Wed Nov 18, 2015 1:34 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1379 :

Post by Etruskas »

@fariz.siracli
These code lines:
String s = "going";       
print(s,  s = "gone");
resolves like this:
1. Java detected statement print(s, s = "gone");
2. Java resolves first operand s.
Resolved: print("going", s = "gone")

3. Java resolves second operand s = "gone"
Second operand is an expression itself.
3.1. Java resolves that expression s = "gone". Variable s is asigned "gone"
3.2. Java now has resolved all arguments. The call is print("going", "gone")
4. Java executes the call print("going", "gone").

Thinking more abstract.
People in math lessons are tend to learn to resolve complex expressions starting at most nested parenthesis.
Java works differently. Java resolves expressions starting from left to right.
And then, similar as in math, java works acording to operator precedence rules.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests