Page 1 of 1

Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 332

Posted: Sun Mar 01, 2020 9:04 am
by javiut
States that List interface has a method with this signature.

void add(E e)

But this is not true has a method with this signature.

boolean add(E e);

Code: Select all

    /**
     * Appends the specified element to the end of this list (optional
     * operation).
     *
     * <p>Lists that support this operation may place limitations on what
     * elements may be added to this list.  In particular, some
     * lists will refuse to add null elements, and others will impose
     * restrictions on the type of elements that may be added.  List
     * classes should clearly specify in their documentation any restrictions
     * on what elements may be added.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this list
     */
    boolean add(E e);
In fact ArrayList returns true hardcoded.

Code: Select all

    /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
:)

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 332

Posted: Sun Mar 01, 2020 9:36 am
by admin
I think this has already been fixed because I see the boolean is mentioned as the return type and also has a comment saying It returns true.