• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Two questions from javacamp

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright. I think I know why the first question gives OutOfMemoryError. Because the for loop is an infinite loop, it will create infinite number of Object arrays, until it runs out of memory and throws an error with oldlen = len.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. }

For ternary operator it evaluates from right to left.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second one appears to be setting the value b to the opposite value. For example (b=!b) is saying set false = !false. So it is true. Then the program does it again so it sets true = !true so it is false which would make it arrive at "hello". Hope this makes sense.
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by raghu nagabandi:

For ternary operator it evaluates from right to left.



Can you please elaborate? Which b=!b is evaluated first, the leftmost, the one in the middle, or the rightmost? And after evaluating a b=!b condition, which String does it go to, "hello", "Hello", "world", or "World"?
 
please buy this thing and then I get a fat cut of the action:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic