About Question enthuware.ocajp.i.v8.2.925 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Aayushma
Posts: 2
Joined: Sun Apr 30, 2017 8:07 am
Contact:

About Question enthuware.ocajp.i.v8.2.925 :

Post by Aayushma »

The question asks which of the following code "compiles without error"?

import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();

//INSERT CODE HERE
}
}
Then why option d) Double d = al.get(al.length); is correct since it compiles fine.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by admin »

Are you sure it compiles fine? Did you read the explanation given with this option?

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by JuergGogo »

al.add(111); is not a valid option.
The add() is expecting Double, so autoboxing has to take place. Since al.add(111d); is working fine.

I don't understand:
< autoboxing will call new Double(), its constructor accepts double and String. Why al.add("111") doesn't work then?
< why the int 111 is't implicitely casted to double, then autoboxed to Double?

Thank you.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by admin »

Because the rules of boxing are clearly defined in JLS ( https://docs.oracle.com/javase/specs/jl ... #jls-5.1.7 ) and it states that if you have an int value then it can only be autoboxed into an Integer. Converting an int to double and then boxing that double into a Double is not permitted in a single step.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by Rinkesh »

Can you give me an example of how the indexOf Method works?

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by Rinkesh »

And one more thing a1.contains("string") will print Boolean value false right??

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by admin »

The code given in the question itself illustrates the use of indexOf. Try adding a few elements in al and then see what it prints for al.indexOf(1.0);

>And one more thing a1.contains("string") will print Boolean value false right?
What happened when you tried it out?

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by Rinkesh »

Code: Select all

import java.util.*;
public class TestClass {
    public static void main(String[] args) throws Exception {
        ArrayList<Double> al = new ArrayList<>();
      al.add(123.0); al.add(114.0); al.add(138.0); al.add(153.0); al.add(168.0);
        System.out.println(al.indexOf(1.0)); //Getting -1 here no matter how many elements I add
        System.out.println(al.contains("string")); //This returns false as expected
    }
}
Last edited by admin on Fri Dec 15, 2017 9:15 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by admin »

Well, the purpose of indexOf is to find out if the passed object exists in the list. If so, it returns the index at which it exists otherwise it returns -1.
So why do you expect it to return anything other than -1 if your list doesn't contain 1.0?

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.925 :

Post by Rinkesh »

Okay got it.Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests