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 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.
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.)
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.
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.
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.
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.
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.
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?
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.
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.
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.
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
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.