Question 1. Given:
1. class
Test {
2. public static void main(
String[] args) {
3. int len = 0, oldlen = 0;
4. Object[] a = new Object[0];
5. try {
6. for (; ; ) {
7. ++len;
8. Object[] temp = new Object[oldlen = len];
9. temp[0] = a;
10. a = temp;
11. }
12. } catch (Error e) {
13. System.out.println(e + ", " + (oldlen==len));
14. }
15. }
16. }
What will happen when you attempt to compile and run the code?
A. It prints: java.lang.OutOfMemoryError, true.
B. It prints: java.lang.OutOfMemoryError, false.
C. It will not compile because of code at line 8.
D. It will not compile because of code at line 10.
E. It will not compile because of code at line 12.
I compiled the class and the answer is A. Can someone explain why the output is OutOfMemoryError and true?
Question 2. What is the output of the following code when compiled and run?
1. public class Test {
2. public static void main(String[] args) {
3. boolean b = false;
4. String s = (b=!b)?(b=!b)?"Hello":"hello" : ( b=!b)?"world":"World";
5. System.out.println(s);
6. }
7. }
A. Prints: Hello
B. Prints: hello
C. Prints: Helloworld
D. Prints: helloWorld
E. Compilation error.
Again, I compiled the class and the answer is B. Can someone explain why the output is hello?
[ August 28, 2007: Message edited by: Tobie Henderson ]