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

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

Moderator: admin

Post Reply
sdey2000
Posts: 1
Joined: Wed Dec 03, 2014 9:15 am
Contact:

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

Post by sdey2000 »

Hi ,

Aren't strings immutable . Here value of s is assigned a new value 1 and same is displayed when run. Am i missing something here ?

Thanks

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

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

Post by admin »

Yes, strings are immutable but you can make a variable point to another String object. That is what is happening here. For example, when you do s += i; the reference variable s starts pointing to a new String object (which has the value of s+i) altogether. The original String object (which contains the value of the original string s) remains as is but without any reference pointing to it.

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

gezzoo
Posts: 2
Joined: Fri Feb 20, 2015 7:14 am
Contact:

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

Post by gezzoo »

public class Test {

static String s = "";

public static void m0(int a, int b) {
s += a;
m2();
m1(b);
}

public static void m1(int i) {
s += i;
}

public static void m2() {
throw new NullPointerException("aa");
}

public static void m() {
m0(1, 2);
m1(3);
}

public static void main(String args[]) {
try {
m();
} catch (Exception e) {
}
System.out.println(s);
}
}
The question is; what will this code print when run.
And the answer is 1, I understand this, because the exception is not caught, it is propagated back to the previous method.
But I always thought you had to use the throws clause in the method declaration to pass the exception to the previous method?
If not, when exactly should you use throws?

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

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

Post by admin »

You need to put the exception in the throws clause only if the exception is a checked exception and the method does not handle the exception. An uncaught runtime exception will be propagated to the caller without any need to put it in the throws clause.
If you like our products and services, please help us by posting your review here.

gezzoo
Posts: 2
Joined: Fri Feb 20, 2015 7:14 am
Contact:

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

Post by gezzoo »

Ah ofcourse.
Thanks for making that clear!

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

This question is not easy.
I think that it is tough.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

I feel strange that NullPointerException can have parameter in the statement.
throw new NullPointerException("aa");

What is the scope of this parameter?

Thanks a lot!

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

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

Post by admin »

Not sure what you mean by, "What is the scope of this parameter?". Which parameter and which scope are you referring to? What is the scope of the parameter when you do "new Integer(10)"?

NPE just like any other class. It does have a constructor that takes a String argument. That is what is being used here. Pretty much all standard exception classes do have a constructor that takes a String argument.
If you like our products and services, please help us by posting your review here.

Walter
Posts: 5
Joined: Tue Apr 04, 2017 3:35 pm
Contact:

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

Post by Walter »

I would have thought the program would have terminated in the catch clause and therefore not execute the System.out.println(s); but it does, is this because the exception is handled and program ends gracefully. I added this code:

Code: Select all

public static void main(String args[]) {
        try {
            m();
        } catch (Exception e) {
			e.printStackTrace();
        }
		finally{
			System.out.println("s");
		}
        System.out.println(s);
    }
It outputs the stacktrace, s and 1 so I am assuming that once the exception is handled properly println() will be executed?

Regards
Walter
Last edited by admin on Tue Apr 04, 2017 10:05 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

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

Post by admin »

Which println are you talking about? If call to m() throws an exception, it will be caught by the catch block, which will print the stack trace. Next, finally will be executed (which has a println), and since there is no return statement in the try, catch, or finally, the statements after the whole try/catch/finally will be executed (which is another println).
If you like our products and services, please help us by posting your review here.

Walter
Posts: 5
Joined: Tue Apr 04, 2017 3:35 pm
Contact:

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

Post by Walter »

Sorry, just considering the last bit of code:

Code: Select all

public static void main(String args[]) {
   try {
      m();
   } catch (Exception e) {
   }
   System.out.println(s);
}

I assumed that the program would terminate at the catch block and there wouldn't be any printout, but the last println() does get executed. I guess the question I am asking is, println() gets executed because the exception is handled and therefore not thrown out of main for the JVM to kill the thread, which is what i have done by removing the try/catch and the stacktrace throws an Exception in thread "main", which answers my own question. Thanks for the earlier response, you mentioning the return statements has gotten the ball rolling for me.

Regards
Walter
Last edited by admin on Wed Apr 05, 2017 2:59 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 111 guests