import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList myList = new ArrayList();
String[] myArray;
try{
while (true){
myList.add("My string");
}
}catch (RuntimeException e){
System.out.println("catch RunTimeException");
}catch (Exception e){
System.out.println("catch Exception");
}
System.out.println("Ready to use ");
}
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:265)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
at java.util.ArrayList.add(ArrayList.java:462)
at catch_error_loop_true.Main.main(Main.java:11)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Answer is C.
P.S. In code fragments you have to assume that required imports are done!!!
A JVM executa o codigo até um certo porto do loop "while(true)" na minha
máquina eu conseguir simular 56 entradas no ArrayList, ao verificar o consumo
de memoria excessivo a JVM para a execução para evitar um erro de
java.lang.OutOfMemoryError, dessa forma o código acima executa sem erro na JVM.
Porém existe um "Erro de tempo de execução" que a JVM omite.
A resposta correta é a questão C onde diz que existe um "Erro de tempo de execução!".
The while loop executes continuously, and keeps shoving strings into the array. Eventually the program will fail, because it will run out of memory (hence the error). Nothing gets printed, as there are no exceptions thrown and the println statement is never reached.
upvoted 7 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
letmein2
Highly Voted 5 years, 6 months agov323rs
Highly Voted 4 years, 11 months agovic88
Most Recent 2 weeks agoVicky_65
1 year, 8 months agoodzio33
1 year, 11 months agoakbiyik
2 years agoUAK94
2 years, 2 months agoMthlagi
2 years, 10 months agoaabbcc99
3 years, 2 months agotawa_z58
1 year, 10 months agorebelsta
2 years, 7 months agoclaudevan
3 years, 2 months agoTarik2190
3 years, 10 months agoAyla
4 years, 1 month ago365everyday
4 years, 2 months ago