Everybody loves a pub quiz. So I collected some Java trivia questions ranging from syntax over frameworks. Have fun!
- What is the name of the following syntax elements: (1 point each)
<>
?: - Is this valid Java code and why or why not?
http://www.google.de
- When does a != a result in true for the same a?
- Which non internal package cannot be imported?
- What’s the result of “Hello” == “Hello” and why?
- How is this piece of code construct called?
new ArrayList() {{ add("Hello"); }};
- For which value does Math.abs(int) return a negative number?
- What does file.delete() do when the file in question cannot be deleted?
- What is the result of Arrays.asList(1, 2, 3).add(2) ?
Solutions
- diamond operator and ternary operator or elvis (in Groovy)
- Yes, http: is a label and // starts a comment
- Double.NaN
- The default package
- True. All String literals are interned.
- Double brace initialisation
- Integer.MIN_VALUE
- It returns false
- java.lang.UnsupportedOperationException
Framework
Here we name three classes from a JDK package or an open source framework, can you guess which package or framework it is?
- Closeable, Console, Serializable
- Objects, Properties, Random
- Callable, Future, Phaser
- Point, Robot, Toolkit
- AutoCloseable, Iterable, Process
- Mapping, PersistentCollection, Session
- EqualsBuilder, Mutable, StringUtils
- ApplicationContext, DataBinder, JdbcTemplate
- Frequency, Length, Volume
- Minutes, Weeks, Years
Solutions
- java.io
- java.util
- java.util.concurrent
- java.awt
- java.lang
- Hibernate
- Apache Commons (Lang)
- Spring
- JScience
- Joda Time
Nice! First part of Java Pub Quiz was quite easy for a reader of the Java Puzzlers book.Second part, was not so easy…I guess I outsourced the knowledge of the package names to my IDE. Could you please post more such Java puzzles? Maybe in the form of the puzzles by Wouter Coekaerts http://wouter.coekaerts.be/ ?