<< Red Hat Settles Hibernate Patent Dispute With Firestar/DataTern | Home | OpenJDK 6 Arrives In Debian Unstable Repository >>

Thursday Java Quiz: This One Should Be Easy...

... but it's not!

Since tomorrow is a holiday here, you get this week's Java Friday Quiz one day early.

Q: Will the following Java program compile, run without exceptions, or not?

import java.util.*;

public class Foo {
  public static void main(String[] args) {
    List list = Arrays.asList(3, 2, 1, 0);
    System.out.println(list.remove(3));
    System.out.println(list.remove(2));
    System.out.println(list.remove(1));
    System.out.println(list.remove(0));
  }
}

The strict rules apply this time: no books, no web, no running the compiler?

P.S.: BWT, my internet connection is acting up recently. Yesterday, I came home to find that my connection has been out for an hour and a half. I have to power cycle the DSL modem to bring the connection back up. I'll be out of town starting from tomorrow, which means this blog may be down until I come back next week.

P.P.S.: This month will mark the fifth anniversary (1810 days) of Weiqi Gao's Observations. I'm glad that I had the opportunity to express myself in ways I would never do in person. I'm grateful for the people who followed this blog through the years. I'm particularly delighted when people took the time to comment on this blog, either to correct my mistakes or to provide an alternative point of view on various topics.

Have a happy Fourth of July.

Tags :


Re: Thursday Java Quiz: This One Should Be Easy...

If I did not overlook something obvious, this example should compile. But it will not run because the created list is a non-modifiable instance.

Re: Thursday Java Quiz: This One Should Be Easy...

darnit I was going to guess compiles and runs fine, but if Arrays.asList results in an immutable List, then it will not run

Re: Thursday Java Quiz: This One Should Be Easy...

I'm nit-picking but the list is not immutable or unmodifiable. Its backed by the array, and follows the same rules as an array. You can still replace values, just not add or remove them.

Re: Thursday Java Quiz: This One Should Be Easy...

Happy 5th, Weiqi !

It takes a lot of work to be have quality and quantity for that long. We truly appreciate it.

Re: Thursday Java Quiz: This One Should Be Easy...

Interesting. I've never used asList for anything but pretty printing, so I'd never encountered this. The implementation is a nice use of nested classes. I would find interesting a discussion of the design decision to allow List to have optional methods vs. creating a separate derived interface MutableList. Thanks for posting this.

Add a comment Send a TrackBack