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

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

Moderator: admin

Post Reply
caromahal
Posts: 1
Joined: Tue Feb 21, 2017 2:54 am
Contact:

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

Post by caromahal »

Doesn't in point 2:

public class X{
static int k = 0;
static {
k= 10/0;
}}
throw an ArithmeticException ?
This exception is not available in the set of Possible solutions

BlietZ
Posts: 1
Joined: Tue Feb 21, 2017 9:50 am
Contact:

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

Post by BlietZ »

I believe all errors/exceptions in the initializer will throw a ExceptionInInitializerError.


edit: see this post viewtopic.php?f=2&t=2222

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

I would like to know why the following shows a compiler error ?

static
{
throw new NullPointerException();
}

but the below code doesn't .

static
{
if(true)
throw new NullPointerException();
}

if(true) evaluates to true always is known at compile time itself right ? Then why does it compile ? Whereas the first case doesn't?

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

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

Post by admin »

The if(true) construct is a special case that is exempted from compilation error by the specification to allow conditional compilation. There are many variations of the same question.

Please see this for details: https://docs.oracle.com/javase/specs/jl ... l#d5e19555

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

thanks :thumbup:

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

Post by kevin35 »

Code: Select all

// #1
	public static void main(String[] args) {
		int x;
      final boolean DEBUG=true;
		if (DEBUG) {
			x = 1;
			return;
		}
		System.out.println(x);  //compile error: The local variable x may not have been initialized
	}

Code: Select all

// #2
	public static void main(String[] args) {
		int x;
		if (true) {
			x = 1;
			return;
		}
		System.out.println(x);   //compiles fine
	}


Hi Paul, can you please explain what is the difference between #1 and #2?
DEBUG is a compile time constant, so why #1 does not compile?

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

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

Post by admin »

There should not be any issue for either one. Both compile fine for me.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

Post by kevin35 »

okay i tried to compile it with command prompt and it works.
wth is eclipse doing? :?

(I know you shouldn't use eclipse for the exam, I used command prompt for 2 weeks while learning for the exam then it was to annoying and I switched back to IDE)

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests