Frenkel Smeijers

+ Follow
since Aug 29, 2018
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Frenkel Smeijers

Jeanne Boyarsky wrote:Imagine you have a million elements in your list. Asking for the millionth element of an ArrayList is fast because it goes to that index. However, in a LInkedLIst, Java has to go through each element to get to the last one. Definitely not fast.

Actually, a LinkedList in Java is a doubly-linked list.

Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

source: Javadoc
So asking for the millionth element in a list of a million elements is also fast for a LinkedList, but asking for the 500,000th element is indeed slow.
2 weeks ago

What is the result of the following?

A. {1=10, 2=20}
B. {1=10, 2=20, 3=null}
C. {1=10, 2=20, 3=3}
D. {1=13, 2=20}
E. {1=13, 2=20, 3=null}
F. {1=13, 2=20, 3=3}
G. The code does not compile.
H. An exception is thrown.

The book says F is the correct answer.

When I run this code I do get the same output as answer F, but is the order of the output of the toString()-method of a HashMap of Integers to Integers guaranteed?
In the book OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide it says on page 144 that there are three rules to know when you write your own Comparable#compareTo() method.

  • The number zero is returned when the current object is equal to the argument to compareTo().
  • A number less than zero is returned when the current object is smaller than the argument to compareTo().
  • A number greater than zero is returned when the current object is larger than the arguments to compareTo().

  • Then it gives an example

    And then is says

    Lines 3 through 5 implement the compareTo() method. Since an int is a primitive, we can't call a method on it. We could create the Integer wrapper class and call compareTo() on that. It's not necessary, though, since it is so easy to implement compareTo() correctly on our own.
    Lines 11 through 13 confirm that we've implemented compareTo() correctly. Line 11 compares a smaller id to a larger one, and therefore it prints a negative number. Line 12 compares animals with the same id, and therefore it prints 0. Line 13 compares a larger id to a smaller one, and therefore it returns a positive number.

    There's a bug in this code. When you change line 9 to = Integer.MIN_VALUE; the output becomes 2147483641, 0 and -2147483641. Line 11 still compares a smaller id to a larger one, but it prints a positive number. And line 13 still compares a larger id to a smaller one, but it returns a negative number.
    A better solution would be to "create the Integer wrapper class and call compareTo() on that" or call the method Integer#compare(int x, int y). Both solutions return -1, 0 and 1 (when I run it on OpenJDK Runtime Environment 8).
    There are even more possible solutions:
    (this gives a compile warning)
    ToIntFunction<? super Double>
    ToIntFunction<? super Number>
    ToIntFunction<? super Object>

    ToIntFunction<? super Serializable>    

    ToIntFunction<Comparable<? super Double>>
    ToIntFunction<Comparable<? extends Double>>
    ToIntFunction<Comparable<? extends Number>>
    ToIntFunction<Comparable<? extends Object>>

    ToIntFunction<? super Comparable>
    ToIntFunction<? super Comparable<?>>
    ToIntFunction<? super Comparable<Double>>
    ToIntFunction<? super Comparable<? super Double>>
    ToIntFunction<? super Comparable<? extends Double>>
    ToIntFunction<? super Comparable<? extends Number>>
    ToIntFunction<? super Comparable<? extends Object>>
    In that same section it says

    3: List<? super IOException> exceptions = new ArrayList<Exception>();


    Line 3 references a List that could be List<IOException> or List<Exception> or List<Object>.

    Just to be complete, List<Throwable> is also possible and even List<Serializable>.

    Jeanne Boyarsky wrote:The exam lives in a perfect world.

    Otherwise you could get an OutOfMemoryError on every statement in every question. That would make the exam very tedious.
    Here's a quote from the book Database System Concepts Fourth Edition:

    3.1 Structure of Relational Databases
    A relational database consists of a collection of tables, each of which is assigned a unique name. Each table has a structure similar to that presented in Chapter 2, where we represented E-R databses by tables. A row in a table represents a relationship among a set of values. Since a table is a colleciton of such relationships, there is a close correspondence between the concept of table and the mathematical concept of relation, from which the relational data model takes its name.

    They say it takes 30 minutes to receive the result. In my case it took 30 hours.

    The CertView site says it can take 48 hours.
    In OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide on page 317 it says:

    Checked exceptions have Exception in their hierarchy but not RuntimeException. They must be handled or declared. They can be thrown by the programmer or by the JVM.

    Can someone give me an example of a checked exception that is thrown by the JVM?

    The book gives some examples of exceptions that are thrown by the JVM, but they are either unchecked exceptions (ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException) or errors (ExceptionInInitializerError, StackOverflowError, NoClassDefFoundError).

    Which of the following are true? (Choose all that apply)
    A. Two arrays with the same content are equal.
    B. Two ArrayLists with the same content are equal.
    C. If you call remove(0) using an empty ArrayList object, it will compile successfully.
    D. If you call remove(0) using an empty ArrayList object, it will run successfully.
    E. None of the above.

    The books says B and C are the correct answer.

    ArrayList does override equals() and defines it as the same elements in the same order.

    About answer B, I think it's wrong.
    English is not my first language and I interpret "same content" as "contains the same elements ignoring the order of the elements".
    Is my interpretation of "same content" wrong?
    11 months ago

    In a table animal with 10 rows, how many times does true get output by the following? (Choose all that apply.)

    A. One
    B. Two
    C. Three
    D. Four
    E. Five
    F. The code does not compile.
    G. A SQLException is thrown.

    The book says answer C is the correct answer.

    One could argue that it doesn't compile, because there's no closing }.

    Anyway, could G also be a correct answer? When the driver doesn't support TYPE_SCROLL_INSENSITIVE and it downgrades to TYPE_FORWARD_ONLY, then rs.absolute(0) would throw a SQLException, right?
    Or when Derby isn't on the classpath.
    Or when the connection to the database suddenly drops.

    Peter Deak wrote:In the middle of page 213, the book asks which functional interface would we fill in the blank with to make the code compile. It concludes:

    "The only functional interface meeting all three of those criteria is DoubleToIntFunction"

    However, that is not the only functional interface that would allow the code to compile. You can also use ToIntFunction<Double> or ToIntFunction<Object>, and the code above will compile.

    And ToIntFunction<Number>.

    Identifying the Structure of a Relational Database
    Our sample database has two tables. One has a row for each species that is in our zoo. The other has a row for each animal. These two relate to each other because an animal belongs to a species. These relationships are why this type of database is called a relational database.

    I don't think that's true. They're not called relational because of the relationships between tables, it's because the data is stored in tables and a table in a database is a relation.