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

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

Moderator: admin

FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

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

Post by FrodoBaggins »

When I put the code in question into my Eclipse compiler I get the following compilation errors:

Type mismatch: cannot convert from ArrayList to List

Code: Select all

import java.awt.List;
import java.util.ArrayList;

public class O21465 {
public static void main(String[] args) {
	List s1 = new ArrayList();
	try{while(true){s1.add("sdfa");}
	}catch(RuntimeException e){
e.printStackTrace();
}
	System.out.println(s1.size());
}
}
What is wrong?

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

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

Post by admin »

We don't recommend using IDEs while studying for the exam. Please use command line and post the output from there.

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

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

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

Post by Ad9999 »

FrodoBaggins wrote:When I put the code in question into my Eclipse compiler I get the following compilation errors:

Type mismatch: cannot convert from ArrayList to List

Code: Select all

import java.awt.List;
import java.util.ArrayList;

public class O21465 {
public static void main(String[] args) {
	List s1 = new ArrayList();
	try{while(true){s1.add("sdfa");}
	}catch(RuntimeException e){
e.printStackTrace();
}
	System.out.println(s1.size());
}
}
What is wrong?
You are importing java.awt.List which is not the java.util.List collection/date structure. Also, infinite loop and out of memory Error. Not a run time exception.

FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

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

Post by FrodoBaggins »

Thanks. Why not use the compiler? In a real work environment I would use it all the time.

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

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

Post by admin »

FrodoBaggins wrote:Thanks. Why not use the compiler? In a real work environment I would use it all the time.
You have to use a compiler. What you mean is IDE. You should not use an IDE like Eclipse or NetBeans while preparing for the exam. See Chris Barrett's posts here for why: http://www.coderanch.com/t/639884/ocajp ... iled-OCAJP
If you like our products and services, please help us by posting your review here.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

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

Post by prasanna_ls »

I was expecting the System.out.println(s1.size()) to cause the compiler to complain about unreachable code. It IS an unreachable code isn't it? What's going on here?

(Yes, I did execute the code. And yes, there was no compilation errors. But it doesn't quite make sense to me.)

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

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

Post by admin »

Why do you think it is unreachable code?
-Paul.
If you like our products and services, please help us by posting your review here.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

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

Post by prasanna_ls »

Because there is an infinite loop i.e. while(true) and it doesn't contain any break statements in it. So, everything that comes after that is unreachable isn't it? So, I was expecting the code to not compile. That DOES seem to be the case for the statements in the same code block that come after while(true){}. But not so for the ones that aren't in the same code block.

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

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

Post by admin »

What if the code within the loop throws an exception?
If you like our products and services, please help us by posting your review here.

smily.sharma2009
Posts: 1
Joined: Thu Feb 25, 2016 1:43 pm
Contact:

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

Post by smily.sharma2009 »

even if it throws an exception, it will still not execute the last system.out.println.Can you please explain the reason for not throwing unreachable code.

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

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

Post by admin »

smily.sharma2009 wrote:even if it throws an exception, it will still not execute the last system.out.println.
Why do you think so? You can try replacing the code in the while loop with throw new RuntimeException() and see what happens.
If you like our products and services, please help us by posting your review here.

roosdebie
Posts: 3
Joined: Wed Nov 16, 2016 10:16 am
Contact:

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

Post by roosdebie »

Why is it a OutOfMemory error and not a StackOverflow error? Do we have to know for the exam the distinction?
Thanks

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

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

Post by admin »

OutOfMemory is when heap space runs out and StackOverflow is when stack space runs out. When you call a method from another method, the call details are placed on the stack. If you make a chain of such calls ( m1 calls m2, m2 calls m3, m3 calls m4 and so on), the stack (which is very small as compared to heap) will get filled up and you will get StackOverflow. This is not happening in this code.

Here is a good link: http://stackoverflow.com/questions/1143 ... emoryerror

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

rp.reshma
Posts: 3
Joined: Wed Feb 15, 2017 8:55 am
Contact:

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

Post by rp.reshma »

please help me to understand how thos code will throw java.lang.OutOfMemoryError .

Code: Select all

List s1 = new ArrayList( );    
  
   try{             
       while(true){          
       s1.add("sdfa"); 
       }   
   }catch(RuntimeException e) {
             e.printStackTrace();
   }  
  System.out.println(s1.size());
Last edited by admin on Wed Feb 15, 2017 9:00 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

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

Post by admin »

If you keep adding elements to an ArrayList forever, it will after some time run out of memory and the JVM will throw OutOfMemoryError.
You should try running the code.
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

Why is this not unreachable? What is it in the JLS that distinguishes between this:

Code: Select all

        try{
            while(true){}
        }catch(RuntimeException e){}
        System.out.println();
wherein the SOP is not considered unreachable and this:

Code: Select all

        while(true){}
        System.out.println();
wherein the SOP IS considered unreachable?

I am quite miffed because it seems to be yet another corner case of a corner case.

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

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

Post by admin »

Well, a JVM could always throw an Error while it is executing the while loop and it will be caught by the catch clause, in which case the println will be executed. So, it doesn't look unreachable to me. Not a corner case at all!
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

taking one of the previous posts one step further,

try{
while(true){}
}catch(RuntimeException e){}
System.out.println();

will the above code produce a run time OutOfMemoryError (which will not be caught by this catch anyway)?
the reason I ask is that this while loop does not do anything except churn, so is it really using up the memory to produce the OutOfMemoryError?

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

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

Post by admin »

what happened when you tried it out?
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

It kept on running for about 7 or 8 minutes, so I stopped it. That's why I wondered if I did not get any OutOfMemeoryError because the while loop doesn't do anything.

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

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

Post by admin »

That's right. OOMError is thrown when the JVM runs out of memory. In this case, the loop is not consuming any memory. Try creating some objects in the loop and you should get the error.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Will this exam have trick questions like this?

What type of error/exception the following piece of code will throw,

try{
while(true){}
} catch(RuntimeException e){}

1. OOMError
2. StackOverflowError
3. RuntimeException
4. no error

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

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

Post by admin »

It is possible. Also, why do you think it is a trick question? It is actually quite straightforward if you know the purpose of these exception classes.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

well the reason I would consider it a trick question is that usually endless events would either produce choice 1 or 2. And if one is not up on exceptions and errors, one may think of choice 3. However there it is, unpretentious answer, choice 4 :shock:

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

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

Post by admin »

1. OOMError : Is the code consuming any memory? creating objects? No.
2. StackOverflowError: Is the code putting anything on the stack that is not getting removed (usually happens in the case of recursion)? No.
3. RuntimeException: Is the code calling any method or doing any operation? No.

Option 4 it is :)
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 52 guests